Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/tig/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void update_status(const char *msg, ...) PRINTF_LIKE(1, 2);
void update_status_with_context(const char *context, const char *msg, ...) PRINTF_LIKE(2, 3);
void report(const char *msg, ...) PRINTF_LIKE(1, 2);
void report_clear(void);
void set_terminal_title(const char *title);

/*
* Display management.
Expand Down
34 changes: 34 additions & 0 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,15 @@ report_clear(void)
update_view_title(view);
}

static void
restore_terminal_title(void)
{
static const char restore_seq[] = "\033[23;2t";

if (opt_tty.fd >= 0)
write(opt_tty.fd, restore_seq, sizeof(restore_seq) - 1);
}

static void
done_display(void)
{
Expand All @@ -581,6 +590,7 @@ done_display(void)
}
curs_set(1);
endwin();
restore_terminal_title();
}
cursed = false;

Expand All @@ -606,6 +616,29 @@ set_terminal_modes(void)
leaveok(stdscr, false);
}

void
set_terminal_title(const char *title)
{
char buf[SIZEOF_STR];
int len;

if (opt_tty.fd < 0)
return;

len = snprintf(buf, sizeof(buf), "\033]0;%s\007", title);
if (len > 0 && (size_t) len < sizeof(buf))
write(opt_tty.fd, buf, len);
}

static void
save_terminal_title(void)
{
static const char save_seq[] = "\033[22;2t";

if (opt_tty.fd >= 0)
write(opt_tty.fd, save_seq, sizeof(save_seq) - 1);
}

void
init_tty(void)
{
Expand Down Expand Up @@ -665,6 +698,7 @@ init_display(void)
die("Failed to initialize curses");

set_terminal_modes();
save_terminal_title();
init_colors();

getmaxyx(stdscr, y, x);
Expand Down
39 changes: 39 additions & 0 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,37 @@ update_view(struct view *view)
return true;
}

static const char *
get_repo_name(void)
{
static char name[SIZEOF_REF];
char *resolved, *base, *dir;

if (*name)
return name;

if (!*repo.git_dir) {
string_ncopy(name, "unknown", 7);
return name;
}

resolved = realpath(repo.git_dir, NULL);
if (!resolved) {
string_ncopy(name, "unknown", 7);
return name;
}

base = basename(resolved);
if (!strcmp(base, ".git")) {
dir = dirname(resolved);
base = basename(dir);
}

string_ncopy(name, base, strlen(base));
free(resolved);
return name;
}

void
update_view_title(struct view *view)
{
Expand Down Expand Up @@ -712,6 +743,14 @@ update_view_title(struct view *view)
lines = view->lines ? MIN(view_lines, view->lines) * 100 / view->lines : 0;
mvwprintw(window, 0, view->width - count_digits(lines) - 1, "%u%%", lines);

if (view == display[current_view]) {
char title[SIZEOF_STR];

snprintf(title, sizeof(title), "tig (%s): %s",
get_repo_name(), view->name);
set_terminal_title(title);
}

wnoutrefresh(window);
}

Expand Down