From 017d42790b7c8732252bee399be7d9c6394b8879 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Thu, 2 May 2019 00:31:15 -0700 Subject: [PATCH 1/4] Add fish shell argument completions for --language option --- assets/completions/bat.fish | 38 +++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/assets/completions/bat.fish b/assets/completions/bat.fish index 66bb5592a3..b09f8087f6 100644 --- a/assets/completions/bat.fish +++ b/assets/completions/bat.fish @@ -1,7 +1,39 @@ # Fish Shell Completions - # Place or symlink to $XDG_CONFIG_HOME/fish/completions/bat.fish ($XDG_CONFIG_HOME is usually set to ~/.config) +# Helper function: +function __bat_autocomplete_languages --description "A helper function used by "(status filename) + bat --list-languages | awk ' + NR == 1 { + dc = 0; + while (substr($0, dc, 2) != " ") dc++; + while (substr($0, dc, 1) == " ") dc++; + } + + { + langField = substr($0, 0, dc - 2); + if (langField !~ /^ *$/) { + lang = langField; + sub(/ +$/, "", lang); + } + + split(substr($0, dc), exts, ","); + for (i in exts) { + ext = exts[i] + + sub(/^ +/, "", ext); # Trim leading whitespace. + sub(/ +$/, "", ext); # Trim trailing whitespace. + + if (ext != "") { + print ext"\t"lang + } + } + } + ' | sort +end + +# Completions: + complete -c bat -l color -xka "auto never always" -d "Specify when to use colored output (default: auto)" -n "not __fish_seen_subcommand_from cache" complete -c bat -l config-dir -d "Display location of 'bat' configuration directory" -n "not __fish_seen_subcommand_from cache" @@ -16,9 +48,7 @@ complete -c bat -s H -l highlight-line -x -d " Highlight the N-th line with a complete -c bat -l italic-text -xka "always never" -d "Specify when to use ANSI sequences for italic text (default: never)" -n "not __fish_seen_subcommand_from cache" -# TODO: add parameter completion for available languages using option: -xka "(bat --list-languages | cat)" -# but replace 'cat' with some sed/awk like command that only outputs lines of valid options for this flag -complete -c bat -s l -l language -d "Set the language for syntax highlighting" -n "not __fish_seen_subcommand_from cache" +complete -c bat -s l -l language -d "Set the language for syntax highlighting" -n "not __fish_seen_subcommand_from cache" -xa "(__bat_autocomplete_languages)" complete -c bat -s r -l line-range -x -d " Only print the specified range of lines for each file" -n "not __fish_seen_subcommand_from cache" From 57736873927a02eff65770152b6d6e49ff5db9ac Mon Sep 17 00:00:00 2001 From: Ethan P Date: Thu, 2 May 2019 00:46:27 -0700 Subject: [PATCH 2/4] Remove language completions for config files --- assets/completions/bat.fish | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/completions/bat.fish b/assets/completions/bat.fish index b09f8087f6..c47c14a37d 100644 --- a/assets/completions/bat.fish +++ b/assets/completions/bat.fish @@ -24,7 +24,7 @@ function __bat_autocomplete_languages --description "A helper function used by " sub(/^ +/, "", ext); # Trim leading whitespace. sub(/ +$/, "", ext); # Trim trailing whitespace. - if (ext != "") { + if ((ext != "") && (ext !~ /[A-Z].*/)) { print ext"\t"lang } } From dfe39257195be285ea3c851c0afe6491fc2dee79 Mon Sep 17 00:00:00 2001 From: Ethan P Date: Fri, 10 May 2019 14:38:48 -0700 Subject: [PATCH 3/4] Added non-interactive mode for `--list-languages` This makes scripting it a lot easier and less hacky. --- src/main.rs | 76 +++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e9c4ccc50..0f09f6e145 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,48 +108,54 @@ pub fn list_languages(config: &Config) -> Result<()> { .collect::>(); languages.sort_by_key(|lang| lang.name.to_uppercase()); - let longest = languages - .iter() - .map(|syntax| syntax.name.len()) - .max() - .unwrap_or(32); // Fallback width if they have no language definitions. - - let comma_separator = ", "; - let separator = " "; - // Line-wrapping for the possible file extension overflow. - let desired_width = config.term_width - longest - separator.len(); - let stdout = io::stdout(); let mut stdout = stdout.lock(); - let style = if config.colored_output { - Green.normal() + if config.loop_through { + for lang in languages { + write!(stdout, "{}:{}\n", lang.name, lang.file_extensions.join(",")); + } } else { - Style::default() - }; - - for lang in languages { - write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?; - - // Number of characters on this line so far, wrap before `desired_width` - let mut num_chars = 0; - - let mut extension = lang.file_extensions.iter().peekable(); - while let Some(word) = extension.next() { - // If we can't fit this word in, then create a line break and align it in. - let new_chars = word.len() + comma_separator.len(); - if num_chars + new_chars >= desired_width { - num_chars = 0; - write!(stdout, "\n{:width$}{}", "", separator, width = longest)?; - } + let longest = languages + .iter() + .map(|syntax| syntax.name.len()) + .max() + .unwrap_or(32); // Fallback width if they have no language definitions. + + let comma_separator = ", "; + let separator = " "; + // Line-wrapping for the possible file extension overflow. + let desired_width = config.term_width - longest - separator.len(); + + let style = if config.colored_output { + Green.normal() + } else { + Style::default() + }; - num_chars += new_chars; - write!(stdout, "{}", style.paint(&word[..]))?; - if extension.peek().is_some() { - write!(stdout, "{}", comma_separator)?; + for lang in languages { + write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?; + + // Number of characters on this line so far, wrap before `desired_width` + let mut num_chars = 0; + + let mut extension = lang.file_extensions.iter().peekable(); + while let Some(word) = extension.next() { + // If we can't fit this word in, then create a line break and align it in. + let new_chars = word.len() + comma_separator.len(); + if num_chars + new_chars >= desired_width { + num_chars = 0; + write!(stdout, "\n{:width$}{}", "", separator, width = longest)?; + } + + num_chars += new_chars; + write!(stdout, "{}", style.paint(&word[..]))?; + if extension.peek().is_some() { + write!(stdout, "{}", comma_separator)?; + } } + writeln!(stdout)?; } - writeln!(stdout)?; } Ok(()) From 49db54c654884785998342af7fefcaf2c2db4bde Mon Sep 17 00:00:00 2001 From: Ethan P Date: Fri, 10 May 2019 15:13:18 -0700 Subject: [PATCH 4/4] Update fish completions for new --list-languages format --- assets/completions/bat.fish | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/assets/completions/bat.fish b/assets/completions/bat.fish index c47c14a37d..cbcb2ec2a1 100644 --- a/assets/completions/bat.fish +++ b/assets/completions/bat.fish @@ -3,28 +3,14 @@ # Helper function: function __bat_autocomplete_languages --description "A helper function used by "(status filename) - bat --list-languages | awk ' - NR == 1 { - dc = 0; - while (substr($0, dc, 2) != " ") dc++; - while (substr($0, dc, 1) == " ") dc++; - } - + bat --list-languages | awk -F':' ' { - langField = substr($0, 0, dc - 2); - if (langField !~ /^ *$/) { - lang = langField; - sub(/ +$/, "", lang); - } + lang=$1 + split($2, exts, ",") - split(substr($0, dc), exts, ","); for (i in exts) { - ext = exts[i] - - sub(/^ +/, "", ext); # Trim leading whitespace. - sub(/ +$/, "", ext); # Trim trailing whitespace. - - if ((ext != "") && (ext !~ /[A-Z].*/)) { + ext=exts[i] + if (ext !~ /[A-Z].*/ && ext !~ /^\..*rc$/) { print ext"\t"lang } }