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

From:
Mark Jamsek <mark@jamsek.com>
Subject:
tog: key map to switch split mode
To:
gameoftrees@openbsd.org
Date:
Sun, 3 Jul 2022 23:05:32 +1000

Download raw body.

Thread
This introduces the 'S' global key map to switch the current splitscreen
mode.

If in a vsplit, it will switch to a hsplit.
If in a hsplit, it will switch to a vsplit IFF COLUMNS > 119 else the
view will remain unchanged (to honour existing vsplit behaviour).

diff /home/mark/src/git/got-current
commit - 41e8d27de3dea9d283199eb2e1a7f6a98ee9286f
path + /home/mark/src/git/got-current
blob - 30d0b7d88189f5969dd01deb66e8f9196bac64d3
file + tog/tog.1
--- tog/tog.1
+++ tog/tog.1
@@ -80,6 +80,12 @@ Toggle fullscreen mode for a split-screen view.
 .Nm
 will automatically use split-screen views if the size of the terminal
 window is sufficiently large.
+.It Cm S
+When in a split-screen view,
+.Nm
+will switch to the alternate split mode.
+If the current view is in a horizontal split and the terminal window is
+less than 120 columns wide, the view will remain unchanged.
 .El
 .Pp
 Global options must precede the command name, and are as follows:
@@ -582,7 +588,7 @@ work tree, use the repository path associated with thi
 .El
 .El
 .Sh ENVIRONMENT
-.Bl -tag -width TOG_DIFF_ALGORITHM
+.Bl -tag -width TOG_VIEW_SPLIT_MODE
 .It Ev TOG_DIFF_ALGORITHM
 Determines the default diff algorithm used by
 .Nm .
@@ -595,6 +601,18 @@ are
 and
 .Dq myers .
 If unset, the Myers diff algorithm will be used by default.
+.It Ev TOG_VIEW_SPLIT_MODE
+Determines the default split-screen mode used by
+.Nm .
+Valid values are
+.Dq h
+for horizontal
+and
+.Dq v
+for vertical.
+If unset, views will open in a vertical split-screen by default when the
+terminal window is sufficiently large.
+Splits can be manipulated in-session as documented above.
 .It Ev TOG_COLORS
 .Nm
 shows colorized output if this variable is set to a non-empty value.
blob - 95a351a042eb915c3180784da0a53b2c2d2abca1
file + tog/tog.c
--- tog/tog.c
+++ tog/tog.c
@@ -1004,7 +1004,69 @@ view_search_start(struct tog_view *view)
 	return NULL;
 }

+static void view_get_split(struct tog_view *, int *, int *);
+static const struct got_error *view_init_hsplit(struct tog_view *, int);
+
 /*
+ * If view is a parent or child view and is currently in a splitscreen, switch
+ * to the alternate split. If in a hsplit and LINES < 120, don't vsplit.
+ */
+static const struct got_error *
+switch_split(struct tog_view *view)
+{
+	const struct got_error	*err = NULL;
+	struct tog_view		*v = NULL;
+
+	if (view_is_parent_view(view))
+		v = view;
+	else if (view->parent)
+		v = view->parent;
+
+	if (!v->child || !view_is_splitscreen(v->child))
+		return NULL;
+	if (v->mode == TOG_VIEW_SPLIT_HRZN && v->cols < 120)
+		return NULL;
+
+	if (!v->mode || v->mode == TOG_VIEW_SPLIT_HRZN) {
+		v->child->nscrolled = LINES - v->child->nlines;
+		v->mode = TOG_VIEW_SPLIT_VERT;
+	} else if (v->mode == TOG_VIEW_SPLIT_VERT)
+		v->mode = TOG_VIEW_SPLIT_HRZN;
+
+	view_get_split(v, &v->child->begin_y, &v->child->begin_x);
+
+	if (v->mode == TOG_VIEW_SPLIT_HRZN) {
+		v->ncols = COLS;
+		v->child->ncols = COLS;
+
+		err = view_init_hsplit(v, v->child->begin_y);
+		if (err)
+			return err;
+	}
+	v->child->mode = v->mode;
+	v->child->nlines = v->lines - v->child->begin_y;
+	v->focus_child = 1;
+
+	err = view_fullscreen(v);
+	if (err)
+		return err;
+	err = view_splitscreen(v->child);
+	if (err)
+		return err;
+
+	if (v->mode == TOG_VIEW_SPLIT_HRZN)
+		err = offset_selection_down(v->child);
+	else if (v->mode == TOG_VIEW_SPLIT_VERT) {
+		if (v->type == TOG_VIEW_LOG)
+			err = request_log_commits(v);
+		else if (v->child->type == TOG_VIEW_LOG)
+			err = request_log_commits(v->child);
+	}
+
+	return err;
+}
+
+/*
  * Compute view->count from numeric input. Assign total to view->count and
  * return first non-numeric key entered.
  */
