diff --git a/.goreleaser.yml b/.goreleaser.yml index 44fc1dc..5d5b0d1 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -30,8 +30,14 @@ changelog: # The release workflow runs when someone publishes a release on GitHub. # GoReleaser adds the files to that release. It does not make a new one, # and it keeps the notes. +# +# GoReleaser publishes the release again after the upload. Without +# make_latest it sends no "latest" to GitHub, and the release that the curl +# install and the update of a downloaded rexdiff get (releases/latest) +# stays old. release: mode: keep-existing + make_latest: true # Each release writes Casks/rexdiff.rb to this repo. main requires CI to # pass, and GITHUB_TOKEN cannot bypass that, so the push uses a deploy key diff --git a/README.md b/README.md index 38db45e..596ea18 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,10 @@ The script downloads the release for this system, checks its checksum, and puts the binary in `~/.local/bin`, or in `REXDIFF_BIN_DIR`. It also says how to add the directory to `PATH` if it is not there. +To update rexdiff, run `rexdiff update`. It installs the latest release the +way you installed rexdiff: with Homebrew, go install, or a download like the +curl script. + With Go: ```sh diff --git a/cmd/rexdiff/main.go b/cmd/rexdiff/main.go index 8401189..70900e5 100644 --- a/cmd/rexdiff/main.go +++ b/cmd/rexdiff/main.go @@ -8,6 +8,7 @@ // rexdiff [flags] [repo] // rexdiff install // rexdiff uninstall +// rexdiff update package main import ( @@ -42,6 +43,7 @@ func main() { const usage = `usage: rexdiff [flags] [repo] rexdiff install rexdiff uninstall + rexdiff update flags: ` @@ -58,6 +60,15 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error { return install(ctx, bin, args[1:], stdout, stderr) case "uninstall": return uninstall(ctx, stdout) + case "update": + if len(args) > 1 { + return errors.New("update takes no arguments") + } + bin, err := os.Executable() + if err != nil { + return err + } + return selfUpdate(ctx, update.Detect(bin, version), stdout) } } f, err := parseFlags(args, stderr) @@ -125,6 +136,31 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error { return fmt.Errorf("start the new rexdiff: %w; your comments are saved in %s", err, resume) } +// selfUpdate installs the latest release with u, the way the user installed +// rexdiff, as the U key of the viewer does. u is nil if rexdiff cannot +// update itself. +func selfUpdate(ctx context.Context, u *update.Updater, stdout io.Writer) error { + if u == nil { + return errors.New("this rexdiff cannot update itself; " + + "install it again the way you installed it") + } + r, isNewer, err := u.Latest(ctx) + if err != nil { + return fmt.Errorf("find the latest release: %w", err) + } + have := update.Version(version) + if !isNewer { + fmt.Fprintf(stdout, "rexdiff %s is the latest\n", have) + return nil + } + fmt.Fprintf(stdout, "updating rexdiff %s to %s\n", have, r.Tag) + if err := u.Install(ctx, r); err != nil { + return err + } + fmt.Fprintf(stdout, "updated rexdiff to %s\n", r.Tag) + return nil +} + // resumeEnv is the variable that gives a restarted rexdiff the path of the // file with the state from before the update. rexdiff uses a variable, not a // flag, so that the arguments of the user do not change. diff --git a/cmd/rexdiff/testdata/script/help.txt b/cmd/rexdiff/testdata/script/help.txt index 36d75f4..90e0657 100644 --- a/cmd/rexdiff/testdata/script/help.txt +++ b/cmd/rexdiff/testdata/script/help.txt @@ -12,3 +12,9 @@ stdout '^\(devel\)$' # install -h writes no file. exec rexdiff install -h ! exists .config/rex + +# A build for development cannot update itself, and says so. +! exec rexdiff update +stderr '^rexdiff: this rexdiff cannot update itself' +! exec rexdiff update extra +stderr 'update takes no arguments' diff --git a/tui/kitty.go b/tui/kitty.go index 7e33b00..de69980 100644 --- a/tui/kitty.go +++ b/tui/kitty.go @@ -36,6 +36,11 @@ import ( // it, to quit or to run an editor, ttyOut deletes them, or they would cover // the shell. Until Bubble Tea comes back, the pictures wait. // +// Until the first pictures go out, the text of Bubble Tea would show as it +// is, with gaps in the box lines and in other colours, and then change into +// the pictures. covered tells the view to show no text until then, and +// onCover makes it draw the text once the pictures cover it. +// // A terminal that reads slowly blocks a write. The writes hold wmu, and // queue takes only mu, so a frame that waits for the terminal does not stop // the program: the keys still work, and q still quits. @@ -47,6 +52,10 @@ type ttyOut struct { timer *time.Timer away bool // Bubble Tea left the alternate screen leaves int // how many times Bubble Tea left it + covered bool // pictures went out since Bubble Tea came to the screen + // onCover runs, in its own goroutine, when the first pictures go out + // after Bubble Tea came to the alternate screen. It can be nil. + onCover func() } // pictureWait is how long the pictures of a frame wait for the text of the @@ -64,7 +73,7 @@ func (o *ttyOut) Write(p []byte) (int, error) { o.mu.Lock() leaving := !o.away && bytes.Contains(p, []byte(ansi.ResetModeAltScreenSaveCursor)) if leaving { - o.away, o.leaves, o.pending = true, o.leaves+1, nil + o.away, o.leaves, o.pending, o.covered = true, o.leaves+1, nil, false } if bytes.Contains(p, []byte(ansi.SetModeAltScreenSaveCursor)) { o.away = false @@ -82,11 +91,32 @@ func (o *ttyOut) Write(p []byte) (int, error) { } n, err := o.File.Write(p) if err == nil && len(pictures) > 0 { - _, err = o.File.Write(pictures) + if _, err = o.File.Write(pictures); err == nil { + o.cover() + } } return n, err } +// cover notes that pictures went out. The first pictures run onCover. +func (o *ttyOut) cover() { + o.mu.Lock() + first := !o.covered && !o.away + o.covered = o.covered || first + o.mu.Unlock() + if first && o.onCover != nil { + go o.onCover() // it sends to the program, which can wait for this write + } +} + +// isCovered reports whether pictures went out since Bubble Tea came to the +// alternate screen. +func (o *ttyOut) isCovered() bool { + o.mu.Lock() + defer o.mu.Unlock() + return o.covered +} + // left returns how many times Bubble Tea left the alternate screen. The // pictures are gone after each time. func (o *ttyOut) left() int { @@ -117,7 +147,9 @@ func (o *ttyOut) flush() { } o.mu.Unlock() if len(pictures) > 0 { - _, _ = o.File.Write(pictures) + if _, err := o.File.Write(pictures); err == nil { + o.cover() + } } } diff --git a/tui/kitty_test.go b/tui/kitty_test.go index 4b986fd..123e826 100644 --- a/tui/kitty_test.go +++ b/tui/kitty_test.go @@ -228,3 +228,40 @@ func TestKittyRedrawAndResize(t *testing.T) { t.Fatalf("new cells did not draw all again: %q", s) } } + +// TestKittyHidesTextUntilPictures checks that the view shows no text until +// the first pictures go out, so the bare text does not show at the start or +// after the editor, and that the view is asked to draw the text then. +func TestKittyHidesTextUntilPictures(t *testing.T) { + m, _ := newTestModel(t) + out, _ := testOut(t) + covers := make(chan struct{}, 4) + out.onCover = func() { covers <- struct{}{} } + m.kitty = newKittyScreen(out, nil) + if v := m.View(); v.Content != "" { + t.Fatal("text before the colours settled") + } + m.kitty.settled.Store(true) + if v := m.View(); v.Content != "" { + t.Fatal("text in the frame of the first pictures") + } + _, _ = out.Write([]byte("frame")) // the pictures go after it + <-covers + if v := m.View(); v.Content == "" { + t.Fatal("no text after the pictures went out") + } + _, _ = out.Write([]byte("more")) + if len(covers) != 0 { + t.Fatal("onCover ran again without a leave") + } + + _, _ = out.Write([]byte(ansi.ResetModeAltScreenSaveCursor)) // the editor + if v := m.View(); v.Content != "" { + t.Fatal("text after leaving the alternate screen") + } + _, _ = out.Write([]byte(ansi.SetModeAltScreenSaveCursor)) + <-covers + if v := m.View(); v.Content == "" { + t.Fatal("no text after coming back") + } +} diff --git a/tui/tui.go b/tui/tui.go index 17dd2cd..a4613f6 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -120,7 +120,9 @@ func Run(ctx context.Context, o Options) (resume string, err error) { program := tea.NewProgram(&m, opts...) if out != nil { // After NewProgram: the timer of the screen sends to program. - m.kitty = newKittyScreen(out, func() { program.Send(kittyWakeMsg{}) }) + wake := func() { program.Send(kittyWakeMsg{}) } + m.kitty = newKittyScreen(out, wake) + out.onCover = wake } if o.Rex != nil { go o.Rex.Watch(ctx, func(e rex.Event) { program.Send(eventMsg(e)) }) diff --git a/tui/view.go b/tui/view.go index cc12382..0b35a8b 100644 --- a/tui/view.go +++ b/tui/view.go @@ -721,6 +721,9 @@ func (m *model) View() tea.View { if m.kitty != nil { m.kitty.colors(m.fg, m.bg) m.kitty.draw(screen, m.w, m.h, caret) + if !m.kitty.out.isCovered() { // see ttyOut + v.Content, v.Cursor = "", nil + } } return v }