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"
"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
}

View File

@ -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 {