From 260249491d2301e3e2bf9d2d0935ccb23c41d491 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 16 Oct 2022 12:20:27 +0530 Subject: [PATCH] Avoid Querying screen size on every resize --- tools/cmd/at/shell.go | 5 +---- tools/tui/readline/api.go | 12 +++++++++++- tools/tui/readline/draw.go | 14 ++++++++------ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tools/cmd/at/shell.go b/tools/cmd/at/shell.go index 1c5258d39..7ab1e807e 100644 --- a/tools/cmd/at/shell.go +++ b/tools/cmd/at/shell.go @@ -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) diff --git a/tools/tui/readline/api.go b/tools/tui/readline/api.go index f2e900493..a8ab8d725 100644 --- a/tools/tui/readline/api.go +++ b/tools/tui/readline/api.go @@ -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 +} diff --git a/tools/tui/readline/draw.go b/tools/tui/readline/draw.go index 6f77b0e45..f407536c5 100644 --- a/tools/tui/readline/draw.go +++ b/tools/tui/readline/draw.go @@ -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 }