More work on readline
This commit is contained in:
parent
c8296a44eb
commit
32059dba7e
@ -34,6 +34,24 @@ func shell_loop(kill_if_signaled bool) (int, error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
lp.OnResize = func(old_size loop.ScreenSize, new_size loop.ScreenSize) error {
|
||||
rl.Redraw()
|
||||
return nil
|
||||
}
|
||||
|
||||
lp.OnKeyEvent = func(event *loop.KeyEvent) error {
|
||||
err := rl.OnKeyEvent(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if event.Handled {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
lp.OnText = rl.OnText
|
||||
|
||||
err = lp.Run()
|
||||
if err != nil {
|
||||
return 1, err
|
||||
|
||||
@ -227,6 +227,14 @@ func (self *Loop) ClearToEndOfScreen() {
|
||||
self.QueueWriteString("\x1b[J")
|
||||
}
|
||||
|
||||
func (self *Loop) StartBracketedPaste() {
|
||||
self.QueueWriteString(BRACKETED_PASTE.EscapeCodeToSet())
|
||||
}
|
||||
|
||||
func (self *Loop) EndBracketedPaste() {
|
||||
self.QueueWriteString(BRACKETED_PASTE.EscapeCodeToReset())
|
||||
}
|
||||
|
||||
func (self *Loop) Quit(exit_code int) {
|
||||
self.exit_code = exit_code
|
||||
self.keep_going = false
|
||||
|
||||
52
tools/tui/readline/actions.go
Normal file
52
tools/tui/readline/actions.go
Normal file
@ -0,0 +1,52 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package readline
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func (self *Readline) add_text(text string) {
|
||||
new_lines := make([]string, 0, len(self.lines)+4)
|
||||
new_lines = append(new_lines, self.lines[:self.cursor_line]...)
|
||||
var lines_after []string
|
||||
if len(self.lines) > self.cursor_line+1 {
|
||||
lines_after = self.lines[self.cursor_line+1:]
|
||||
}
|
||||
has_trailing_newline := strings.HasSuffix(text, "\n")
|
||||
|
||||
add_line_break := func() {
|
||||
new_lines = append(new_lines, "")
|
||||
self.cursor_pos_in_line = 0
|
||||
self.cursor_line += 1
|
||||
}
|
||||
cline := self.lines[self.cursor_line]
|
||||
before_first_line := cline[:self.cursor_pos_in_line]
|
||||
after_first_line := ""
|
||||
if self.cursor_pos_in_line < len(cline) {
|
||||
after_first_line = cline[self.cursor_pos_in_line:]
|
||||
}
|
||||
for i, line := range utils.Splitlines(text) {
|
||||
if i > 0 {
|
||||
add_line_break()
|
||||
new_lines = append(new_lines, line)
|
||||
self.cursor_pos_in_line = len(line)
|
||||
} else {
|
||||
line := before_first_line + line
|
||||
self.cursor_pos_in_line = len(line)
|
||||
line += after_first_line
|
||||
}
|
||||
}
|
||||
if has_trailing_newline {
|
||||
add_line_break()
|
||||
}
|
||||
if len(lines_after) > 0 {
|
||||
new_lines = append(new_lines, lines_after...)
|
||||
}
|
||||
self.lines = new_lines
|
||||
}
|
||||
@ -59,6 +59,7 @@ func New(loop *loop.Loop, r RlInit) *Readline {
|
||||
|
||||
func (self *Readline) Start() {
|
||||
self.loop.SetCursorShape(loop.BAR_CURSOR, true)
|
||||
self.loop.StartBracketedPaste()
|
||||
self.Redraw()
|
||||
}
|
||||
|
||||
@ -73,9 +74,19 @@ func (self *Readline) RedrawNonAtomic() {
|
||||
}
|
||||
|
||||
func (self *Readline) End() {
|
||||
self.loop.EndBracketedPaste()
|
||||
self.loop.QueueWriteString("\r\n")
|
||||
self.loop.SetCursorShape(loop.BLOCK_CURSOR, true)
|
||||
if self.mark_prompts {
|
||||
self.loop.QueueWriteString(PROMPT_MARK + "C" + ST)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Readline) OnKeyEvent(event *loop.KeyEvent) error {
|
||||
return self.handle_key_event(event)
|
||||
}
|
||||
|
||||
func (self *Readline) OnText(text string, from_key_event bool, in_bracketed_paste bool) error {
|
||||
self.add_text(text)
|
||||
return nil
|
||||
}
|
||||
|
||||
18
tools/tui/readline/keys.go
Normal file
18
tools/tui/readline/keys.go
Normal file
@ -0,0 +1,18 @@
|
||||
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package readline
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"kitty/tools/tui/loop"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
func (self *Readline) handle_key_event(event *loop.KeyEvent) error {
|
||||
if event.Text != "" {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user