Avoid Querying screen size on every resize

This commit is contained in:
Kovid Goyal
2022-10-16 12:20:27 +05:30
parent 595a78c956
commit 260249491d
3 changed files with 20 additions and 11 deletions

View File

@@ -47,10 +47,7 @@ func shell_loop(rl *readline.Readline, 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.OnResize = rl.OnResize
lp.OnKeyEvent = func(event *loop.KeyEvent) error {
err := rl.OnKeyEvent(event)

View File

@@ -59,7 +59,8 @@ type Readline struct {
history *History
// The number of lines after the initial line on the screen
cursor_y int
cursor_y int
screen_width int
// Input lines
lines []string
// The cursor position in the text
@@ -172,3 +173,12 @@ func (self *Readline) AllText() string {
func (self *Readline) CursorAtEndOfLine() bool {
return self.cursor.X >= len(self.lines[self.cursor.Y])
}
func (self *Readline) OnResize(old_size loop.ScreenSize, new_size loop.ScreenSize) error {
self.screen_width = int(new_size.CellWidth)
if self.screen_width < 1 {
self.screen_width = 1
}
self.Redraw()
return nil
}

View File

@@ -29,7 +29,7 @@ func (self *Readline) move_cursor_to_text_position(pos, screen_width int) int {
return num_of_lines
}
func (self *Readline) current_screen_size() (int, int) {
func (self *Readline) update_current_screen_size() {
screen_size, err := self.loop.ScreenSize()
if err != nil {
screen_size.WidthCells = 80
@@ -41,12 +41,14 @@ func (self *Readline) current_screen_size() (int, int) {
if screen_size.HeightCells < 1 {
screen_size.HeightCells = 1
}
return int(screen_size.WidthCells), int(screen_size.HeightCells)
self.screen_width = int(screen_size.WidthCells)
}
func (self *Readline) redraw() {
screen_width, _ := self.current_screen_size()
if screen_width < 4 {
if self.screen_width == 0 {
self.update_current_screen_size()
}
if self.screen_width < 4 {
return
}
if self.cursor_y > 0 {
@@ -66,7 +68,7 @@ func (self *Readline) redraw() {
if i == self.cursor.Y {
line_with_cursor = y
}
y += self.write_line_with_prompt(line, p, screen_width)
y += self.write_line_with_prompt(line, p, self.screen_width)
}
self.loop.MoveCursorVertically(-y + line_with_cursor)
line := self.lines[self.cursor.Y]
@@ -74,6 +76,6 @@ func (self *Readline) redraw() {
if self.cursor.Y > 0 {
plen = self.continuation_prompt_len
}
line_with_cursor += self.move_cursor_to_text_position(plen+wcswidth.Stringwidth(line[:self.cursor.X]), screen_width)
line_with_cursor += self.move_cursor_to_text_position(plen+wcswidth.Stringwidth(line[:self.cursor.X]), self.screen_width)
self.cursor_y = line_with_cursor
}