"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Mark Jamsek <mark@jamsek.com>
Subject:
tog: add horizontal scroll to tree view (+ minor refactor)
To:
Game of Trees <gameoftrees@openbsd.org>
Date:
Wed, 1 Feb 2023 22:40:44 +1100

Download raw body.

Thread
As per discussion with op in the previous thread adding hscroll to ref
view, add hscroll to the tree view too. With this, all views now support
sideways scrolling :)

While here, remove some copypasta with a new subroutine for hscroll
input.

The new routine introduces a small change in behaviour for the blame,
diff, and help views such that the rightmost position now centers the
end of the longest line on the screen. Previously, for these three views
only, we would continue scrolling till the end of the longest line
finished about one-third into the view.

I don't think it matters enough to add another int parameter to the new
horizontal_scroll_input() routine just to make the rightmost position
dependent on the view. In fact, it's probably better to keep it
consistent across all views. But I'm happy either way if you think we
should.

tog(1) has been updated by moving the relevant keymaps to global space;
runtime help was already like this.

I haven't committed the ref view hscroll change yet but this diff
applies on top of that one.

-----------------------------------------------
commit e1975a4a12a87b72a7f9187bff622b73308fcb7b
from: Mark Jamsek <mark@jamsek.dev>
date: Wed Feb  1 11:30:27 2023 UTC
 
 tog: add horizontal scroll to the tree view
 
 With this, tog supports hscroll in all views so move corresponding keymap docs
 in tog(1) to global space (this was already the case in tog runtime help).
 While here, remove some copypasta with a new subroutine to handle hscroll
 input.
 
 M  tog/tog.1  |  12+  36-
 M  tog/tog.c  |  59+  69-

2 files changed, 71 insertions(+), 105 deletions(-)

diff 58b4cc4632fdcc6cd3a62a7a0678189680e7b78d e1975a4a12a87b72a7f9187bff622b73308fcb7b
commit - 58b4cc4632fdcc6cd3a62a7a0678189680e7b78d
commit + e1975a4a12a87b72a7f9187bff622b73308fcb7b
blob - a752780caccc9b9007daf5bc95cf05bde851090c
blob + 667bfe283076f37b6cb5690da6b2a98d9aae516a
--- tog/tog.1
+++ tog/tog.1
@@ -112,6 +112,18 @@ Go to line N in the view (default: first line).
 Go to line N in the view (default: last line).
 .It Cm g
 Go to line N in the view (default: first line).
+.It Cm Right-arrow, l
+Scroll view to the right N increments (default: 1).
+.br
+Output moves left on the screen.
+.It Cm Left-arrow, h
+Scroll view to the left N increments (default: 1).
+.br
+Output moves right on the screen.
+.It Cm $
+Scroll view to the rightmost position.
+.It Cm 0
+Scroll view left to the start of the line.
 .El
 .Pp
 The commands for
@@ -145,18 +157,6 @@ Move the selection cursor up N lines (default: 1).
 Move the selection cursor down N lines (default: 1).
 .It Cm Up-arrow, k, <, Comma, Ctrl-p
 Move the selection cursor up N lines (default: 1).
-.It Cm Right-arrow, l
-Scroll log message field to the right N increments (default: 1).
-.br
-Log message moves left on the screen.
-.It Cm Left-arrow, h
-Scroll log message field to the left N increments (default: 1).
-.br
-Log message moves right on the screen.
-.It Cm $
-Scroll log message field to the rightmost position.
-.It Cm 0
-Scroll log message field to the leftmost position.
 .It Cm Page-down, Space, Ctrl+f, f
 Move the selection cursor down N pages (default: 1).
 .It Cm Page-up, Ctrl+b, b
@@ -313,18 +313,6 @@ Scroll up N lines (default: 1).
 Scroll down N lines (default: 1).
 .It Cm Up-arrow, k, Ctrl-p
 Scroll up N lines (default: 1).
-.It Cm Right-arrow, l
-Scroll view to the right N increments (default: 1).
-.br
-Diff output moves left on the screen.
-.It Cm Left-arrow, h
-Scroll view to the left N increments (default: 1).
-.br
-Diff output moves right on the screen.
-.It Cm $
-Scroll view to the rightmost position.
-.It Cm 0
-Scroll view left to the start of the line.
 .It Cm Page-down, Space, Ctrl+f, f
 Scroll down N pages (default: 1).
 .It Cm Page-up, Ctrl+b, b
