kitty/tools/tui/hold.go
Kovid Goyal ed0d236708
Program status protocol: fixes and improvements
- Use the SIMD base64 decoder
- Allow ids of up to 263 bytes, the 128 byte limit in the spec is an error
- Drop transient records when a program run with launch --hold exits, even without shell integration
- Drop done and error records when a key is pressed in the window
- Only build the records list when there are watchers
- Count the raw bytes of a report against the length limit
- Strip only ASCII whitespace and discard reports with over long keys regardless of case
- Identical reports do not change which record is shown in the tab bar
2026-10-13 08:50:39 +05:30

77 lines
1.8 KiB
Go

// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package tui
import (
"errors"
"fmt"
"os"
"os/exec"
"github.com/kovidgoyal/kitty/tools/tui/loop"
)
var _ = fmt.Print
// Tells kitty that a program it cannot see exit has exited, so that it can drop
// the program status (OSC 7501) records that do not outlive the program
const PROGRAM_STATUS_PROCESS_EXITED = "\x1b]7501;process-exited\x1b\\"
func HoldTillEnter(start_with_newline bool) {
lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors, loop.NoMouseTracking)
if err != nil {
return
}
lp.OnInitialize = func() (string, error) {
lp.SetCursorVisible(false)
if start_with_newline {
lp.QueueWriteString("\r\n")
}
lp.QueueWriteString("\x1b[1;32mPress Enter or Esc to exit\x1b[m")
return "", nil
}
lp.OnFinalize = func() string {
lp.SetCursorVisible(true)
return ""
}
lp.OnKeyEvent = func(event *loop.KeyEvent) error {
if event.MatchesPressOrRepeat("enter") || event.MatchesPressOrRepeat("kp_enter") || event.MatchesPressOrRepeat("esc") || event.MatchesPressOrRepeat("ctrl+c") || event.MatchesPressOrRepeat("ctrl+d") {
event.Handled = true
lp.Quit(0)
}
return nil
}
lp.Run()
}
func ExecAndHoldTillEnter(cmdline []string) (int, error) {
if len(cmdline) == 0 {
HoldTillEnter(false)
return 0, nil
}
var cmd *exec.Cmd
if len(cmdline) == 1 {
cmd = exec.Command(cmdline[0])
} else {
cmd = exec.Command(cmdline[0], cmdline[1:]...)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
var ee *exec.ExitError
err := cmd.Run()
_, _ = os.Stdout.WriteString(PROGRAM_STATUS_PROCESS_EXITED)
is_exit_error := err != nil && errors.As(err, &ee)
if err != nil && !is_exit_error {
return 1, err
}
HoldTillEnter(true)
if err == nil {
return 0, nil
}
if is_exit_error {
return ee.ExitCode(), nil
}
return 1, err
}