Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions cmd/rexdiff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// rexdiff [flags] [repo]
// rexdiff install
// rexdiff uninstall
// rexdiff update
package main

import (
Expand Down Expand Up @@ -42,6 +43,7 @@ func main() {
const usage = `usage: rexdiff [flags] [repo]
rexdiff install
rexdiff uninstall
rexdiff update

flags:
`
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions cmd/rexdiff/testdata/script/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'
38 changes: 35 additions & 3 deletions tui/kitty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions tui/kitty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
4 changes: 3 additions & 1 deletion tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) })
Expand Down
3 changes: 3 additions & 0 deletions tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading