Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single-line output of external command to status bar #678

Merged
merged 2 commits into from
Jul 26, 2017
Merged
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
5 changes: 5 additions & 0 deletions doc/tigrc.5.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ the command that should be executed.
|=============================================================================
|! |Run the command in the foreground with output shown.
|@ |Run the command in the background with no output.
|+ |Run the command synchronously, and echo the first line
of output to the status bar.
|? |Prompt the user before executing the command.
|< |Exit Tig after executing the command.
|=============================================================================
Expand Down Expand Up @@ -704,6 +706,9 @@ bind main S !git format-patch -1 %(commit)

# Create and checkout a new branch; specify custom prompt
bind main B ?git checkout -b "%(prompt Enter new branch name: )"

# Show commit statistics for the author under the cursor
bind main U +sh -c 'git --no-pager shortlog -s --author="$(git show -s --format=%aE %(commit))" </dev/tty'
--------------------------------------------------------------------------

Advanced shell-like commands
Expand Down
2 changes: 1 addition & 1 deletion include/tig/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool save_view(struct view *view, const char *path);
bool vertical_split_is_enabled(enum vertical_split vsplit, int height, int width);
int apply_vertical_split(int base_width);

bool open_external_viewer(const char *argv[], const char *dir, bool silent, bool confirm, bool refresh, const char *notice);
bool open_external_viewer(const char *argv[], const char *dir, bool silent, bool confirm, bool echo, bool refresh, const char *notice);
void open_editor(const char *file, unsigned int lineno);
void enable_mouse(bool enable);

Expand Down
2 changes: 1 addition & 1 deletion include/tig/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool io_get(struct io *io, struct buffer *buf, int c, bool can_read);
bool io_write(struct io *io, const void *buf, size_t bufsize);
bool io_printf(struct io *io, const char *fmt, ...) PRINTF_LIKE(2, 3);
bool io_read_buf(struct io *io, char buf[], size_t bufsize, bool allow_empty);
bool io_run_buf(const char **argv, char buf[], size_t bufsize, bool allow_empty);
bool io_run_buf(const char **argv, char buf[], size_t bufsize, const char *dir, bool allow_empty);
enum status_code io_load(struct io *io, const char *separators,
io_read_fn read_property, void *data);
enum status_code io_load_span(struct io *io, const char *separators,
Expand Down
1 change: 1 addition & 0 deletions include/tig/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct run_request_flags {
bool confirm;
bool exit;
bool internal;
bool echo;
};

struct run_request {
Expand Down
2 changes: 1 addition & 1 deletion src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ blob_open(struct view *view, enum open_flags flags)
};

if (!string_format(blob_spec, "%s:%s", commit, view->env->file) ||
!io_run_buf(rev_parse_argv, view->env->blob, sizeof(view->env->blob), false))
!io_run_buf(rev_parse_argv, view->env->blob, sizeof(view->env->blob), NULL, false))
return error("Failed to resolve blob from file name");

string_ncopy(state->commit, commit, strlen(commit));
Expand Down
17 changes: 14 additions & 3 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,22 @@ open_script(const char *path)
}