@@ -430,18 +418,6 @@ Move the selection cursor up N pages (default: 1).
 Move the selection cursor down N pages (default: 1).
 .It Cm Up-arrow, k, Ctrl-p
 Move the selection cursor up N pages (default: 1).
-.It Cm Right-arrow, l
-Scroll view to the right N increments (default: 1).
-.br
-File output moves left on the screen.
-.It Cm Left-arrow, h
-Scroll view to the left N increments (default: 1).
-.br
-File output moves right on the screen.
-.It Cm $
-Scroll view to the rightmost position.
-.It Cm 0
-Scroll view left to the start of the line.
 .It Cm Page-down, Space, Ctrl+f, f
 Move the selection cursor down N pages (default: 1).
 .It Cm Page-up, Ctrl+b, b
blob - 73869bfa87df9af7a68e8ec4c44690f950d74ccb
blob + a6351da680ffacc2f47bf1a42b494d77af0b4a3a
--- tog/tog.c
+++ tog/tog.c
@@ -3692,6 +3692,36 @@ static const struct got_error *
 
 }
 
+static void
+horizontal_scroll_input(struct tog_view *view, int ch)
+{
+
+	switch (ch) {
+	case KEY_LEFT:
+	case 'h':
+		view->x -= MIN(view->x, 2);
+		if (view->x <= 0)
+			view->count = 0;
+		break;
+	case KEY_RIGHT:
+	case 'l':
+		if (view->x + view->ncols / 2 < view->maxx)
+			view->x += 2;
+		else
+			view->count = 0;
+		break;
+	case '0':
+		view->x = 0;
+		break;
+	case '$':
+		view->x = MAX(view->maxx - view->ncols / 2, 0);
+		view->count = 0;
+		break;
+	default:
+		break;
+	}
+}
+
 static const struct got_error *
 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
 {
@@ -3725,24 +3755,12 @@ input_log_view(struct tog_view **new_view, struct tog_
 		s->quit = 1;
 		break;
 	case '0':
-		view->x = 0;
-		break;
 	case '$':
-		view->x = MAX(view->maxx - view->ncols / 2, 0);
-		view->count = 0;
-		break;
 	case KEY_RIGHT:
 	case 'l':
-		if (view->x + view->ncols / 2 < view->maxx)
-			view->x += 2;  /* move two columns right */
-		else
-			view->count = 0;
-		break;
 	case KEY_LEFT:
 	case 'h':
-		view->x -= MIN(view->x, 2);  /* move two columns back */
-		if (view->x <= 0)
-			view->count = 0;
+		horizontal_scroll_input(view, ch);
 		break;
 	case 'k':
 	case KEY_UP:
@@ -5299,24 +5317,12 @@ input_diff_view(struct tog_view **new_view, struct tog
 
 	switch (ch) {
 	case '0':
-		view->x = 0;
-		break;
 	case '$':
-		view->x = MAX(view->maxx - view->ncols / 3, 0);
-		view->count = 0;
-		break;
 	case KEY_RIGHT:
 	case 'l':
-		if (view->x + view->ncols / 3 < view->maxx)
-			view->x += 2;  /* move two columns right */
-		else
-			view->count = 0;
-		break;
 	case KEY_LEFT:
 	case 'h':
-		view->x -= MIN(view->x, 2);  /* move two columns back */
-		if (view->x <= 0)
-			view->count = 0;
+		horizontal_scroll_input(view, ch);
 		break;
 	case 'a':
 	case 'w':
@@ -6324,24 +6330,12 @@ input_blame_view(struct tog_view **new_view, struct to
 
 	switch (ch) {
 	case '0':
-		view->x = 0;
-		break;
 	case '$':
-		view->x = MAX(view->maxx - view->ncols / 3, 0);
-		view->count = 0;
-		break;
 	case KEY_RIGHT:
 	case 'l':
-		if (view->x + view->ncols / 3 < view->maxx)
-			view->x += 2;  /* move two columns right */
-		else
-			view->count = 0;
-		break;
 	case KEY_LEFT:
 	case 'h':
-		view->x -= MIN(view->x, 2);  /* move two columns back */
-		if (view->x <= 0)
-			view->count = 0;
+		horizontal_scroll_input(view, ch);
 		break;
 	case 'q':
 		s->done = 1;
@@ -6779,7 +6773,7 @@ draw_tree_entries(struct tog_view *view, const char *p
 	wchar_t *wline;
 	char *index = NULL;
 	struct tog_color *tc;
-	int width, n, nentries, i = 1;
+	int width, n, nentries, scrollx, i = 1;
 	int limit = view->nlines;
 
 	s->ndisplayed = 0;
@@ -6856,6 +6850,7 @@ draw_tree_entries(struct tog_view *view, const char *p
 		te = s->first_displayed_entry;
 	}
 
+	view->maxx = 0;
 	for (i = got_tree_entry_get_index(te); i < nentries; i++) {
 		char *line = NULL, *id_str = NULL, *link_target = NULL;
 		const char *modestr = "";
@@ -6902,12 +6897,23 @@ draw_tree_entries(struct tog_view *view, const char *p
 		}
 		free(id_str);
 		free(link_target);
-		err = format_line(&wline, &width, NULL, line, 0, view->ncols,
-		    0, 0);
+
+		/* use full line width to determine view->maxx */
+		err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0, 0);
 		if (err) {
 			free(line);
 			break;
 		}
+		view->maxx = MAX(view->maxx, width);
+		free(wline);
+		wline = NULL;
+
+		err = format_line(&wline, &width, &scrollx, line, view->x,
+		    view->ncols, 0, 0);
+		if (err) {
+			free(line);
+			break;
+		}
 		if (n == s->selected) {
 			if (view->focussed)
 				wstandout(view->window);
@@ -6917,7 +6923,7 @@ draw_tree_entries(struct tog_view *view, const char *p
 		if (tc)
 			wattr_on(view->window,
 			    COLOR_PAIR(tc->colorpair), NULL);
-		waddwstr(view->window, wline);
+		waddwstr(view->window, &wline[scrollx]);
 		if (tc)
 			wattr_off(view->window,
 			    COLOR_PAIR(tc->colorpair), NULL);
@@ -7391,6 +7397,14 @@ input_tree_view(struct tog_view **new_view, struct tog
 		return tree_goto_line(view, nscroll);
 
 	switch (ch) {
+	case '0':
+	case '$':
+	case KEY_RIGHT:
+	case 'l':
+	case KEY_LEFT:
+	case 'h':
+		horizontal_scroll_input(view, ch);
+		break;
 	case 'i':
 		s->show_ids = !s->show_ids;
 		view->count = 0;
@@ -8302,24 +8316,12 @@ input_ref_view(struct tog_view **new_view, struct tog_
 
 	switch (ch) {
 	case '0':
-		view->x = 0;
-		break;
 	case '$':
-		view->x = MAX(view->maxx - view->ncols / 2, 0);
-		view->count = 0;
-		break;
 	case KEY_RIGHT:
 	case 'l':
-		if (view->x + view->ncols / 2 < view->maxx)
-			view->x += 2;
-		else
-			view->count = 0;
-		break;
 	case KEY_LEFT:
 	case 'h':
-		view->x -= MIN(view->x, 2);
-		if (view->x <= 0)
-			view->count = 0;
+		horizontal_scroll_input(view, ch);
 		break;
 	case 'i':
 		s->show_ids = !s->show_ids;
@@ -8933,24 +8935,12 @@ input_help_view(struct tog_view **new_view, struct tog
 
 	switch (ch) {
 	case '0':
-		view->x = 0;
-		break;
 	case '$':
-		view->x = MAX(view->maxx - view->ncols / 3, 0);
-		view->count = 0;
-		break;
 	case KEY_RIGHT:
 	case 'l':
-		if (view->x + view->ncols / 3 < view->maxx)
-			view->x += 2;
-		else
-			view->count = 0;
-		break;
 	case KEY_LEFT:
 	case 'h':
-		view->x -= MIN(view->x, 2);
-		if (view->x <= 0)
-			view->count = 0;
+		horizontal_scroll_input(view, ch);
 		break;
 	case 'g':
 	case KEY_HOME:


-- 
Mark Jamsek <fnc.bsdbox.org>
GPG: F2FF 13DE 6A06 C471 CA80  E6E2 2930 DC66 86EE CF68