From 2f2dbfb45fe7b6b8556997b0054d7b2f6511952b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 6 Nov 2022 07:20:04 +0530 Subject: [PATCH] Cleanup prompt handling --- tools/tui/readline/api.go | 51 ++++++++++++++++++++++++-------------- tools/tui/readline/draw.go | 18 +++++++------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/tools/tui/readline/api.go b/tools/tui/readline/api.go index a97e75335..70f9be73c 100644 --- a/tools/tui/readline/api.go +++ b/tools/tui/readline/api.go @@ -7,6 +7,7 @@ import ( "fmt" "strings" + "kitty/tools/cli/markup" "kitty/tools/tui/loop" "kitty/tools/wcswidth" ) @@ -113,15 +114,18 @@ func (self *kill_ring) clear() { self.items = self.items.Init() } +type Prompt struct { + text string + length int +} + type Readline struct { - prompt string - prompt_len int - continuation_prompt string - continuation_prompt_len int - mark_prompts bool - loop *loop.Loop - history *History - kill_ring kill_ring + prompt, continuation_prompt, reverse_search_prompt, forward_search_prompt Prompt + + mark_prompts bool + loop *loop.Loop + history *History + kill_ring kill_ring // The number of lines after the initial line on the screen cursor_y int @@ -143,21 +147,32 @@ func New(loop *loop.Loop, r RlInit) *Readline { if hc == 0 { hc = 8192 } + c := markup.New(true) ans := &Readline{ - prompt: r.Prompt, prompt_len: wcswidth.Stringwidth(r.Prompt), mark_prompts: !r.DontMarkPrompts, - loop: loop, lines: []string{""}, history: NewHistory(r.HistoryPath, hc), kill_ring: kill_ring{items: list.New().Init()}, + mark_prompts: !r.DontMarkPrompts, + loop: loop, lines: []string{""}, history: NewHistory(r.HistoryPath, hc), kill_ring: kill_ring{items: list.New().Init()}, } + make_prompt := func(text string, is_secondary bool) Prompt { + if ans.mark_prompts { + m := PROMPT_MARK + "A" + if is_secondary { + m += ";k=s" + } + text = m + ST + text + } + return Prompt{text: text, length: wcswidth.Stringwidth(text)} + } + ans.prompt = make_prompt(r.Prompt, false) + t := "" if r.ContinuationPrompt != "" || !r.EmptyContinuationPrompt { - ans.continuation_prompt = r.ContinuationPrompt - if ans.continuation_prompt == "" { - ans.continuation_prompt = "> " + t = r.ContinuationPrompt + if t == "" { + t = c.Yellow(">") + " " } } - ans.continuation_prompt_len = wcswidth.Stringwidth(ans.continuation_prompt) - if ans.mark_prompts { - ans.prompt = PROMPT_MARK + "A" + ST + ans.prompt - ans.continuation_prompt = PROMPT_MARK + "A;k=s" + ST + ans.continuation_prompt - } + ans.continuation_prompt = make_prompt(t, true) + ans.reverse_search_prompt = make_prompt(c.Blue("?")+": ", false) + ans.forward_search_prompt = make_prompt(c.Cyan("/")+": ", false) return ans } diff --git a/tools/tui/readline/draw.go b/tools/tui/readline/draw.go index 5f1c85737..99d7d6ccb 100644 --- a/tools/tui/readline/draw.go +++ b/tools/tui/readline/draw.go @@ -30,6 +30,13 @@ type ScreenLine struct { Text string } +func (self *Readline) prompt_for_line_number(i int) Prompt { + if i == 0 { + return self.prompt + } + return self.continuation_prompt +} + func (self *Readline) get_screen_lines() []*ScreenLine { if self.screen_width == 0 { self.update_current_screen_size() @@ -38,10 +45,7 @@ func (self *Readline) get_screen_lines() []*ScreenLine { found_cursor := false cursor_at_start_of_next_line := false for i, line := range self.lines { - plen := self.prompt_len - if i > 0 { - plen = self.continuation_prompt_len - } + plen := self.prompt_for_line_number(i).length offset := 0 has_cursor := i == self.cursor.Y for is_first := true; is_first || offset < len(line); is_first = false { @@ -101,11 +105,7 @@ func (self *Readline) redraw() { self.loop.QueueWriteString("\n") } if sl.PromptLen > 0 { - if i == 0 { - self.loop.QueueWriteString(self.prompt) - } else { - self.loop.QueueWriteString(self.continuation_prompt) - } + self.loop.QueueWriteString(self.prompt_for_line_number(i).text) } self.loop.QueueWriteString(sl.Text) if sl.CursorCell > -1 {