More work on mouse selection

This commit is contained in:
Kovid Goyal 2023-03-28 08:10:29 +05:30
parent 40ca46d8d8
commit 45c1e36de9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 40 additions and 6 deletions

View File

@ -98,6 +98,7 @@ func (self *Handler) update_mouse_selection(ev *loop.MouseEvent) {
ms.end.x = ev.Cell.X
ms.end.x = utils.Max(ms.min_x, utils.Min(ms.end.x, ms.max_x))
ms.end.line = pos
self.draw_screen()
}
func (self *Handler) clear_mouse_selection() {
@ -113,9 +114,34 @@ func (self *Handler) finish_mouse_selection(ev *loop.MouseEvent) {
ms.is_active = false
}
func (self *Handler) add_mouse_selection_to_line(line string, line_pos ScrollPos) {
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.is_active {
return
return line
}
a, b := ms.start.line, ms.end.line
ax, bx := ms.start.x, ms.end.x
if b.Less(a) {
a, b = b, a
ax, bx = bx, ax
}
if a.Less(line_pos) {
if line_pos.Less(b) {
line += format_part_of_line(selection_sgr, 0, ms.max_x, y)
} else if b == line_pos {
line += format_part_of_line(selection_sgr, 0, bx, y)
}
} else if a == line_pos {
if line_pos.Less(b) {
line += format_part_of_line(selection_sgr, ax, ms.max_x, y)
} else if b == line_pos {
line += format_part_of_line(selection_sgr, ax, bx, y)
}
}
return line
}

View File

@ -82,16 +82,17 @@ func place_in(text string, sz int) string {
return fill_in(fit_in(text, sz), sz)
}
var title_format, text_format, margin_format, added_format, removed_format, added_margin_format, removed_margin_format, filler_format, margin_filler_format, hunk_margin_format, hunk_format, statusline_format, added_count_format, removed_count_format, message_format func(...any) string
var title_format, text_format, margin_format, added_format, removed_format, added_margin_format, removed_margin_format, filler_format, margin_filler_format, hunk_margin_format, hunk_format, statusline_format, added_count_format, removed_count_format, message_format, selection_format func(...any) string
var selection_sgr string
func create_formatters() {
ctx := style.Context{AllowEscapeCodes: true}
text_format = ctx.SprintFunc(fmt.Sprintf("bg=%s", conf.Background.AsRGBSharp()))
filler_format = ctx.SprintFunc(fmt.Sprintf("bg=%s", conf.Filler_bg.AsRGBSharp()))
if conf.Margin_filler_bg.IsSet {
margin_filler_format = ctx.SprintFunc(fmt.Sprintf("bg=%s", conf.Margin_filler_bg.Color.AsRGBSharp()))
margin_filler_format = ctx.SprintFunc("bg=" + conf.Margin_filler_bg.Color.AsRGBSharp())
} else {
margin_filler_format = ctx.SprintFunc(fmt.Sprintf("bg=%s", conf.Filler_bg.AsRGBSharp()))
margin_filler_format = ctx.SprintFunc("bg=" + conf.Filler_bg.AsRGBSharp())
}
added_format = ctx.SprintFunc(fmt.Sprintf("bg=%s", conf.Added_bg.AsRGBSharp()))
added_margin_format = ctx.SprintFunc(fmt.Sprintf("fg=%s bg=%s", conf.Margin_fg.AsRGBSharp(), conf.Added_margin_bg.AsRGBSharp()))
@ -105,6 +106,13 @@ func create_formatters() {
hunk_format = ctx.SprintFunc(fmt.Sprintf("fg=%s bg=%s", conf.Margin_fg.AsRGBSharp(), conf.Hunk_bg.AsRGBSharp()))
hunk_margin_format = ctx.SprintFunc(fmt.Sprintf("fg=%s bg=%s", conf.Margin_fg.AsRGBSharp(), conf.Hunk_margin_bg.AsRGBSharp()))
message_format = ctx.SprintFunc("bold")
if conf.Select_fg.IsSet {
selection_format = ctx.SprintFunc(fmt.Sprintf("fg=%s bg=%s", conf.Select_fg.Color.AsRGBSharp(), conf.Select_bg.AsRGBSharp()))
} else {
selection_format = ctx.SprintFunc("bg=" + conf.Select_bg.AsRGBSharp())
}
selection_sgr, _, _ = strings.Cut(selection_format("|"), "|")
selection_sgr = selection_sgr[2 : len(selection_sgr)-1]
}
func center_span(ltype string, offset, size int) *sgr.Span {

View File

@ -354,7 +354,7 @@ func (self *Handler) draw_screen() {
if self.current_search != nil {
sl = self.current_search.markup_line(sl, pos)
}
self.add_mouse_selection_to_line(sl, pos)
sl = self.add_mouse_selection_to_line(sl, pos, num_written)
lp.QueueWriteString(sl)
lp.MoveCursorVertically(1)
lp.QueueWriteString("\x1b[m\r")