From 8867818dfe53405ac66aa8976b92dd7e7bec1dd7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 28 Mar 2023 11:55:08 +0530 Subject: [PATCH] DRYer --- kittens/diff/mouse.go | 15 +-------------- tools/tui/mouse.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/kittens/diff/mouse.go b/kittens/diff/mouse.go index add52aa8e..54853d067 100644 --- a/kittens/diff/mouse.go +++ b/kittens/diff/mouse.go @@ -93,19 +93,6 @@ func (self *Handler) finish_mouse_selection(ev *loop.MouseEvent) { self.mouse_selection.Finish() } -func format_part_of_line(sgr string, start_x, end_x, y int) string { - // DECCARA used to set formatting in specified region using zero based indexing - return fmt.Sprintf("\x1b[%d;%d;%d;%d;%s$r", y+1, start_x+1, y+1, end_x+1, sgr) -} - func (self *Handler) add_mouse_selection_to_line(line string, line_pos ScrollPos, y int) string { - ms := &self.mouse_selection - if ms.IsEmpty() { - return line - } - x_start, x_end := self.mouse_selection.LineBounds(&line_pos) - if x_start > -1 { - line += format_part_of_line(selection_sgr, x_start, x_end, y) - } - return line + return line + self.mouse_selection.LineFormatSuffix(&line_pos, selection_sgr, y) } diff --git a/tools/tui/mouse.go b/tools/tui/mouse.go index 848e71589..8ffeddc9a 100644 --- a/tools/tui/mouse.go +++ b/tools/tui/mouse.go @@ -91,3 +91,16 @@ func (ms *MouseSelection) LineBounds(line_pos LinePos) (start_x, end_x int) { } return -1, -1 } + +func FormatPartOfLine(sgr string, start_x, end_x, y int) string { + // DECCARA used to set formatting in specified region using zero based indexing + return fmt.Sprintf("\x1b[%d;%d;%d;%d;%s$r", y+1, start_x+1, y+1, end_x+1, sgr) +} + +func (ms *MouseSelection) LineFormatSuffix(line_pos LinePos, sgr string, y int) string { + s, e := ms.LineBounds(line_pos) + if s > -1 { + return FormatPartOfLine(sgr, s, e, y) + } + return "" +}