bool
open_external_viewer(const char *argv[], const char *dir, bool silent, bool confirm, bool refresh, const char *notice)
open_external_viewer(const char *argv[], const char *dir, bool silent, bool confirm, bool echo, bool refresh, const char *notice)
{
bool ok;

if (silent || is_script_executing()) {
if (echo) {
char buf[SIZEOF_STR] = "";

io_run_buf(argv, buf, sizeof(buf), dir, false);
if (*buf) {
report("%s", buf);
return true;
} else {
report("No output");
return false;
}
} else if (silent || is_script_executing()) {
ok = io_run_bg(argv, dir);

} else {
Expand Down Expand Up @@ -127,7 +138,7 @@ open_editor(const char *file, unsigned int lineno)
if (lineno && opt_editor_line_number && string_format(lineno_cmd, "+%u", lineno))
editor_argv[argc++] = lineno_cmd;
editor_argv[argc] = file;
if (!open_external_viewer(editor_argv, repo.cdup, false, false, true, EDITOR_LINENO_MSG))
if (!open_external_viewer(editor_argv, repo.cdup, false, false, false, true, EDITOR_LINENO_MSG))
opt_editor_line_number = false;
}

Expand Down
8 changes: 4 additions & 4 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ get_path_encoding(const char *path, struct encoding *default_encoding)

/* <path>: encoding: <encoding> */

if (!*path || !io_run_buf(check_attr_argv, buf, sizeof(buf), false)
if (!*path || !io_run_buf(check_attr_argv, buf, sizeof(buf), NULL, false)
|| !(encoding = strstr(buf, ENCODING_SEP)))
return default_encoding;

Expand All @@ -121,7 +121,7 @@ get_path_encoding(const char *path, struct encoding *default_encoding)
"file", "-I", "--", path, NULL
};

if (!*path || !io_run_buf(file_argv, buf, sizeof(buf), false)
if (!*path || !io_run_buf(file_argv, buf, sizeof(buf), NULL, false)
|| !(encoding = strstr(buf, CHARSET_SEP)))
return default_encoding;

Expand Down Expand Up @@ -582,11 +582,11 @@ io_read_buf(struct io *io, char buf[], size_t bufsize, bool allow_empty)
}

bool
io_run_buf(const char **argv, char buf[], size_t bufsize, bool allow_empty)
io_run_buf(const char **argv, char buf[], size_t bufsize, const char *dir, bool allow_empty)
{
struct io io;

return io_run(&io, IO_RD, NULL, NULL, argv) && io_read_buf(&io, buf, bufsize, allow_empty);
return io_run(&io, IO_RD, dir, NULL, argv) && io_read_buf(&io, buf, bufsize, allow_empty);
}

bool
Expand Down
6 changes: 5 additions & 1 deletion src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static size_t run_requests;

DEFINE_ALLOCATOR(realloc_run_requests, struct run_request, 8)

#define COMMAND_FLAGS ":!?@<"
#define COMMAND_FLAGS ":!?@<+"

enum status_code
parse_run_request_flags(struct run_request_flags *flags, const char **argv)
Expand All @@ -469,6 +469,8 @@ parse_run_request_flags(struct run_request_flags *flags, const char **argv)
flags->confirm = 1;
} else if (*argv[0] == '<') {
flags->exit = 1;
} else if (*argv[0] == '+') {
flags->echo = 1;
} else if (*argv[0] != '!') {
break;
}
Expand Down Expand Up @@ -529,6 +531,8 @@ format_run_request_flags(const struct run_request *req)
flags[flagspos++] = '?';
if (req->flags.exit)
flags[flagspos++] = '<';
if (req->flags.echo)
flags[flagspos++] = '+';
if (flagspos > 1)
flags[flagspos++] = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ exec_run_request(struct view *view, struct run_request *req)

if (confirmed)
open_external_viewer(argv, repo.cdup, req->flags.silent,
!req->flags.exit, false, "");
!req->flags.exit, req->flags.echo, false, "");
}

if (argv)
Expand Down
2 changes: 1 addition & 1 deletion src/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ open_mergetool(const char *file)
{
const char *mergetool_argv[] = { "git", "mergetool", file, NULL };

open_external_viewer(mergetool_argv, repo.cdup, false, true, true, "");
open_external_viewer(mergetool_argv, repo.cdup, false, true, false, true, "");
}

static enum request
Expand Down
2 changes: 1 addition & 1 deletion src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ goto_id(struct view *view, const char *expr, bool from_start, bool save_search)
const char *rev_parse_argv[] = {
"git", "rev-parse", "--revs-only", rev, NULL
};
bool ok = rev && io_run_buf(rev_parse_argv, id, sizeof(id), true);
bool ok = rev && io_run_buf(rev_parse_argv, id, sizeof(id), NULL, true);

free(rev);
if (!ok) {
Expand Down
15 changes: 8 additions & 7 deletions test/tigrc/parse-test
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ bind main 1 !external command
bind main 2 @silent command
bind main 3 ?prompted command
bind main 4 <quitting command
bind main 0 !@?<all modifiers
bind main 5 +echoed command
bind main 0 !@?<+all modifiers

# Non-ending multi-line command
c\\
Expand Down Expand Up @@ -98,9 +99,9 @@ tig warning: ~/.tigrc:25: Unknown color attribute: normally
tig warning: ~/.tigrc:34: Unknown option \`visibility' for column line-number
tig warning: ~/.tigrc:36: Invalid key binding: bind keymap key action
tig warning: ~/.tigrc:37: Invalid key binding: bind keymap key action
tig warning: ~/.tigrc:38: Unknown command flag '%'; expected one of :!?@<
tig warning: ~/.tigrc:39: Unknown command flag '|'; expected one of :!?@<
tig warning: ~/.tigrc:56: Unknown option command: c
tig warning: ~/.tigrc:38: Unknown command flag '%'; expected one of :!?@<+
tig warning: ~/.tigrc:39: Unknown command flag '|'; expected one of :!?@<+
tig warning: ~/.tigrc:57: Unknown option command: c
tig warning: Errors while loading HOME/.tigrc.
EOF

Expand All @@ -120,7 +121,8 @@ External commands:
2 @silent command
3 ?prompted command
4 <quitting command
0 @?<all modifiers
5 +echoed command
0 @?<+all modifiers



Expand All @@ -132,6 +134,5 @@ External commands:




[help] - line 1 of 16 100%
[help] - line 1 of 17 100%
EOF