[BACK]Return to cmd-display-panes.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-display-panes.c, Revision 1.39

1.39    ! nicm        1: /* $OpenBSD: cmd-display-panes.c,v 1.38 2020/11/26 09:19:10 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
                    189: cmd_display_panes_draw(struct client *c, struct screen_redraw_ctx *ctx)
                    190: {
                    191:        struct window           *w = c->session->curw->window;
                    192:        struct window_pane      *wp;
                    193:
                    194:        log_debug("%s: %s @%u", __func__, c->name, w->id);
1.1       nicm      195:
1.24      nicm      196:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    197:                if (window_pane_visible(wp))
                    198:                        cmd_display_panes_draw_pane(ctx, wp);
                    199:        }
1.13      nicm      200: }
                    201:
                    202: static void
1.24      nicm      203: cmd_display_panes_free(struct client *c)
                    204: {
                    205:        struct cmd_display_panes_data   *cdata = c->overlay_data;
                    206:
                    207:        if (cdata->item != NULL)
1.28      nicm      208:                cmdq_continue(cdata->item);
1.24      nicm      209:        free(cdata->command);
                    210:        free(cdata);
                    211: }
                    212:
                    213: static int
                    214: cmd_display_panes_key(struct client *c, struct key_event *event)
1.13      nicm      215: {
1.24      nicm      216:        struct cmd_display_panes_data   *cdata = c->overlay_data;
1.33      nicm      217:        char                            *cmd, *expanded, *error;
1.24      nicm      218:        struct window                   *w = c->session->curw->window;
                    219:        struct window_pane              *wp;
1.33      nicm      220:        enum cmd_parse_status            status;
1.36      nicm      221:        u_int                            index;
                    222:        key_code                         key;
1.13      nicm      223:
1.36      nicm      224:        if (event->key >= '0' && event->key <= '9')
                    225:                index = event->key - '0';
                    226:        else if ((event->key & KEYC_MASK_MODIFIERS) == 0) {
                    227:                key = (event->key & KEYC_MASK_KEY);
                    228:                if (key >= 'a' && key <= 'z')
                    229:                        index = 10 + (key - 'a');
                    230:                else
                    231:                        return (-1);
                    232:        } else
1.29      nicm      233:                return (-1);
1.24      nicm      234:
1.36      nicm      235:        wp = window_pane_at_index(w, index);
1.15      nicm      236:        if (wp == NULL)
1.24      nicm      237:                return (1);
                    238:        window_unzoom(w);
1.21      nicm      239:
1.15      nicm      240:        xasprintf(&expanded, "%%%u", wp->id);
1.24      nicm      241:        cmd = cmd_template_replace(cdata->command, expanded, 1);
1.15      nicm      242:
1.33      nicm      243:        status = cmd_parse_and_append(cmd, NULL, c, NULL, &error);
                    244:        if (status == CMD_PARSE_ERROR) {
                    245:                cmdq_append(c, cmdq_get_error(error));
                    246:                free(error);
1.23      nicm      247:        }
1.15      nicm      248:
                    249:        free(cmd);
                    250:        free(expanded);
1.24      nicm      251:        return (1);
                    252: }
                    253:
                    254: static enum cmd_retval
                    255: cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
                    256: {
1.31      nicm      257:        struct args                     *args = cmd_get_args(self);
1.34      nicm      258:        struct client                   *tc = cmdq_get_target_client(item);
                    259:        struct session                  *s = tc->session;
1.24      nicm      260:        u_int                            delay;
                    261:        char                            *cause;
                    262:        struct cmd_display_panes_data   *cdata;
                    263:
1.34      nicm      264:        if (tc->overlay_draw != NULL)
1.24      nicm      265:                return (CMD_RETURN_NORMAL);
1.13      nicm      266:
1.24      nicm      267:        if (args_has(args, 'd')) {
                    268:                delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
                    269:                if (cause != NULL) {
                    270:                        cmdq_error(item, "delay %s", cause);
                    271:                        free(cause);
                    272:                        return (CMD_RETURN_ERROR);
                    273:                }
                    274:        } else
                    275:                delay = options_get_number(s->options, "display-panes-time");
                    276:
                    277:        cdata = xmalloc(sizeof *cdata);
                    278:        if (args->argc != 0)
                    279:                cdata->command = xstrdup(args->argv[0]);
                    280:        else
                    281:                cdata->command = xstrdup("select-pane -t '%%'");
                    282:        if (args_has(args, 'b'))
                    283:                cdata->item = NULL;
                    284:        else
                    285:                cdata->item = item;
1.21      nicm      286:
1.38      nicm      287:        if (args_has(args, 'N')) {
                    288:                server_client_set_overlay(tc, delay, NULL, NULL,
1.39    ! nicm      289:                    cmd_display_panes_draw, NULL, cmd_display_panes_free, NULL,
1.38      nicm      290:                    cdata);
                    291:        } else {
                    292:                server_client_set_overlay(tc, delay, NULL, NULL,
                    293:                    cmd_display_panes_draw, cmd_display_panes_key,
1.39    ! nicm      294:                    cmd_display_panes_free, NULL, cdata);
1.38      nicm      295:        }
1.21      nicm      296:
1.24      nicm      297:        if (args_has(args, 'b'))
                    298:                return (CMD_RETURN_NORMAL);
                    299:        return (CMD_RETURN_WAIT);
1.1       nicm      300: }