Skip to content

Commit

Permalink
Refactor list visualization (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro committed Oct 28, 2019
1 parent 70bf907 commit a0c5a62
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 84 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -88,7 +88,7 @@ as a [shell widget](#shell-widget) with no additional setup.
> to be able to run the command interactively, you will need to do one of the following:
- Install it to /usr/bin/local (via `sudo make install`)
- Manually set your `PATH` so that navi can be found.
- Manually set `$PATH` so that navi can be found.

You can manually update your path by adding a line like this in your `.zshrc`:

Expand Down Expand Up @@ -250,6 +250,12 @@ List customization

Lists can be stylized with the [$FZF_DEFAULT_OPTS](https://github.com/junegunn/fzf) environment variable. This way, you can change the [color scheme](https://github.com/junegunn/fzf/wiki/Color-schemes), for example.

In addition:
- the `--fzf-overrides` option allows you to hide columns, for example
- the `--col-widths` option allows you to limit column widths

Please refer to `navi --help` for more details.

Related projects
----------------

Expand Down
2 changes: 1 addition & 1 deletion cheats/compression.cheat
@@ -1,4 +1,4 @@
% tar, zip, gzip, compression
% compression

# Create a tar containing files
tar cf <name>.tar <files>
Expand Down
2 changes: 1 addition & 1 deletion cheats/crontab.cheat
@@ -1,4 +1,4 @@
% crontab, scheduling
% crontab, schedule

# List cron jobs
crontab -l
Expand Down
4 changes: 2 additions & 2 deletions cheats/docker.cheat
@@ -1,4 +1,4 @@
% docker, container
% docker

# Remove an image
docker image rm <image_id>
Expand Down Expand Up @@ -47,7 +47,7 @@ $ container_id: docker ps --- --headers 1 --column 1



% docker-compose, container
% docker-compose

# Builds, (re)creates, starts, and attaches to containers for all services
docker-compose up
Expand Down
52 changes: 52 additions & 0 deletions docstring.txt
@@ -0,0 +1,52 @@
An interactive cheatsheet tool for the command-line

Usage:
navi [command] [<args>...] [options]

Commands:
search <cmd> Search for cheatsheets on online repositories
query <cmd> Pre-filter results
best <cmd> <args>... Considers the best match
widget <shell> Prints the path for the widget file to be sourced

Options:
--print Prevent script execution
--path List of :-separated paths to look for cheats
--no-interpolation Prevent argument interpolation
--no-preview Hide command preview window
--fzf-overrides Overrides for fzf commands [default: --with-nth 3,1,2 --exact]
--col-widths Set column widths [default: 15,50,0]

Environment variables:
NAVI_PATH List of :-separated paths to look for cheats
FZF_DEFAULT_OPTS Default fzf options (e.g. '--layout=reverse')

Examples:
navi # default behavior
navi --print # doesn't execute the snippet
navi --path '/some/dir:/other/dir' # uses custom cheats
navi search docker # uses online data
navi query git # filter results by "git"
navi best 'sql create db' root mydb # uses a snippet as a CLI
source "$(navi widget zsh)" # loads the zsh widget
navi --col-widths 10,50,0 # limits the first two columns's width
navi --fzf-overrides '--with-nth 1,2' # shows only the comment and tag columns
navi --fzf-overrides '--nth 1,2' # search will consider only the first two columns
navi --fzf-overrides '--no-exact' # looser search algorithm

More info:

search
http://cheat.sh is used for cheatsheet retrieval
please note that these cheatsheets may not work out of the box
always check the preview window before executing commands!

--col-widths
the number is the percentage relative to the terminal width
0 means that a column will be as wide as necessary

fzf's --with-nth
1 refers to the comment column; 2, snippet; 3, tag

full docs
please refer to the README at https://github.com/denisidoro/navi
33 changes: 0 additions & 33 deletions navi
Expand Up @@ -2,41 +2,8 @@
set -euo pipefail

export NAVI_HOME="$(cd "$(dirname "$0")" && pwd)"

source "${NAVI_HOME}/src/main.sh"

##? An interactive cheatsheet tool for the command-line
##?
##? Usage:
##? navi [command] [<args>...] [options]
##?
##? Commands:
##? search <cmd> Search for cheatsheets on online repositories
##? query <cmd> Pre-filter results
##? best <cmd> <args>... Considers the best match
##?
##? Options:
##? --print Prevent script execution
##? --path List of paths to look for cheats
##? --no-interpolation Prevent argument interpolation
##? --no-preview Hide command preview window
##?
##? Examples:
##? navi
##? navi --path '/some/folder:/another/folder'
##? navi search awk
##? navi search docker --print
##? navi query git
##? navi best 'sql create db' root mydb
##?
##? More info:
##? search
##? Queries cheatsheets from http://cheat.sh
##? Please note that these cheatsheets may not work out of the box
##? Always check the preview window before executing commands!
##? full docs
##? Please refer to the README at https://github.com/denisidoro/navi

VERSION="0.14.3"
NAVI_ENV="${NAVI_ENV:-prod}"

Expand Down
2 changes: 1 addition & 1 deletion src/arg.sh
Expand Up @@ -38,7 +38,7 @@ arg::next() {
}

arg::deserialize() {
local arg="$1"
local arg="${1:-}"

arg="${arg:1:${#arg}-2}"
echo "$arg" \
Expand Down
76 changes: 68 additions & 8 deletions src/cheat.sh
Expand Up @@ -41,15 +41,68 @@ cheat::memoized_read_all() {
| cheat::join_lines
}

# TODO: move this elsewhere
cheat::get_index() {
local -r txt="$1"
local -r ref="$2"

local -r i="$(echo "$txt" | grep "${ref}\$" | awk '{print $1}')"
echo $((i - 1))
}

# TODO: move this elsewhere
cheat::with_nth() {
grep -Eo 'with\-nth +([^ ]+)' | awk '{print $NF}'
}

cheat::prettify() {
awk 'function color(c,s) {
printf("\033[%dm%s\033[0m",30+c,s)
local -r print="$(dict::get "$OPTIONS" print)"
local -r widths="$(dict::get "$OPTIONS" col-widths | tr ',' $'\n')"
local -r numbered_with_nth="$(dict::get "$OPTIONS" fzf-overrides | cheat::with_nth | tr ',' $'\n' | str::with_line_numbers)"

if [ -n "$numbered_with_nth" ]; then
local -r comment_index="$(cheat::get_index "$numbered_with_nth" 1 2>/dev/null)"
local -r snippet_index="$(cheat::get_index "$numbered_with_nth" 2 2>/dev/null)"
local -r tag_index="$(cheat::get_index "$numbered_with_nth" 3 2>/dev/null)"
local -r comment_width="$(echo "$widths" | coll::get $comment_index 2>/dev/null || echo 0)"
local -r snippet_width="$(echo "$widths" | coll::get $snippet_index 2>/dev/null || echo 0)"
local -r tag_width="$(echo "$widths" | coll::get $tag_index 2>/dev/null || echo 0)"
local -r columns="$(ui::width)"
else
local -r comment_width=0
local -r snippet_width=0
local -r tag_width=0
local -r columns=0
fi

awk \
-v COMMENT_MAX=$((columns * comment_width / 100)) \
-v SNIPPET_MAX=$((columns * snippet_width / 100)) \
-v TAG_MAX=$((columns * tag_width / 100)) \
-v SEP="$ESCAPE_CHAR_3" \
'function color(c,s,max) {
if (max > 0 && length(s) > max) {
s=substr(s, 0, max)
s=s"…"
}
printf("\033[%dm%s", c, s)
}
/^%/ { tags=" ["substr($0, 3)"]"; next }
/^#/ { print color(4, $0) color(60, tags); next }
/^%/ { tags=substr($0, 3); next }
/^#/ { comment=substr($0, 3); next }
/^\$/ { next }
NF { print color(7, $0) color(60, tags); next }'
BEGIN { ORS="" }
NF {
print color(34, comment, COMMENT_MAX)
print color(0, SEP, 0)
print color(37, $0, SNIPPET_MAX)
print color(0, SEP, 0)
print color(90, tags, TAG_MAX);
print color(0, SEP, 0)
print color(90, "\033", 0);
print "\n"
next
}'
}

cheat::until_percentage() {
Expand All @@ -60,13 +113,20 @@ cheat::until_percentage() {
{ print $0 }'
}

cheat::from_tags() {
local -r cheats="$1"
local -r tags="$2"

echo "$cheats" \
| grep "% ${tags}" -A99999 \
| cheat::until_percentage
}

cheat::from_selection() {
local -r cheats="$1"
local -r selection="$2"

local -r tags="$(dict::get "$selection" tags)"

echo "$cheats" \
| grep "% ${tags}" -A99999 \
| cheat::until_percentage
cheat::from_tags "$cheats" "$tags"
}
6 changes: 3 additions & 3 deletions src/main.sh
Expand Up @@ -26,7 +26,7 @@ handler::main() {

local -r interpolation="$(dict::get "$OPTIONS" interpolation)"

local cmd="$(selection::cmd "$selection" "$cheat")"
local cmd="$(selection::snippet "$selection")"
local result arg value

local i=0
Expand All @@ -50,10 +50,10 @@ handler::main() {

handler::preview() {
local -r query="$1"
local -r selection="$(echo "$query" | selection::dict)"
local -r cheats="$(cheat::memoized_read_all)"
local -r selection="$(echo "$query" | selection::dict "$cheats")"
local -r cheat="$(cheat::from_selection "$cheats" "$selection")"
[ -n "$cheat" ] && selection::cmd_or_comment "$selection" "$cheat" | cmd::unescape
[ -n "$cheat" ] && ui::print_preview "$selection"
}

handler::help() {
Expand Down
17 changes: 14 additions & 3 deletions src/opts.sh
Expand Up @@ -2,8 +2,8 @@
set -euo pipefail

opts::extract_help() {
local -r file="$1"
grep "^##?" "$file" | cut -c 5-
local -r file="${NAVI_HOME}/docstring.txt"
cat "$file"
}

opts::eval() {
Expand All @@ -17,6 +17,8 @@ opts::eval() {
local best=false
local query=""
local values=""
local col_widths="15,50,0"
local fzf_overrides="--with-nth 3,1,2 --exact"

case "${1:-}" in
--version|version) entry_point="version"; shift ;;
Expand All @@ -39,14 +41,21 @@ opts::eval() {
search) query="$arg"; wait_for=""; path="${path}:$(search::full_path "$query")"; continue ;;
query|best) query="$arg"; wait_for=""; continue ;;
widget) SH="$arg"; wait_for=""; continue ;;
col-widths) col_widths="$(echo "$arg" | xargs | tr ' ' ',')"; wait_for=""; continue ;;
fzf-overrides) fzf_overrides="$arg" ; wait_for=""; continue ;;
esac

case $arg in
--print) print=true ;;
--no-interpolation) interpolation=false ;;
--interpolation) interpolation=true ;;
--no-preview) preview=false ;;
--preview) preview=true ;;
--path|--dir) wait_for="path" ;;
--no-autoselect) autoselect=false ;;
--autoselect) autoselect=true ;;
--col-widths) wait_for="col-widths" ;;
--fzf-overrides) wait_for="fzf-overrides" ;;
*) values="$(echo "$values" | coll::add "$arg")" ;;
esac
done
Expand All @@ -59,7 +68,9 @@ opts::eval() {
autoselect "$autoselect" \
query "$query" \
best "$best" \
values "$values")"
values "$values" \
fzf-overrides "$fzf_overrides" \
col-widths "$col_widths")"

export NAVI_PATH="$path"
}

0 comments on commit a0c5a62

Please sign in to comment.