Annotation of src/usr.bin/tmux/cmd-display-panes.c, Revision 1.41
1.41 ! nicm 1: /* $OpenBSD: cmd-display-panes.c,v 1.40 2021/08/13 18:54:54 nicm Exp $ */
1.1 nicm 2:
3: /*
1.12 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
1.13 nicm 21: #include <stdlib.h>
1.24 nicm 22: #include <string.h>
1.13 nicm 23:
1.1 nicm 24: #include "tmux.h"
25:
26: /*
27: * Display panes on a client.
28: */
29:
1.16 nicm 30: static enum cmd_retval cmd_display_panes_exec(struct cmd *,
31: struct cmdq_item *);
1.13 nicm 32:
1.1 nicm 33: const struct cmd_entry cmd_display_panes_entry = {
1.10 nicm 34: .name = "display-panes",
35: .alias = "displayp",
36:
1.38 nicm 37: .args = { "bd:Nt:", 0, 1 },
38: .usage = "[-bN] [-d duration] " CMD_TARGET_CLIENT_USAGE " [template]",
1.10 nicm 39:
1.34 nicm 40: .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG,
1.10 nicm 41: .exec = cmd_display_panes_exec
1.1 nicm 42: };
43:
1.24 nicm 44: struct cmd_display_panes_data {
45: struct cmdq_item *item;
46: char *command;
47: };
48:
49: static void
50: cmd_display_panes_draw_pane(struct screen_redraw_ctx *ctx,
51: struct window_pane *wp)
1.1 nicm 52: {
1.24 nicm 53: struct client *c = ctx->c;
54: struct tty *tty = &c->tty;
55: struct session *s = c->session;
56: struct options *oo = s->options;
57: struct window *w = wp->window;
1.36 nicm 58: struct grid_cell fgc, bgc;
59: u_int pane, idx, px, py, i, j, xoff, yoff, sx, sy;
1.24 nicm 60: int colour, active_colour;
1.36 nicm 61: char buf[16], lbuf[16], rbuf[16], *ptr;
62: size_t len, llen, rlen;
1.24 nicm 63:
64: if (wp->xoff + wp->sx <= ctx->ox ||
65: wp->xoff >= ctx->ox + ctx->sx ||
66: wp->yoff + wp->sy <= ctx->oy ||
67: wp->yoff >= ctx->oy + ctx->sy)
68: return;
69:
70: if (wp->xoff >= ctx->ox && wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
71: /* All visible. */
72: xoff = wp->xoff - ctx->ox;
73: sx = wp->sx;
74: } else if (wp->xoff < ctx->ox &&
75: wp->xoff + wp->sx > ctx->ox + ctx->sx) {
76: /* Both left and right not visible. */
77: xoff = 0;
78: sx = ctx->sx;
79: } else if (wp->xoff < ctx->ox) {
80: /* Left not visible. */
81: xoff = 0;
82: sx = wp->sx - (ctx->ox - wp->xoff);
83: } else {
84: /* Right not visible. */
85: xoff = wp->xoff - ctx->ox;
86: sx = wp->sx - xoff;
87: }
88: if (wp->yoff >= ctx->oy && wp->yoff + wp->sy <= ctx->oy + ctx->sy) {
89: /* All visible. */
90: yoff = wp->yoff - ctx->oy;
91: sy = wp->sy;
92: } else if (wp->yoff < ctx->oy &&
93: wp->yoff + wp->sy > ctx->oy + ctx->sy) {
94: /* Both top and bottom not visible. */
95: yoff = 0;
96: sy = ctx->sy;
97: } else if (wp->yoff < ctx->oy) {
98: /* Top not visible. */
99: yoff = 0;
100: sy = wp->sy - (ctx->oy - wp->yoff);
101: } else {
102: /* Bottom not visible. */
103: yoff = wp->yoff - ctx->oy;
104: sy = wp->sy - yoff;
105: }
1.19 nicm 106:
1.24 nicm 107: if (ctx->statustop)
108: yoff += ctx->statuslines;
109: px = sx / 2;
110: py = sy / 2;
111:
1.36 nicm 112: if (window_pane_index(wp, &pane) != 0)
1.24 nicm 113: fatalx("index not found");
1.36 nicm 114: len = xsnprintf(buf, sizeof buf, "%u", pane);
1.24 nicm 115:
116: if (sx < len)
117: return;
118: colour = options_get_number(oo, "display-panes-colour");
119: active_colour = options_get_number(oo, "display-panes-active-colour");
120:
1.36 nicm 121: memcpy(&fgc, &grid_default_cell, sizeof fgc);
122: memcpy(&bgc, &grid_default_cell, sizeof bgc);
123: if (w->active == wp) {
124: fgc.fg = active_colour;
125: bgc.bg = active_colour;
126: } else {
127: fgc.fg = colour;
128: bgc.bg = colour;
129: }
130:
131: rlen = xsnprintf(rbuf, sizeof rbuf, "%ux%u", wp->sx, wp->sy);
132: if (pane > 9 && pane < 35)
133: llen = xsnprintf(lbuf, sizeof lbuf, "%c", 'a' + (pane - 10));
134: else
135: llen = 0;
136:
1.24 nicm 137: if (sx < len * 6 || sy < 5) {
1.36 nicm 138: tty_attributes(tty, &fgc, &grid_default_cell, NULL);
139: if (sx >= len + llen + 1) {
140: len += llen + 1;
141: tty_cursor(tty, xoff + px - len / 2, yoff + py);
142: tty_putn(tty, buf, len, len);
143: tty_putn(tty, " ", 1, 1);
144: tty_putn(tty, lbuf, llen, llen);
145: } else {
146: tty_cursor(tty, xoff + px - len / 2, yoff + py);
147: tty_putn(tty, buf, len, len);
148: }
149: goto out;
1.24 nicm 150: }
1.13 nicm 151:
1.24 nicm 152: px -= len * 3;
153: py -= 2;
1.13 nicm 154:
1.36 nicm 155: tty_attributes(tty, &bgc, &grid_default_cell, NULL);
1.24 nicm 156: for (ptr = buf; *ptr != '\0'; ptr++) {
157: if (*ptr < '0' || *ptr > '9')
158: continue;
159: idx = *ptr - '0';
160:
161: for (j = 0; j < 5; j++) {
162: for (i = px; i < px + 5; i++) {
163: tty_cursor(tty, xoff + i, yoff + py + j);
164: if (window_clock_table[idx][j][i - px])
165: tty_putc(tty, ' ');
166: }
167: }
168: px += 6;
169: }
170:
1.37 nicm 171: if (sy <= 6)
1.36 nicm 172: goto out;
173: tty_attributes(tty, &fgc, &grid_default_cell, NULL);
174: if (rlen != 0 && sx >= rlen) {
175: tty_cursor(tty, xoff + sx - rlen, yoff);
176: tty_putn(tty, rbuf, rlen, rlen);
177: }
178: if (llen != 0) {
179: tty_cursor(tty, xoff + sx / 2 + len * 3 - llen - 1,
180: yoff + py + 5);
181: tty_putn(tty, lbuf, llen, llen);
182: }
1.24 nicm 183:
1.36 nicm 184: out:
1.24 nicm 185: tty_cursor(tty, 0, 0);
186: }
187:
188: static void
1.40 nicm 189: cmd_display_panes_draw(struct client *c, __unused void *data,
190: struct screen_redraw_ctx *ctx)
1.24 nicm 191: {
192: struct window *w = c->session->curw->window;
193: struct window_pane *wp;
194:
195: log_debug("%s: %s @%u", __func__, c->name, w->id);
1.1 nicm 196:
1.24 nicm 197: TAILQ_FOREACH(wp, &w->panes, entry) {
198: if (window_pane_visible(wp))
199: cmd_display_panes_draw_pane(ctx, wp);
200: }
1.13 nicm 201: }
202:
203: static void
1.40 nicm 204: cmd_display_panes_free(__unused struct client *c, void *data)
1.24 nicm 205: {
1.40 nicm 206: struct cmd_display_panes_data *cdata = data;
1.24 nicm 207:
208: if (cdata->item != NULL)
1.28 nicm 209: cmdq_continue(cdata->item);
1.24 nicm 210: free(cdata->command);
211: free(cdata);
212: }
213:
214: static int
1.40 nicm 215: cmd_display_panes_key(struct client *c, void *data, struct key_event *event)
1.13 nicm 216: {
1.40 nicm 217: struct cmd_display_panes_data *cdata = data;
1.33 nicm 218: char *cmd, *expanded, *error;
1.24 nicm 219: struct window *w = c->session->curw->window;
220: struct window_pane *wp;
1.33 nicm 221: enum cmd_parse_status status;
1.36 nicm 222: u_int index;
223: key_code key;
1.13 nicm 224:
1.36 nicm 225: if (event->key >= '0' && event->key <= '9')
226: index = event->key - '0';
227: else if ((event->key & KEYC_MASK_MODIFIERS) == 0) {
228: key = (event->key & KEYC_MASK_KEY);
229: if (key >= 'a' && key <= 'z')
230: index = 10 + (key - 'a');
231: else
232: return (-1);
233: } else
1.29 nicm 234: return (-1);
1.24 nicm 235:
1.36 nicm 236: wp = window_pane_at_index(w, index);
1.15 nicm 237: if (wp == NULL)
1.24 nicm 238: return (1);
239: window_unzoom(w);
1.21 nicm 240:
1.15 nicm 241: xasprintf(&expanded, "%%%u", wp->id);
1.24 nicm 242: cmd = cmd_template_replace(cdata->command, expanded, 1);
1.15 nicm 243:
1.33 nicm 244: status = cmd_parse_and_append(cmd, NULL, c, NULL, &error);
245: if (status == CMD_PARSE_ERROR) {
246: cmdq_append(c, cmdq_get_error(error));
247: free(error);
1.23 nicm 248: }
1.15 nicm 249:
250: free(cmd);
251: free(expanded);
1.24 nicm 252: return (1);
253: }
254:
255: static enum cmd_retval
256: cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
257: {
1.31 nicm 258: struct args *args = cmd_get_args(self);
1.34 nicm 259: struct client *tc = cmdq_get_target_client(item);
260: struct session *s = tc->session;
1.24 nicm 261: u_int delay;
262: char *cause;
263: struct cmd_display_panes_data *cdata;
264:
1.34 nicm 265: if (tc->overlay_draw != NULL)
1.24 nicm 266: return (CMD_RETURN_NORMAL);
1.13 nicm 267:
1.24 nicm 268: if (args_has(args, 'd')) {
269: delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
270: if (cause != NULL) {
271: cmdq_error(item, "delay %s", cause);
272: free(cause);
273: return (CMD_RETURN_ERROR);
274: }
275: } else
276: delay = options_get_number(s->options, "display-panes-time");
277:
278: cdata = xmalloc(sizeof *cdata);
1.41 ! nicm 279: if (args_count(args))
! 280: cdata->command = xstrdup(args_string(args, 0));
1.24 nicm 281: else
282: cdata->command = xstrdup("select-pane -t '%%'");
283: if (args_has(args, 'b'))
284: cdata->item = NULL;
285: else
286: cdata->item = item;
1.21 nicm 287:
1.38 nicm 288: if (args_has(args, 'N')) {
289: server_client_set_overlay(tc, delay, NULL, NULL,
1.39 nicm 290: cmd_display_panes_draw, NULL, cmd_display_panes_free, NULL,
1.38 nicm 291: cdata);
292: } else {
293: server_client_set_overlay(tc, delay, NULL, NULL,
294: cmd_display_panes_draw, cmd_display_panes_key,
1.39 nicm 295: cmd_display_panes_free, NULL, cdata);
1.38 nicm 296: }
1.21 nicm 297:
1.24 nicm 298: if (args_has(args, 'b'))
299: return (CMD_RETURN_NORMAL);
300: return (CMD_RETURN_WAIT);
1.1 nicm 301: }