diff --git a/scripts/action b/scripts/action index a9b2d50..844c6b8 100755 --- a/scripts/action +++ b/scripts/action @@ -8,37 +8,37 @@ source "${NAVI_HOME}/scripts/install" release() { - TAR_DIR="${NAVI_HOME}/target/tar" + TAR_DIR="${NAVI_HOME}/target/tar" - target="${1:-}" - if [[ $target == *"osx"* ]]; then - echoerr "OSX cross-compile is impossible. Fallbacking to cargo..." - target="" - fi + target="${1:-}" + if [[ $target == *"osx"* ]]; then + echoerr "OSX cross-compile is impossible. Fallbacking to cargo..." + target="" + fi - cd "$NAVI_HOME" + cd "$NAVI_HOME" - rm -rf "${NAVI_HOME}/target" 2> /dev/null || true + rm -rf "${NAVI_HOME}/target" 2> /dev/null || true - if [ -n "$target" ]; then - cargo install cross 2> /dev/null || true - cross build --release --locked --target "$target" - bin_folder="${target}/release" - else - cargo build --release --locked - bin_folder="release" - fi + if [ -n "$target" ]; then + cargo install cross 2> /dev/null || true + cross build --release --locked --target "$target" + bin_folder="${target}/release" + else + cargo build --release --locked + bin_folder="release" + fi - bin_path="${NAVI_HOME}/target/${bin_folder}/navi" - chmod +x "$bin_path" - mkdir -p "$TAR_DIR" 2> /dev/null || true + bin_path="${NAVI_HOME}/target/${bin_folder}/navi" + chmod +x "$bin_path" + mkdir -p "$TAR_DIR" 2> /dev/null || true - cp -r "${NAVI_HOME}/cheats" "$TAR_DIR" - cp -r "${NAVI_HOME}/shell" "$TAR_DIR" - cp "$bin_path" "$TAR_DIR" + cp -r "${NAVI_HOME}/cheats" "$TAR_DIR" + cp -r "${NAVI_HOME}/shell" "$TAR_DIR" + cp "$bin_path" "$TAR_DIR" - cd "${NAVI_HOME}/target/tar" - tar -czf navi.tar.gz * + cd "${NAVI_HOME}/target/tar" + tar -czf navi.tar.gz * } @@ -46,5 +46,5 @@ cmd="$1" shift case "$cmd" in - "release") release "$@" ;; + "release") release "$@" ;; esac diff --git a/scripts/brew b/scripts/brew index 11cd7bd..1944346 100755 --- a/scripts/brew +++ b/scripts/brew @@ -7,36 +7,36 @@ export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)" source "${NAVI_HOME}/scripts/install" gen_formula() { - version="$(latest_version_released)" - header "version: ${version}" + version="$(latest_version_released)" + header "version: ${version}" - header "sha_for x86_64-unknown-linux-musl..." - sha_linux="$(sha_for_asset_on_github "$version" "x86_64-unknown-linux-musl")" + header "sha_for x86_64-unknown-linux-musl..." + sha_linux="$(sha_for_asset_on_github "$version" "x86_64-unknown-linux-musl")" - header "sha_for x86_64-osx..." - sha_osx="$(sha_for_asset_on_github "$version" "x86_64-osx")" + header "sha_for x86_64-osx..." + sha_osx="$(sha_for_asset_on_github "$version" "x86_64-osx")" - header "rb..." - curl -s https://raw.githubusercontent.com/denisidoro/homebrew-tools/master/navirs.rb \ - | sed -E "s/version ['\"].*/version '${version}'/" \ - | awk '!x{x=sub("sha256","sha_osx")}7' \ - | awk '!x{x=sub("sha256","sha_linux")}7' \ - | sed -E "s/sha_osx.*/sha256 \"${sha_osx}\"/" \ - | sed -E "s/sha_linux.*/sha256 \"${sha_linux}\"/" + header "rb..." + curl -s https://raw.githubusercontent.com/denisidoro/homebrew-tools/master/navirs.rb \ + | sed -E "s/version ['\"].*/version '${version}'/" \ + | awk '!x{x=sub("sha256","sha_osx")}7' \ + | awk '!x{x=sub("sha256","sha_linux")}7' \ + | sed -E "s/sha_osx.*/sha256 \"${sha_osx}\"/" \ + | sed -E "s/sha_linux.*/sha256 \"${sha_linux}\"/" } gen_sha() { - version="${1:-$(latest_version_released)}" - header "version: ${version}" + version="${1:-$(latest_version_released)}" + header "version: ${version}" - header "sha_for source..." - sha_for_asset_on_github "$version" + header "sha_for source..." + sha_for_asset_on_github "$version" } cmd="$1" shift case "$cmd" in - "formula") gen_formula "$@" ;; - "sha") gen_sha "$@" ;; + "formula") gen_formula "$@" ;; + "sha") gen_sha "$@" ;; esac diff --git a/scripts/docker b/scripts/docker index e5319a1..f8787f9 100755 --- a/scripts/docker +++ b/scripts/docker @@ -6,6 +6,6 @@ export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)" cd "$NAVI_HOME" docker run \ - -e HOMEBREW_NO_AUTO_UPDATE=1 \ - -it linuxbrew/alpine \ - bash -c 'brew install denisidoro/tools/navirs; bash' + -e HOMEBREW_NO_AUTO_UPDATE=1 \ + -it linuxbrew/alpine \ + bash -c 'brew install denisidoro/tools/navirs; bash' diff --git a/scripts/fix b/scripts/fix index 0d927ab..4886a17 100755 --- a/scripts/fix +++ b/scripts/fix @@ -2,7 +2,16 @@ set -euo pipefail export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)" +source "${NAVI_HOME}/scripts/install" +header "Cargo nighly fix..." cargo +nightly fix --clippy -Z unstable-options 2> /dev/null || true + +header "Cargo fix..." cargo fix 2> /dev/null || true + +header "Cargo fmt..." cargo fmt 2> /dev/null || true + +header "dot code beautify..." +find scripts -type f | xargs -I% dot code beautify 2> /dev/null || true \ No newline at end of file diff --git a/scripts/install b/scripts/install index b4aae21..9b078d3 100755 --- a/scripts/install +++ b/scripts/install @@ -60,14 +60,14 @@ latest_version_released() { } asset_url() { - local -r version="$1" - local -r variant="${2:-}" - - if [[ -n "$variant" ]]; then - echo "https://github.com/denisidoro/navi/releases/download/v${version}/navi-${variant}.tar.gz" - else - echo "https://github.com/denisidoro/navi/archive/v${version}.tar.gz" - fi + local -r version="$1" + local -r variant="${2:-}" + + if [[ -n "$variant" ]]; then + echo "https://github.com/denisidoro/navi/releases/download/v${version}/navi-${variant}.tar.gz" + else + echo "https://github.com/denisidoro/navi/archive/v${version}.tar.gz" + fi } download_asset() { @@ -83,8 +83,8 @@ download_asset() { } sha_for_asset_on_github() { - local -r url="$(asset_url "$@")" - curl -sL "$url" | sha256 | awk '{print $1}' + local -r url="$(asset_url "$@")" + curl -sL "$url" | sha256 | awk '{print $1}' } get_target() { @@ -132,7 +132,7 @@ install_navi() { make install else - no_binary_warning + no_binary_warning echoerr "You don't have the necessary tools to build it" echoerr "Please open an issue at https://github.com/denisidoro/navi" echoerr "Aborting..." diff --git a/scripts/make b/scripts/make index d50a0de..787fa84 100755 --- a/scripts/make +++ b/scripts/make @@ -8,23 +8,23 @@ export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)" source "${NAVI_HOME}/scripts/install" install() { - export SOURCE_DIR="${SOURCE_DIR:-"$(get_source_dir)"}" - export BIN_DIR="${BIN_DIR:-"$(get_bin_dir)"}" - echoerr "The final binary will be available at ${BIN_DIR}/navi..." - "${NAVI_HOME}/scripts/action" release - ln -s "${NAVI_HOME}/target/tar/navi" "${BIN_DIR}/navi" + export SOURCE_DIR="${SOURCE_DIR:-"$(get_source_dir)"}" + export BIN_DIR="${BIN_DIR:-"$(get_bin_dir)"}" + echoerr "The final binary will be available at ${BIN_DIR}/navi..." + "${NAVI_HOME}/scripts/action" release + ln -s "${NAVI_HOME}/target/tar/navi" "${BIN_DIR}/navi" } uninstall() { - export SOURCE_DIR="${SOURCE_DIR:-"$(get_source_dir)"}" - rm -rf "${NAVI_HOME}/target" - rm "$SOURCE_DIR" + export SOURCE_DIR="${SOURCE_DIR:-"$(get_source_dir)"}" + rm -rf "${NAVI_HOME}/target" + rm "$SOURCE_DIR" } cmd="$1" shift case "$cmd" in - "install") install "$@" ;; - "uninstall") uninstall "$@" ;; + "install") install "$@" ;; + "uninstall") uninstall "$@" ;; esac diff --git a/src/cheat.rs b/src/cheat.rs index 60eb942..b5de854 100644 --- a/src/cheat.rs +++ b/src/cheat.rs @@ -11,6 +11,7 @@ pub struct SuggestionOpts { pub header_lines: u8, pub column: Option, pub multi: bool, + pub delimiter: Option, } pub type Value = (String, Option); @@ -27,16 +28,18 @@ fn parse_opts(text: &str) -> SuggestionOpts { let mut header_lines: u8 = 0; let mut column: Option = None; let mut multi = false; + let mut delimiter: Option = None; let mut parts = text.split(' '); while let Some(p) = parts.next() { match p { "--multi" => multi = true, - "--header" | "--header-lines" => { + "--header" | "--headers" | "--header-lines" => { header_lines = parts.next().unwrap().parse::().unwrap() } "--column" => column = Some(parts.next().unwrap().parse::().unwrap()), + "--delimiter" => delimiter = Some(parts.next().unwrap().to_string()), _ => (), } } @@ -45,6 +48,7 @@ fn parse_opts(text: &str) -> SuggestionOpts { header_lines, column, multi, + delimiter, } } @@ -132,8 +136,8 @@ pub fn read_all(config: &Config, stdin: &mut std::process::ChildStdin) -> HashMa let current_exe = filesystem::exe_path_string(); let fallback = format!( - "{}/cheats:{}/../cheats:{}/../libexec/cheats", - current_exe, current_exe, current_exe + "{path}/cheats:{path}/../cheats:{path}/../libexec/cheats", + path = current_exe ); let folders_str = config.path.as_ref().unwrap_or(&fallback); let folders = folders_str.split(':'); diff --git a/src/cmds/core.rs b/src/cmds/core.rs index b3b30b0..fe0208f 100644 --- a/src/cmds/core.rs +++ b/src/cmds/core.rs @@ -42,7 +42,7 @@ fn extract_from_selections(raw_output: &str, contains_key: bool) -> (&str, &str, } else { "enter" }; - let mut parts = lines.next().unwrap().split('\t'); + let mut parts = lines.next().unwrap().split(display::DELIMITER); parts.next(); parts.next(); parts.next(); @@ -68,10 +68,16 @@ fn prompt_with_suggestions(config: &Config, suggestion: &cheat::Value) -> String ..Default::default() }; + let mut column: Option = None; + let mut delimiter = r"\s\s+"; + if let Some(o) = &suggestion.1 { opts.multi = o.multi; opts.header_lines = o.header_lines; - opts.nth = o.column; + column = o.column; + if let Some(d) = o.delimiter.as_ref() { + delimiter = d.as_str(); + } }; let (output, _) = fzf::call(opts, |stdin| { @@ -79,7 +85,16 @@ fn prompt_with_suggestions(config: &Config, suggestion: &cheat::Value) -> String None }); - output + if let Some(c) = column { + let re = regex::Regex::new(delimiter).unwrap(); + let mut parts = re.split(output.as_str()); + for _ in 0..(c - 1) { + parts.next().unwrap(); + } + parts.next().unwrap().to_string() + } else { + output + } } fn prompt_without_suggestions(varname: &str) -> String { diff --git a/src/cmds/preview.rs b/src/cmds/preview.rs index c90b6d1..bbb0403 100644 --- a/src/cmds/preview.rs +++ b/src/cmds/preview.rs @@ -4,7 +4,7 @@ use std::process; use crate::display; fn extract_elements(argstr: &str) -> (&str, &str, &str) { - let mut parts = argstr.split('\t').skip(3); + let mut parts = argstr.split(display::DELIMITER).skip(3); let tags = parts.next().unwrap(); let comment = parts.next().unwrap(); let snippet = parts.next().unwrap(); diff --git a/src/display.rs b/src/display.rs index e4a1c30..25da1c8 100644 --- a/src/display.rs +++ b/src/display.rs @@ -5,6 +5,8 @@ static COMMENT_COLOR: color::LightCyan = color::LightCyan; static TAG_COLOR: color::LightGreen = color::LightGreen; static SNIPPET_COLOR: color::White = color::White; +pub static DELIMITER: char = '\t'; + lazy_static! { pub static ref WIDTHS: (usize, usize) = get_widths(); } @@ -48,7 +50,7 @@ pub fn format_line( comment_width: usize, ) -> String { format!( - "{tag_color}{tags_short}\t{comment_color}{comment_short}\t{snippet_color}{snippet_short}\t{tags}\t{comment}\t{snippet}\t\n", + "{tag_color}{tags_short}{delimiter}{comment_color}{comment_short}{delimiter}{snippet_color}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}\n", tags_short = limit_str(tags, tag_width), comment_short = limit_str(comment, comment_width), snippet_short = full_snippet, @@ -57,5 +59,6 @@ pub fn format_line( snippet_color = color::Fg(SNIPPET_COLOR), tags = tags, comment = comment, + delimiter = DELIMITER, snippet = &full_snippet) } diff --git a/src/fzf.rs b/src/fzf.rs index d66b641..2afb241 100644 --- a/src/fzf.rs +++ b/src/fzf.rs @@ -1,4 +1,5 @@ use crate::cheat; +use crate::display; use crate::filesystem; use std::collections::HashMap; @@ -16,7 +17,6 @@ pub struct Opts<'a> { pub multi: bool, pub copyable: bool, pub suggestions: bool, - pub nth: Option, } impl Default for Opts<'_> { @@ -32,7 +32,6 @@ impl Default for Opts<'_> { multi: false, copyable: false, suggestions: true, - nth: None, } } } @@ -49,7 +48,7 @@ where "--with-nth", "1,2,3", "--delimiter", - "\t", + display::DELIMITER.to_string().as_str(), "--ansi", "--bind", "ctrl-j:down,ctrl-k:up", @@ -87,10 +86,6 @@ where c.args(&["--prompt", &p]); } - if let Some(n) = opts.nth { - c.args(&["--nth", &n.to_string()]); - } - if opts.header_lines > 0 { c.args(&["--header-lines", format!("{}", opts.header_lines).as_str()]); }