Use RenderLines.InRectangle

This commit is contained in:
Kovid Goyal
2024-05-04 14:15:44 +05:30
parent 7f965eba5f
commit 4998fe66b9
3 changed files with 23 additions and 19 deletions

View File

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

View File

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