Skip to content

Commit

Permalink
fix(render): set and unset RUNEWIDTH_EASTASIAN in program lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Aug 23, 2023
1 parent b5e2519 commit 6af5f51
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"sync"
"syscall"

"github.com/charmbracelet/log"

Check failure on line 23 in tea.go

View workflow job for this annotation

GitHub Actions / coverage (^1, ubuntu-latest)

no required module provides package github.com/charmbracelet/log; to add it:

Check failure on line 23 in tea.go

View workflow job for this annotation

GitHub Actions / build (^1, ubuntu-latest)

no required module provides package github.com/charmbracelet/log; to add it:

"github.com/containerd/console"
isatty "github.com/mattn/go-isatty"
"github.com/muesli/cancelreader"
Expand Down Expand Up @@ -169,6 +171,11 @@ type Program struct {
// fps is the frames per second we should set on the renderer, if
// applicable,
fps int

// We initialize RUNEWIDTH_EASTASIAN to resolve runewidth conflicts. This
// value will be hold the original value of the environment variable on
// startup, so we can reset it on program shutdown.
runewidthEastAsian string
}

// Quit is a special command that tells the Bubble Tea program to exit.
Expand Down Expand Up @@ -406,6 +413,10 @@ func (p *Program) eventLoop(model Model, cmds chan Cmd) (Model, error) {
// terminated by either [Program.Quit], [Program.Kill], or its signal handler.
// Returns the final model.
func (p *Program) Run() (Model, error) {
p.runewidthEastAsian = os.Getenv("RUNEWIDTH_EASTASIAN")
if err := os.Setenv("RUNEWIDTH_EASTASIAN", "0"); err != nil {
log.Error("Unable to set RUNEWIDTH_EASTASIAN environment variable; it should be set to true for compatiblity purposes", "err", err)
}
handlers := handlers{}
cmds := make(chan Cmd)
p.errs = make(chan error)
Expand Down Expand Up @@ -596,13 +607,19 @@ func (p *Program) Send(msg Msg) {
// If the program is not running this will be a no-op, so it's safe to call
// if the program is unstarted or has already exited.
func (p *Program) Quit() {
if err := os.Setenv("RUNEWIDTH_EASTASIAN", p.runewidthEastAsian); err != nil {
log.Error("Unable to unset RUNEWIDTH_EASTASIAN environment variable; it is set to true for compatiblity purposes", "err", err)
}
p.Send(Quit())
}

// Kill stops the program immediately and restores the former terminal state.
// The final render that you would normally see when quitting will be skipped.
// [program.Run] returns a [ErrProgramKilled] error.
func (p *Program) Kill() {
if err := os.Setenv("RUNEWIDTH_EASTASIAN", p.runewidthEastAsian); err != nil {
log.Error("Unable to unset RUNEWIDTH_EASTASIAN environment variable; it is set to true for compatiblity purposes", "err", err)
}
p.cancel()
}

Expand Down

0 comments on commit 6af5f51

Please sign in to comment.