From e7ed5716a9aefefd67a07fc79bea2a66d5cc4f33 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 4 May 2024 14:15:44 +0530 Subject: [PATCH] Use RenderLines.InRectangle --- tools/cmd/list_fonts/ui.go | 23 ++++++++++++----------- tools/tui/mouse.go | 3 +++ tools/tui/render_lines.go | 16 ++++++++-------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/tools/cmd/list_fonts/ui.go b/tools/cmd/list_fonts/ui.go index 8609d01d7..5c014ccf0 100644 --- a/tools/cmd/list_fonts/ui.go +++ b/tools/cmd/list_fonts/ui.go @@ -32,6 +32,7 @@ type handler struct { err_mutex sync.Mutex err_in_worker_thread error mouse_state tui.MouseState + render_lines tui.RenderLines // Listing rl *readline.Readline @@ -127,24 +128,24 @@ func (h *handler) draw_listing_screen() (err error) { num_rows := max(0, int(sz.HeightCells)-1) mw := h.family_list.max_width + 1 green_fg, _, _ := strings.Cut(h.lp.SprintStyled("fg=green", "|"), "|") + lines := make([]string, 0, num_rows) for _, l := range h.family_list.Lines(num_rows) { line := l.text if l.is_current { line = strings.ReplaceAll(line, MARK_AFTER, green_fg) - h.lp.PrintStyled("fg=green", ">") - h.lp.PrintStyled("fg=green bold", line) + line = h.lp.SprintStyled("fg=green", ">") + h.lp.SprintStyled("fg=green bold", line) } else { - h.lp.PrintStyled("fg=green", " ") - h.lp.QueueWriteString(line) + line = " " + line } - h.lp.MoveCursorHorizontally(mw - l.width) - h.lp.Println(SEPARATOR) - num_rows-- - } - for ; num_rows > 0; num_rows-- { - h.lp.MoveCursorHorizontally(mw + 1) - h.lp.Println(SEPARATOR) + lines = append(lines, line) } + _, _, str := h.render_lines.InRectangle(lines, 0, 0, 0, num_rows, &h.mouse_state) + h.lp.QueueWriteString(str) + seps := strings.Repeat(SEPARATOR, num_rows) + seps = strings.TrimSpace(seps) + _, _, str = h.render_lines.InRectangle(strings.Split(seps, ""), mw+1, 0, 0, num_rows, &h.mouse_state) + h.lp.QueueWriteString(str) + if h.family_list.Len() > 0 { if err = h.draw_family_summary(mw+3, sz); err != nil { return err diff --git a/tools/tui/mouse.go b/tools/tui/mouse.go index 58ec22ca1..6d1d22f19 100644 --- a/tools/tui/mouse.go +++ b/tools/tui/mouse.go @@ -235,6 +235,9 @@ type MouseState struct { func (m *MouseState) AddCellRegion(id string, start_x, start_y, end_x, end_y int, on_click ...func(id string) error) *CellRegion { cr := CellRegion{TopLeft: struct{ X, Y int }{start_x, start_y}, BottomRight: struct{ X, Y int }{end_x, end_y}, Id: id, OnClick: on_click} m.regions = append(m.regions, &cr) + if m.region_id_map == nil { + m.region_id_map = make(map[string][]*CellRegion) + } m.region_id_map[id] = append(m.region_id_map[id], &cr) return &cr } diff --git a/tools/tui/render_lines.go b/tools/tui/render_lines.go index 4026b9dc7..839ede7d2 100644 --- a/tools/tui/render_lines.go +++ b/tools/tui/render_lines.go @@ -33,10 +33,10 @@ var hyperlink_pat = sync.OnceValue(func() *regexp.Regexp { // MouseState. func (r RenderLines) InRectangle( lines []string, start_x, start_y, width, height int, mouse_state *MouseState, -) (all_rendered bool, final_y int, ans string) { +) (all_rendered bool, y_after_last_line int, ans string) { end_y := start_y + height - 1 if end_y < start_y { - return len(lines) == 0, start_y, "" + return len(lines) == 0, start_y + 1, "" } x, y := start_x, start_y buf := strings.Builder{} @@ -100,18 +100,18 @@ func (r RenderLines) InRectangle( all_rendered = true for _, line := range lines { - lines := []string{line} + wrapped_lines := []string{line} if width > 0 { - lines = style.WrapTextAsLines(line, width, r.WrapOptions) + wrapped_lines = style.WrapTextAsLines(line, width, r.WrapOptions) } - for _, line := range lines { + for _, line := range wrapped_lines { + move_cursor(start_x, y) + add_line(line) + y += 1 if y > end_y { all_rendered = false goto end } - move_cursor(start_x, y) - add_line(line) - y += 1 } } end: