index c971ff754476630cbb858e6f9659ebf081cd9a19..199eb34b7cfc434e915bf9026114dafb68ca280d 100644 (file)
--- a/tig.c
+++ b/tig.c
#define VIEW_MAX_LEN(view) ((view)->width + (view)->yoffset - (view)->col)
-static int
+static bool
draw_chars(struct view *view, enum line_type type, const char *string,
int max_len, bool use_tilde)
{
size_t skip = view->yoffset > view->col ? view->yoffset - view->col : 0;
if (max_len <= 0)
- return 0;
+ return VIEW_MAX_LEN(view) <= 0;
len = utf8_length(&string, skip, &col, max_len, &trimmed, use_tilde, opt_tab_size);
}
}
- return col;
+ view->col += col;
+ return VIEW_MAX_LEN(view) <= 0;
}
-static int
+static bool
draw_space(struct view *view, enum line_type type, int max, int spaces)
{
static char space[] = " ";
- int col = 0;
spaces = MIN(max, spaces);
while (spaces > 0) {
int len = MIN(spaces, sizeof(space) - 1);
- col += draw_chars(view, type, space, len, FALSE);
+ if (draw_chars(view, type, space, len, FALSE))
+ return TRUE;
spaces -= len;
}
- return col;
+ return VIEW_MAX_LEN(view) <= 0;
}
static bool
do {
size_t pos = string_expand(text, sizeof(text), string, opt_tab_size);
- view->col += draw_chars(view, type, text, VIEW_MAX_LEN(view), TRUE);
+ if (draw_chars(view, type, text, VIEW_MAX_LEN(view), TRUE))
+ return TRUE;
string += pos;
- } while (*string && VIEW_MAX_LEN(view) > 0);
+ } while (*string);
return VIEW_MAX_LEN(view) <= 0;
}
draw_field(struct view *view, enum line_type type, const char *text, int len, bool trim)
{
int max = MIN(VIEW_MAX_LEN(view), len);
- int col;
+ int col = view->col;
- if (text)
- col = draw_chars(view, type, text, max - 1, trim);
- else
- col = draw_space(view, type, max - 1, max - 1);
+ if (!text)
+ return draw_space(view, type, max, max);
- view->col += col;
- view->col += draw_space(view, LINE_DEFAULT, max - col, max - col);
- return VIEW_MAX_LEN(view) <= 0;
+ return draw_chars(view, type, text, max - 1, trim)
+ || draw_space(view, LINE_DEFAULT, max - (view->col - col), max);
}
static bool
const char *date = mkdate(time, opt_date);
int cols = opt_date == DATE_SHORT ? DATE_SHORT_COLS : DATE_COLS;
+ if (opt_date == DATE_NO)
+ return FALSE;
+
return draw_field(view, LINE_DATE, date, cols, FALSE);
}
bool trim = opt_author_cols == 0 || opt_author_cols > 5;
bool abbreviate = opt_author == AUTHOR_ABBREVIATED || !trim;
+ if (opt_author == AUTHOR_NO)
+ return FALSE;
+
if (abbreviate && author)
author = get_author_initials(author);
text = number;
}
if (text)
- view->col += draw_chars(view, LINE_LINE_NUMBER, text, max, TRUE);
+ draw_chars(view, LINE_LINE_NUMBER, text, max, TRUE);
else
- view->col += draw_space(view, LINE_LINE_NUMBER, max, digits3);
+ draw_space(view, LINE_LINE_NUMBER, max, digits3);
return draw_graphic(view, LINE_DEFAULT, &separator, 1, TRUE);
}
@@ -2499,14 +2504,6 @@ prepare_io(struct view *view, const char *dir, const char *argv[], bool replace)
return format_argv(&view->argv, argv, replace, !view->prev);
}
-static bool
-prepare_update(struct view *view, const char *argv[], const char *dir)
-{
- if (view->pipe)
- end_update(view, TRUE);
- return prepare_io(view, dir, argv, FALSE);
-}
-
static bool
start_update(struct view *view, const char **argv, const char *dir)
{
io_run(&view->io, IO_RD, dir, view->argv);
}
-static bool
-prepare_update_file(struct view *view, const char *name)
-{
- if (view->pipe)
- end_update(view, TRUE);
- argv_free(view->argv);
- return io_open(&view->io, "%s/%s", opt_cdup[0] ? opt_cdup : ".", name);
-}
-
static bool
begin_update(struct view *view, const char *dir, const char **argv, enum open_flags flags)
{
@@ -2551,6 +2539,9 @@ begin_update(struct view *view, const char *dir, const char **argv, enum open_fl
if (view->argv && view->argv[0] &&
!io_run(&view->io, IO_RD, view->dir, view->argv))
return FALSE;
+ else if (view->argv && !strcmp(view->argv[0], opt_cdup) &&
+ !io_open(&view->io, "%s%s", opt_cdup, view->argv[1]))
+ return FALSE;
setup_update(view, view->id);
}
}
+static void
+open_argv(struct view *prev, struct view *view, const char *argv[], const char *dir, enum open_flags flags)
+{
+ enum request request = view - views + REQ_OFFSET + 1;
+
+ if (view->pipe)
+ end_update(view, TRUE);
+ if (!prepare_io(view, dir, argv, FALSE)) {
+ report("Failed to open %s view: %s", view->name, io_strerror(&view->io));
+ } else {
+ open_view(prev, request, flags | OPEN_PREPARED);
+ }
+}
+
+static void
+open_file(struct view *prev, struct view *view, const char *file, enum open_flags flags)
+{
+ enum request request = view - views + REQ_OFFSET + 1;
+ const char *file_argv[] = { opt_cdup, file , NULL };
+
+ if (view->pipe)
+ end_update(view, TRUE);
+ if (!argv_copy(&view->argv, file_argv)) {
+ report("Failed to load %s: out of memory", file);
+ } else {
+ open_view(prev, request, flags | OPEN_PREPARED);
+ }
+}
+
static void
open_external_viewer(const char *argv[], const char *dir)
{
if (draw_mode(view, entry->mode))
return TRUE;
- if (opt_author && draw_author(view, entry->author))
+ if (draw_author(view, entry->author))
return TRUE;
- if (opt_date && draw_date(view, &entry->time))
+ if (draw_date(view, &entry->time))
return TRUE;
}
time = &blame->commit->time;
}
- if (opt_date && draw_date(view, time))
+ if (draw_date(view, time))
return TRUE;
- if (opt_author && draw_author(view, author))
+ if (draw_author(view, author))
return TRUE;
if (draw_field(view, LINE_BLAME_ID, id, ID_COLS, FALSE))
diff_index_argv[7] = "/dev/null";
}
- if (!prepare_update(diff, diff_index_argv, NULL)) {
- report("Failed to allocate diff command");
- break;
- }
- flags |= OPEN_PREPARED;
+ open_argv(view, diff, diff_index_argv, NULL, flags);
+ } else {
+ open_view(view, REQ_VIEW_DIFF, flags);
}
-
- open_view(view, REQ_VIEW_DIFF, flags);
if (VIEW(REQ_VIEW_DIFF)->pipe && !strcmp(blame->commit->id, NULL_ID))
string_copy_rev(VIEW(REQ_VIEW_DIFF)->ref, NULL_ID);
break;
struct branch *branch = line->data;
enum line_type type = branch->ref->head ? LINE_MAIN_HEAD : LINE_DEFAULT;
- if (opt_date && draw_date(view, &branch->time))
+ if (draw_date(view, &branch->time))
return TRUE;
- if (opt_author && draw_author(view, branch->author))
+ if (draw_author(view, branch->author))
return TRUE;
draw_text(view, type, branch->ref == &branch_all ? "All branches" : branch->ref->name);
};
struct view *main_view = VIEW(REQ_VIEW_MAIN);
- if (!prepare_update(main_view, all_branches_argv, NULL))
- report("Failed to load view of all branches");
- else
- open_view(view, REQ_VIEW_MAIN, OPEN_PREPARED | OPEN_SPLIT);
+ open_argv(view, main_view, all_branches_argv, NULL, OPEN_SPLIT);
return REQ_NONE;
}
default:
return TRUE;
}
-static enum request
-status_load_error(struct view *view, struct view *stage, const char *path)
-{
- if (displayed_views() == 2 || display[current_view] != view)
- maximize_view(view);
- report("Failed to load '%s': %s", path, io_strerror(&stage->io));
- return REQ_NONE;
-}
-
static enum request
status_enter(struct view *view, struct line *line)
{
* path, so leave it empty. */
const char *newpath = status && status->status != 'U' ? status->new.name : NULL;
const char *info;
- enum open_flags split;
+ enum open_flags flags = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT;
struct view *stage = VIEW(REQ_VIEW_STAGE);
if (line->type == LINE_STAT_NONE ||
"--", "/dev/null", newpath, NULL
};
- if (!prepare_update(stage, no_head_diff_argv, opt_cdup))
- return status_load_error(view, stage, newpath);
+ open_argv(view, stage, no_head_diff_argv, opt_cdup, flags);
} else {
const char *index_show_argv[] = {
"git", "diff-index", "--root", "--patch-with-stat",
oldpath, newpath, NULL
};
- if (!prepare_update(stage, index_show_argv, opt_cdup))
- return status_load_error(view, stage, newpath);
+ open_argv(view, stage, index_show_argv, opt_cdup, flags);
}
if (status)
"-C", "-M", "--", oldpath, newpath, NULL
};
- if (!prepare_update(stage, files_show_argv, opt_cdup))
- return status_load_error(view, stage, newpath);
+ open_argv(view, stage, files_show_argv, opt_cdup, flags);
if (status)
info = "Unstaged changes to %s";
else
return REQ_NONE;
}
- if (!prepare_update_file(stage, newpath))
- return status_load_error(view, stage, newpath);
+ open_file(view, stage, newpath, flags);
info = "Untracked file %s";
break;
die("line type %d not handled in switch", line->type);
}
- split = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT;
- open_view(view, REQ_VIEW_STAGE, OPEN_PREPARED | split);
if (view_is_displayed(VIEW(REQ_VIEW_STAGE))) {
if (status) {
stage_status = *status;
return REQ_VIEW_CLOSE;
}
- if (stage_line_type == LINE_STAT_UNTRACKED) {
- if (!suffixcmp(stage_status.new.name, -1, "/")) {
- report("Cannot display a directory");
- return REQ_NONE;
- }
-
- if (!prepare_update_file(view, stage_status.new.name)) {
- report("Failed to open file: %s", strerror(errno));
- return REQ_NONE;
- }
- }
open_view(view, REQ_VIEW_STAGE, OPEN_REFRESH);
return REQ_NONE;
if (!commit->author)
return FALSE;
- if (opt_date && draw_date(view, &commit->time))
+ if (draw_date(view, &commit->time))
return TRUE;
- if (opt_author && draw_author(view, commit->author))
+ if (draw_author(view, commit->author))
return TRUE;
if (opt_rev_graph && draw_graph(view, &commit->graph))
if (!argv_from_string(argv, &argc, cmd)) {
report("Too many arguments");
- } else if (!prepare_update(next, argv, NULL)) {
- report("Failed to format command");
} else {
- open_view(view, REQ_VIEW_PAGER, OPEN_PREPARED);
+ open_argv(view, next, argv, NULL, OPEN_DEFAULT);
}
}