summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2019-12-27 23:18:16 -0800
committerJohn Hawthorn <john@hawthorn.email>2019-12-27 23:18:16 -0800
commit28195b3a709995354b6c33cec9bb7c9f81532eb6 (patch)
treefbfd61fb82d8212284f5d5a0319ff3a8ec2b82d1
parent04c342cd0bbff6f9a790ba1f3abc79517bbec096 (diff)
parent8505df59ffd9b578f98591520b51abc818509fc3 (diff)
Merge branch 'show-info'
-rw-r--r--src/config.def.h1
-rw-r--r--src/options.c8
-rw-r--r--src/options.h1
-rw-r--r--src/tty_interface.c15
-rw-r--r--test/acceptance/acceptance_test.rb11
5 files changed, 31 insertions, 5 deletions
diff --git a/src/config.def.h b/src/config.def.h
index 27ef4b1..fcdcc03 100644
--- a/src/config.def.h
+++ b/src/config.def.h
@@ -16,3 +16,4 @@
#define DEFAULT_PROMPT "> "
#define DEFAULT_NUM_LINES 10
#define DEFAULT_WORKERS 0
+#define DEFAULT_SHOW_INFO 0
diff --git a/src/options.c b/src/options.c
index 58f8fa0..e35402f 100644
--- a/src/options.c
+++ b/src/options.c
@@ -19,6 +19,7 @@ static const char *usage_str =
" -s, --show-scores Show the scores of each match\n"
" -0, --read-null Read input delimited by ASCII NUL characters\n"
" -j, --workers NUM Use NUM workers for searching. (default is # of CPUs)\n"
+ " -i, --show-info Show selection info line\n"
" -h, --help Display this help and exit\n"
" -v, --version Output version information and exit\n";
@@ -36,6 +37,7 @@ static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'
{"version", no_argument, NULL, 'v'},
{"benchmark", optional_argument, NULL, 'b'},
{"workers", required_argument, NULL, 'j'},
+ {"show-info", no_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
@@ -51,13 +53,14 @@ void options_init(options_t *options) {
options->prompt = DEFAULT_PROMPT;
options->workers = DEFAULT_WORKERS;
options->input_delimiter = '\n';
+ options->show_info = DEFAULT_SHOW_INFO;
}
void options_parse(options_t *options, int argc, char *argv[]) {
options_init(options);
int c;
- while ((c = getopt_long(argc, argv, "vhs0e:q:l:t:p:j:", longopts, NULL)) != -1) {
+ while ((c = getopt_long(argc, argv, "vhs0e:q:l:t:p:j:i", longopts, NULL)) != -1) {
switch (c) {
case 'v':
printf("%s " VERSION " © 2014-2018 John Hawthorn\n", argv[0]);
@@ -108,6 +111,9 @@ void options_parse(options_t *options, int argc, char *argv[]) {
}
options->num_lines = l;
} break;
+ case 'i':
+ options->show_info = 1;
+ break;
case 'h':
default:
usage(argv[0]);
diff --git a/src/options.h b/src/options.h
index 6098a64..4be4cb6 100644
--- a/src/options.h
+++ b/src/options.h
@@ -12,6 +12,7 @@ typedef struct {
const char *prompt;
unsigned int workers;
char input_delimiter;
+ int show_info;
} options_t;
void options_init(options_t *options);
diff --git a/src/tty_interface.c b/src/tty_interface.c
index aa67f1f..343dde8 100644
--- a/src/tty_interface.c
+++ b/src/tty_interface.c
@@ -20,7 +20,7 @@ static void clear(tty_interface_t *state) {
tty_setcol(tty, 0);
size_t line = 0;
- while (line++ < state->options->num_lines) {
+ while (line++ < state->options->num_lines + (state->options->show_info ? 1 : 0)) {
tty_newline(tty);
}
tty_clearline(tty);
@@ -90,9 +90,16 @@ static void draw(tty_interface_t *state) {
start = available - num_lines;
}
}
+
tty_setcol(tty, 0);
tty_printf(tty, "%s%s", options->prompt, state->search);
tty_clearline(tty);
+
+ if (options->show_info) {
+ tty_printf(tty, "\n[%lu/%lu]", choices->available, choices->size);
+ tty_clearline(tty);
+ }
+
for (size_t i = start; i < start + num_lines; i++) {
tty_printf(tty, "\n");
tty_clearline(tty);
@@ -101,9 +108,9 @@ static void draw(tty_interface_t *state) {
draw_match(state, choice, i == choices->selection);
}
}
- if (num_lines > 0) {
- tty_moveup(tty, num_lines);
- }
+
+ if (num_lines + options->show_info)
+ tty_moveup(tty, num_lines + options->show_info);
tty_setcol(tty, 0);
fputs(options->prompt, tty->fout);
diff --git a/test/acceptance/acceptance_test.rb b/test/acceptance/acceptance_test.rb
index ba3ec01..13af803 100644
--- a/test/acceptance/acceptance_test.rb
+++ b/test/acceptance/acceptance_test.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
require 'minitest'
require 'minitest/autorun'
require 'ttytest'
@@ -446,6 +447,15 @@ class FzyTest < Minitest::Test
TTY
end
+ def test_show_info
+ @tty = interactive_fzy(input: %w[foo bar baz], args: "-i")
+ @tty.assert_matches ">\n[3/3]\nfoo\nbar\nbaz"
+ @tty.send_keys("ba")
+ @tty.assert_matches "> ba\n[2/3]\nbar\nbaz"
+ @tty.send_keys("q")
+ @tty.assert_matches "> baq\n[0/3]"
+ end
+
def test_help
@tty = TTYtest.new_terminal(%{#{FZY_PATH} --help})
@tty.assert_matches <<TTY
@@ -458,6 +468,7 @@ Usage: fzy [OPTION]...
-s, --show-scores Show the scores of each match
-0, --read-null Read input delimited by ASCII NUL characters
-j, --workers NUM Use NUM workers for searching. (default is # of CPUs)
+ -i, --show-info Show selection info line
-h, --help Display this help and exit
-v, --version Output version information and exit
TTY