From a4e43b39258f6b35c8d9ece93642fa7f85243353 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 16 Nov 2022 20:44:13 +0530 Subject: [PATCH] Use a pager for the help interactive command as well --- tools/cli/help.go | 16 ++++++++++------ tools/cmd/at/shell.go | 20 +++++++++++--------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tools/cli/help.go b/tools/cli/help.go index 99b99cbe7..548944e3c 100644 --- a/tools/cli/help.go +++ b/tools/cli/help.go @@ -92,6 +92,14 @@ func (self *Command) ShowHelp() { self.ShowHelpWithCommandString(strings.TrimSpace(self.CommandStringForUsage())) } +func ShowHelpInPager(text string) { + pager := exec.Command(kitty.DefaultPager[0], kitty.DefaultPager[1:]...) + pager.Stdin = strings.NewReader(text) + pager.Stdout = os.Stdout + pager.Stderr = os.Stderr + pager.Run() +} + func (self *Command) ShowHelpWithCommandString(cs string) { formatter := markup.New(tty.IsTerminal(os.Stdout.Fd())) screen_width := 80 @@ -144,12 +152,8 @@ func (self *Command) ShowHelpWithCommandString(cs string) { output_text := output.String() // fmt.Printf("%#v\n", output_text) if formatter.EscapeCodesAllowed() { - pager := exec.Command(kitty.DefaultPager[0], kitty.DefaultPager[1:]...) - pager.Stdin = strings.NewReader(output_text) - pager.Stdout = os.Stdout - pager.Stderr = os.Stderr - pager.Run() + ShowHelpInPager(output_text) } else { - os.Stdout.Write([]byte(output_text)) + os.Stdout.WriteString(output_text) } } diff --git a/tools/cmd/at/shell.go b/tools/cmd/at/shell.go index c837ffc0d..c4cf07e43 100644 --- a/tools/cmd/at/shell.go +++ b/tools/cmd/at/shell.go @@ -96,19 +96,21 @@ func shell_loop(rl *readline.Readline, kill_if_signaled bool) (int, error) { return 0, nil } -func print_basic_help() { - fmt.Println("Control kitty by sending it commands.") - fmt.Println() - fmt.Println(formatter.Title("Commands") + ":") +func show_basic_help() { + output := strings.Builder{} + fmt.Fprintln(&output, "Control kitty by sending it commands.") + fmt.Fprintln(&output) + fmt.Fprintln(&output, formatter.Title("Commands")+":") r := EntryPoint(cli.NewRootCommand()) for _, g := range r.SubCommandGroups { for _, sc := range g.SubCommands { - fmt.Println(" ", formatter.Green(sc.Name)) - fmt.Println(" ", sc.ShortDescription) + fmt.Fprintln(&output, " ", formatter.Green(sc.Name)) + fmt.Fprintln(&output, " ", sc.ShortDescription) } } - fmt.Println(" ", formatter.Green("exit")) - fmt.Println(" ", "Exit this shell") + fmt.Fprintln(&output, " ", formatter.Green("exit")) + fmt.Fprintln(&output, " ", "Exit this shell") + cli.ShowHelpInPager(output.String()) } func exec_command(rl *readline.Readline, cmdline string) bool { @@ -131,7 +133,7 @@ func exec_command(rl *readline.Readline, cmdline string) bool { hi.ExitCode = 0 defer rl.AddHistoryItem(hi) if len(parsed_cmdline) == 1 { - print_basic_help() + show_basic_help() return true } switch parsed_cmdline[1] {