From fff5211d2776185348d6b813f2a16cedd5f485d0 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Thu, 1 Dec 2022 20:24:35 +0000 Subject: Add --ascii-input option. --- README.md | 5 +++++ completions/tofi | 1 + doc/config | 5 +++++ doc/tofi.5.md | 8 ++++++++ doc/tofi.5.scd | 7 +++++++ src/config.c | 5 +++++ src/main.c | 16 +++++++++++++--- src/tofi.h | 1 + 8 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6344599..fee5463 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,11 @@ In roughly descending order, the most important options for performance are: sharpness, but your mileage may vary. This option has no effect if a path to a font file hasn't been passed to `--font`. +* `--ascii-input` - Proper Unicode handling is slower than plain ASCII - on the + order of a few ms for ~40 kB of input. Specifying `--ascii-input true` will + disable some of this handling, speeding up tofi's startup, but searching for + non-ASCII characters may not work properly. + * `--late-keyboard-init` - The last avoidable thing that slows down startup is initialisation of the keyboard. This only takes 1-2ms on my laptop, but up to 60ms on a Raspberry Pi Zero 2 W. Passing this option will delay keyboard diff --git a/completions/tofi b/completions/tofi index 328f7dc..d871db1 100644 --- a/completions/tofi +++ b/completions/tofi @@ -79,6 +79,7 @@ _tofi() --hint-font --late-keyboard-init --multi-instance + --ascii-input ) case "${prev}" in diff --git a/doc/config b/doc/config index 017aeb1..4ce0a7d 100644 --- a/doc/config +++ b/doc/config @@ -255,6 +255,11 @@ # from running simultaneously. multi-instance = false + # Assume input is plain ASCII, and disable some Unicode handling + # functions. This is faster, but means e.g. a search for "e" will not + # match "é". + ascii-input = false + # ### Inclusion # diff --git a/doc/tofi.5.md b/doc/tofi.5.md index e8f3c49..622858f 100644 --- a/doc/tofi.5.md +++ b/doc/tofi.5.md @@ -137,6 +137,14 @@ options. > > Default: false +**ascii-input**=*true\|false* + +> Assume input is plain ASCII, and disable some Unicode handling +> functions. This is faster, but means e.g. a search for "e" will not +> match "é". +> +> Default: false + # STYLE OPTIONS **font**=*font* diff --git a/doc/tofi.5.scd b/doc/tofi.5.scd index 72be59e..4dde4d6 100644 --- a/doc/tofi.5.scd +++ b/doc/tofi.5.scd @@ -118,6 +118,13 @@ options. Default: false +*ascii-input*=_true|false_ + Assume input is plain ASCII, and disable some Unicode handling + functions. This is faster, but means e.g. a search for "e" will not + match "é". + + Default: false + # STYLE OPTIONS *font*=_font_ diff --git a/src/config.c b/src/config.c index 83321ed..0cfd566 100644 --- a/src/config.c +++ b/src/config.c @@ -686,6 +686,11 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const if (!err) { tofi->multiple_instance = val; } + } else if (strcasecmp(option, "ascii-input") == 0) { + bool val = parse_bool(filename, lineno, value, &err); + if (!err) { + tofi->ascii_input = val; + } } else if (strcasecmp(option, "late-keyboard-init") == 0) { bool val = parse_bool(filename, lineno, value, &err); if (!err) { diff --git a/src/main.c b/src/main.c index 7a92c19..cde56b4 100644 --- a/src/main.c +++ b/src/main.c @@ -48,7 +48,7 @@ static uint32_t gettime_ms() { /* Read all of stdin into a buffer. */ -static char *read_stdin() { +static char *read_stdin(bool normalize) { const size_t block_size = BUFSIZ; size_t num_blocks = 1; size_t buf_size = block_size; @@ -66,12 +66,21 @@ static char *read_stdin() { stdin); if (bytes_read != block_size) { if (!feof(stdin) && ferror(stdin)) { - log_error("Error reading stdin\n"); + log_error("Error reading stdin.\n"); } buf[block * block_size + bytes_read] = '\0'; break; } } + if (normalize) { + if (utf8_validate(buf)) { + char *tmp = utf8_normalize(buf); + free(buf); + buf = tmp; + } else { + log_error("Invalid UTF-8 in stdin.\n"); + } + } return buf; } @@ -875,6 +884,7 @@ const struct option long_options[] = { {"terminal", required_argument, NULL, 0}, {"hint-font", required_argument, NULL, 0}, {"multi-instance", required_argument, NULL, 0}, + {"ascii-input", required_argument, NULL, 0}, {"output", required_argument, NULL, 0}, {"scale", required_argument, NULL, 0}, {"late-keyboard-init", optional_argument, NULL, 'k'}, @@ -1360,7 +1370,7 @@ int main(int argc, char *argv[]) log_debug("App list generated.\n"); } else { log_debug("Reading stdin.\n"); - char *buf = read_stdin(); + char *buf = read_stdin(!tofi.ascii_input); tofi.window.entry.command_buffer = buf; tofi.window.entry.commands = string_ref_vec_from_buffer(buf); if (tofi.use_history) { diff --git a/src/tofi.h b/src/tofi.h index b7437a1..547b4ed 100644 --- a/src/tofi.h +++ b/src/tofi.h @@ -86,6 +86,7 @@ struct tofi { /* Options */ uint32_t anchor; + bool ascii_input; bool hide_cursor; bool use_history; bool use_scale; -- cgit v1.2.3