/* $Id: status.c,v 1.88 2009-06-25 16:34:50 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "tmux.h"
char *status_replace(struct session *, char *, time_t);
char *status_replace_popen(char **);
size_t status_width(struct winlink *);
char *status_print(struct session *, struct winlink *, struct grid_cell *);
void status_prompt_add_history(struct client *);
char *status_prompt_complete(const char *);
/* Draw status for client on the last lines of given context. */
int
status_redraw(struct client *c)
{
struct screen_write_ctx ctx;
struct session *s = c->session;
struct winlink *wl;
struct window_pane *wp;
struct screen *sc = NULL, old_status;
char *left, *right, *text, *ptr;
size_t llen, llen2, rlen, rlen2, offset;
size_t xx, yy, sy, size, start, width;
struct grid_cell stdgc, gc;
int larrow, rarrow, utf8flag;
left = right = NULL;
/* Create the target screen. */
memcpy(&old_status, &c->status, sizeof old_status);
screen_init(&c->status, c->tty.sx, 1, 0);
/* No status line? */
if (c->tty.sy == 0 || !options_get_number(&s->options, "status"))
goto off;
larrow = rarrow = 0;
if (gettimeofday(&c->status_timer, NULL) != 0)
fatal("gettimeofday");
memcpy(&stdgc, &grid_default_cell, sizeof gc);
stdgc.bg = options_get_number(&s->options, "status-fg");
stdgc.fg = options_get_number(&s->options, "status-bg");
stdgc.attr |= options_get_number(&s->options, "status-attr");
yy = c->tty.sy - 1;
if (yy == 0)
goto blank;
/* Caring about UTF-8 in status line? */
utf8flag = options_get_number(&s->options, "status-utf8");
/* Work out the left and right strings. */
left = status_replace(s, options_get_string(
&s->options, "status-left"), c->status_timer.tv_sec);
llen = options_get_number(&s->options, "status-left-length");
llen2 = screen_write_strlen(utf8flag, "%s", left);
if (llen2 < llen)
llen = llen2;
right = status_replace(s, options_get_string(
&s->options, "status-right"), c->status_timer.tv_sec);
rlen = options_get_number(&s->options, "status-right-length");
rlen2 = screen_write_strlen(utf8flag, "%s", right);
if (rlen2 < rlen)
rlen = rlen2;
right[rlen] = '\0';
/*
* Figure out how much space we have for the window list. If there isn't
* enough space, just wimp out.
*/
xx = 0;
if (llen != 0)
xx += llen + 1;
if (rlen != 0)
xx += rlen + 1;
if (c->tty.sx == 0 || c->tty.sx <= xx)
goto blank;
xx = c->tty.sx - xx;
/*
* Right. We have xx characters to fill. Find out how much is to go in
* them and the offset of the current window (it must be on screen).
*/
width = offset = 0;
RB_FOREACH(wl, winlinks, &s->windows) {
size = status_width(wl) + 1;
if (wl == s->curw)
offset = width;
width += size;
}
start = 0;
/* If there is enough space for the total width, all is gravy. */
if (width <= xx)
goto draw;
/* Find size of current window text. */