@@ -1209,6 +1271,9 @@ view_input(struct tog_view **new, int *done, struct to
 		if (!err)
 			err = offset_selection_down(view);
 		break;
+	case 'S':
+		err = switch_split(view);
+		break;
 	case KEY_RESIZE:
 		break;
 	case '/':
@@ -1274,9 +1339,16 @@ view_loop(struct tog_view *view)
 	const struct got_error *err = NULL;
 	struct tog_view_list_head views;
 	struct tog_view *new_view;
+	char *mode;
 	int fast_refresh = 10;
 	int done = 0, errcode;

+	mode = getenv("TOG_VIEW_SPLIT_MODE");
+	if (!mode || !(*mode == 'h' || *mode == 'H'))
+		view->mode = TOG_VIEW_SPLIT_VERT;
+	else
+		view->mode = TOG_VIEW_SPLIT_HRZN;
+
 	errcode = pthread_mutex_lock(&tog_mutex);
 	if (errcode)
 		return got_error_set_errno(errcode, "pthread_mutex_lock");
@@ -2881,30 +2953,16 @@ log_move_cursor_down(struct tog_view *view, int page)
 	return NULL;
 }

-/*
- * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
- *    TOG_VIEW_SPLIT_VERT    vertical split if COLS > 119 (default)
- *    TOG_VIEW_SPLIT_HRZN    horizontal split
- * Assign start column and line of the new split to *x and *y, respectively,
- * and assign view mode to view->mode.
- */
 static void
 view_get_split(struct tog_view *view, int *y, int *x)
 {
-	char *mode;
-
 	*x = 0;
 	*y = 0;

-	mode = getenv("TOG_VIEW_SPLIT_MODE");
-
-	if (!mode || mode[0] != 'h') {
-		view->mode = TOG_VIEW_SPLIT_VERT;
+	if (view->mode == TOG_VIEW_SPLIT_VERT)
 		*x = view_split_begin_x(view->begin_x);
-	} else if (mode && mode[0] == 'h') {
-		view->mode = TOG_VIEW_SPLIT_HRZN;
+	else if (view->mode == TOG_VIEW_SPLIT_HRZN)
 		*y = view_split_begin_y(view->lines);
-	}
 }

 /* Split view horizontally at y and offset view->state->selected line. */
@@ -2914,6 +2972,7 @@ view_init_hsplit(struct tog_view *view, int y)
 	const struct got_error *err = NULL;

 	view->nlines = y;
+	view->ncols = COLS;
 	err = view_resize(view);
 	if (err)
 		return err;
@@ -7710,7 +7769,7 @@ offset_selection_down(struct tog_view *view)
 	case TOG_VIEW_LOG: {
 		struct tog_log_view_state *s = &view->state.log;
 		scrolld = &log_scroll_down;
-		header = 3;
+		header = view_is_parent_view(view) ? 3 : 2;
 		selected = &s->selected;
 		break;
 	}

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