Cleanup prompt handling

This commit is contained in:
Kovid Goyal 2022-11-06 07:20:04 +05:30
parent 13a266aa42
commit 2f2dbfb45f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 42 additions and 27 deletions

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"kitty/tools/cli/markup"
"kitty/tools/tui/loop" "kitty/tools/tui/loop"
"kitty/tools/wcswidth" "kitty/tools/wcswidth"
) )
@ -113,15 +114,18 @@ func (self *kill_ring) clear() {
self.items = self.items.Init() self.items = self.items.Init()
} }
type Prompt struct {
text string
length int
}
type Readline struct { type Readline struct {
prompt string prompt, continuation_prompt, reverse_search_prompt, forward_search_prompt Prompt
prompt_len int
continuation_prompt string mark_prompts bool
continuation_prompt_len int loop *loop.Loop
mark_prompts bool history *History
loop *loop.Loop kill_ring kill_ring
history *History
kill_ring kill_ring
// The number of lines after the initial line on the screen // The number of lines after the initial line on the screen
cursor_y int cursor_y int
@ -143,21 +147,32 @@ func New(loop *loop.Loop, r RlInit) *Readline {
if hc == 0 { if hc == 0 {
hc = 8192 hc = 8192
} }
c := markup.New(true)
ans := &Readline{ ans := &Readline{
prompt: r.Prompt, prompt_len: wcswidth.Stringwidth(r.Prompt), mark_prompts: !r.DontMarkPrompts, mark_prompts: !r.DontMarkPrompts,
loop: loop, lines: []string{""}, history: NewHistory(r.HistoryPath, hc), kill_ring: kill_ring{items: list.New().Init()}, 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 { if r.ContinuationPrompt != "" || !r.EmptyContinuationPrompt {
ans.continuation_prompt = r.ContinuationPrompt t = r.ContinuationPrompt
if ans.continuation_prompt == "" { if t == "" {
ans.continuation_prompt = "> " t = c.Yellow(">") + " "
} }
} }
ans.continuation_prompt_len = wcswidth.Stringwidth(ans.continuation_prompt) ans.continuation_prompt = make_prompt(t, true)
if ans.mark_prompts { ans.reverse_search_prompt = make_prompt(c.Blue("?")+": ", false)
ans.prompt = PROMPT_MARK + "A" + ST + ans.prompt ans.forward_search_prompt = make_prompt(c.Cyan("/")+": ", false)
ans.continuation_prompt = PROMPT_MARK + "A;k=s" + ST + ans.continuation_prompt
}
return ans return ans
} }

View File

@ -30,6 +30,13 @@ type ScreenLine struct {
Text string 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 { func (self *Readline) get_screen_lines() []*ScreenLine {
if self.screen_width == 0 { if self.screen_width == 0 {
self.update_current_screen_size() self.update_current_screen_size()
@ -38,10 +45,7 @@ func (self *Readline) get_screen_lines() []*ScreenLine {
found_cursor := false found_cursor := false
cursor_at_start_of_next_line := false cursor_at_start_of_next_line := false
for i, line := range self.lines { for i, line := range self.lines {
plen := self.prompt_len plen := self.prompt_for_line_number(i).length
if i > 0 {
plen = self.continuation_prompt_len
}
offset := 0 offset := 0
has_cursor := i == self.cursor.Y has_cursor := i == self.cursor.Y
for is_first := true; is_first || offset < len(line); is_first = false { for is_first := true; is_first || offset < len(line); is_first = false {
@ -101,11 +105,7 @@ func (self *Readline) redraw() {
self.loop.QueueWriteString("\n") self.loop.QueueWriteString("\n")
} }
if sl.PromptLen > 0 { if sl.PromptLen > 0 {
if i == 0 { self.loop.QueueWriteString(self.prompt_for_line_number(i).text)
self.loop.QueueWriteString(self.prompt)
} else {
self.loop.QueueWriteString(self.continuation_prompt)
}
} }
self.loop.QueueWriteString(sl.Text) self.loop.QueueWriteString(sl.Text)
if sl.CursorCell > -1 { if sl.CursorCell > -1 {