Skip to content

Commit 3cd963c

Browse files
committed
Add debug option. Plugins!
1 parent ccd156f commit 3cd963c

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ selection tools.
77

88
What is an "interactive line selection tool"? It's a kind of program
99
that reads text input then presents an interactive menu for the user to
10-
select one line.
10+
select one line. Some line selection tools run only in the terminal and
11+
others are graphical - cz uses a tool appropriate for the situation.
1112

12-
Some line selection tools run only in the terminal and others are
13-
graphical - cz uses a tool appropriate for the situation.
14-
15-
Cz implements a plugin system. Each plugin defines a set of lines which
16-
to select and templates to use the selection.
13+
Cz implements a plugin system. Each plugin defines lines from which to
14+
select, an output template, and a command template. Creating new plugins
15+
is quick and painless in any programming language.
1716

1817
Included are over 250 plugins covering a variety of use cases. For
1918
example with some of the included plugins you can select from:

cz

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ EOF
661661

662662
cz() {
663663
# version
664-
local ver="0.8.4"
664+
local ver="0.8.5"
665665
# usage
666666
local hows="" use=""
667667
read -r -d '' use <<EOF
@@ -701,14 +701,17 @@ OPTIONS
701701
-y : Use a terminal line selection tool.
702702
-z TOOL : Use the given line selection tool.
703703
704+
These options control debugging features:
705+
-m : Print some debygging information.
706+
704707
TOOLS
705708
The following interactive line selection tools are supported:
706709
choose, dmenu, fzf, fzy, iselect, pick, pipedial, rofi, selecta, sentaku,
707-
slmenu, and vis-menu.
710+
slmenu, vis-menu, and zenity.
708711
709712
PLUGINS
710713
Plugins use cz for an application specific task. Each plugin defines input
711-
lines and delimiter or template options.
714+
lines, delimiter, and template options.
712715
Run 'cz -l' to list plugins and 'cz -h PLUGIN' or 'cz help' for help text.
713716
All commands starting with 'cz_' are considered plugins.
714717
@@ -786,15 +789,16 @@ EOF
786789
local mode="${_CZ_MODE}" # string | indicator for program mode
787790
local help="${_CZ_HELP}" # string | indicator for help requested
788791
local dlm="${_CZ_DELIM:-$IFS}" # string | delimeter to split selected line
792+
local dbg="${_CZ_DEBUG}" # boolean | whether or not write debug info
789793
# set option defaults
790-
local inp="/dev/stdin" # string | file from which to select a line
791-
local buf=0 # boolean | require buffered stdin for command
792-
local run="" # string | program mode implied by another option
793-
local nul=0 # boolean | whether to read null separated lines
794-
local clr=0 # boolean | whether to refresh cached input lines
794+
local inp="/dev/stdin" # string | file from which to select a line
795+
local buf=0 # boolean | require buffered stdin for command
796+
local run="" # string | program mode implied by another option
797+
local nul=0 # boolean | whether to read null separated lines
798+
local clr=0 # boolean | whether to refresh cached input lines
795799
# accept options
796800
local opt OPTIND OPTARG
797-
while getopts ":cd:e:f:ghHi:lkopqrstuvxyz:0" opt; do
801+
while getopts ":cd:e:f:ghHi:klmopqrstuvxyz:0" opt; do
798802
case "$opt" in
799803
c) # clear hold caches
800804
clr=1
@@ -831,6 +835,10 @@ EOF
831835
return 2
832836
fi
833837
;;
838+
k) # list supported tools
839+
printf "%s\\n" "$exes"
840+
return 0
841+
;;
834842
l) # list available plugins
835843
while read -r f; do
836844
if [[ "$f" == cz_* ]]; then
@@ -839,9 +847,8 @@ EOF
839847
done < <(compgen -c) | sort
840848
return 0
841849
;;
842-
k) # list supported tools
843-
printf "%s\\n" "$exes"
844-
return 0
850+
m) # mention some debugging information
851+
dbg=1
845852
;;
846853
o) # just print lines without interactive selection
847854
mode=-1
@@ -940,6 +947,7 @@ EOF
940947
shift "$word"
941948
# run the plugin with override environment variables
942949
_CZ_DELIM="$dlm" \
950+
_CZ_DEBUG="$dbg" \
943951
_CZ_FIELDS="$fld" \
944952
CZ_GUI="${gfx}:${app}" \
945953
_CZ_HELP="$help" \
@@ -1053,6 +1061,10 @@ EOF
10531061
if ((nul)); then
10541062
out=$(eval printf "%s" "$out")
10551063
fi
1064+
# maybe print the selection
1065+
if ((dbg)); then
1066+
printf "> %s\\n" "$out" >&2
1067+
fi
10561068
# tokenize selected line for use in templates
10571069
local cmd=""
10581070
local toks=()
@@ -1074,20 +1086,29 @@ EOF
10741086
printf "Failed to render template: %s\\n" "$fld" >&2
10751087
return 5
10761088
fi
1089+
if ((dbg)); then
1090+
printf "# %s ==> %s\\n" "$fld" "$out" >&2
1091+
fi
10771092
printf "%s\\n" "$out"
10781093
;;
10791094
5) # print some fields extracted from the line
10801095
if ! out="$(nth -p -- "$fld" "${toks[@]}" 2>/dev/null)"; then
10811096
printf "Failed to render template: %s\\n" "$fld" >&2
10821097
return 5
10831098
fi
1099+
if ((dbg)); then
1100+
printf "# %s ==> %s\\n" "$fld" "$out" >&2
1101+
fi
10841102
printf "%s\\n" "$out"
10851103
;;
10861104
2) # template the command string then run it
10871105
if ! cmd="$(nth -q -- "$tpl" "${toks[@]}" 2>/dev/null)"; then
10881106
printf "Failed to parse template: %s\\n" "$tpl" >&2
10891107
return 5
10901108
fi
1109+
if ((dbg)); then
1110+
printf "# %s\\n" "$cmd" >&2
1111+
fi
10911112
unset -v _CZ_DELIM _CZ_FIELDS _CZ_HELP _CZ_MODE _CZ_TEMPLATE HOLD_CLEAR
10921113
# eval the command maybe passing through stdin or the terminal
10931114
if ((${#stdin})); then
@@ -1103,13 +1124,19 @@ EOF
11031124
printf "Failed to render template.\\n%s\\n" "$tpl" >&2
11041125
return 4
11051126
fi
1127+
if ((dbg)); then
1128+
printf "# %s ==> %s\\n" "$fld" "$out" >&2
1129+
fi
11061130
printf "%s\\n" "$cmd"
11071131
;;
11081132
4) # render the command template unquoted
11091133
if ! cmd="$(nth -p "$tpl" "${toks[@]}")"; then
11101134
printf "Failed to render template.\\n%s\\n" "$tpl" >&2
11111135
return 4
11121136
fi
1137+
if ((dbg)); then
1138+
printf "# %s ==> %s\\n" "$fld" "$out" >&2
1139+
fi
11131140
printf "%s\\n" "$cmd"
11141141
;;
11151142
esac
@@ -1125,7 +1152,7 @@ function _cz() {
11251152
# cz has some options
11261153
local opt="" opts
11271154
declare -A opts
1128-
for opt in -{d,e,f,h,i,l,k,o,p,q,r,s,t,u,v,x,y,z,0}; do opts["$opt"]=0; done
1155+
for opt in -{d,e,f,h,i,l,k,m,o,p,q,r,s,t,u,v,x,y,z,0}; do opts["$opt"]=0; done
11291156
# scan all previous words
11301157
local base="" last="" word="" i=0
11311158
for ((i=1; i < "$COMP_CWORD"; i++)); do
@@ -1668,13 +1695,23 @@ EOF
16681695
cz -e 'cd {0}' find dir "$d"
16691696
}
16701697
1698+
cz_dict_database() {
1699+
{ hep "$_CZ_HELP" && return; } <<EOF
1700+
cz dict database [QUERY]
1701+
Select a dict server database.
1702+
EOF
1703+
req dict sed || return 5
1704+
local qry="${1}"
1705+
cz -e "dict -d {0} $qry" -f 0 -i <(dict -D | sed 1d)
1706+
}
1707+
16711708
cz_dict_strategy() {
16721709
{ hep "$_CZ_HELP" && return; } <<EOF
16731710
cz dict strategy [QUERY]
16741711
Select a dict server search strategy.
16751712
EOF
16761713
req dict sed || return 5
1677-
local qry="${1:-}"
1714+
local qry="${1}"
16781715
cz -e "dict -s {0} $qry" -f 0 -i <(dict -S | sed 1d)
16791716
}
16801717
@@ -1684,8 +1721,11 @@ cz dict word [QUERY]
16841721
Select a word from a dict server and print its definition.
16851722
EOF
16861723
req dict || return 5
1687-
local qry="${1:-.*}"
1688-
cz -f 3: -e 'dict -- "{3:@P}"' -i <(dict -m -f -s re "$qry")
1724+
local qry="${1}"
1725+
1726+
cz -f 3: -e 'dict -- "{3:@P}"' \
1727+
-i <( if [ -z "$qry" ]; then hold -ep -t 86400 -- dict -m -f -s re "";
1728+
else dict -m -f -s re "$qry"; fi)
16891729
}
16901730
16911731
cz_dir() {
@@ -1836,6 +1876,30 @@ EOF
18361876
done)
18371877
}
18381878
1879+
cz_radiogarden_place() {
1880+
{ hep "$_CZ_HELP" && return; } <<EOF
1881+
cz radiogarden place
1882+
Select a place known to radio.garden.
1883+
go install github.com/jonasrmichel/radio-garden-go/radio-garden@latest
1884+
EOF
1885+
req radio-garden || return 5
1886+
cz -f 0 -i <(hold -ep -t 86400 -- radio-garden places \
1887+
| jq -r '.data.list[] | [.id, .country, .title] | @tsv')
1888+
}
1889+
1890+
cz_radiogarden_station() {
1891+
{ hep "$_CZ_HELP" && return; } <<EOF
1892+
cz radiogarden station [PLACE-ID]
1893+
Select a station known to radio.garden near the given place.
1894+
go install github.com/jonasrmichel/radio-garden-go/radio-garden@latest
1895+
EOF
1896+
req radio-garden || return 5
1897+
local place="${1:-$(insulate ^_CZ cz -u radiogarden place)}"
1898+
cz -e 'radio-garden station stream -i {0}' -f 0 \
1899+
-i <(radio-garden stations -i "$place" \
1900+
| jq -r '.data.content[0].items[] | [(.href | split("/")[-1]), .title] | @tsv')
1901+
}
1902+
18391903
cz_rofi_theme() {
18401904
{ hep "$_CZ_HELP" && return; } <<EOF
18411905
cz rofi theme
@@ -3029,7 +3093,6 @@ EOF
30293093
| jq -r '.data[] | select(.type == "oracle_cards") | .download_uri'); then
30303094
return 2
30313095
fi
3032-
30333096
local data="" menu=""
30343097
if ! data=$(hold -e -n scryfalldata -t $((86400 * 7)) curl -sS "$url"); then
30353098
return 2

www/index.org

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
** About
77
Cz is a bash script that provides a common interface to interactive line selection tools.
88

9-
What is an "interactive line selection tool"? It's a kind of program that reads text input then presents an interactive menu for the user to select one line.
9+
What is an "interactive line selection tool"? It's a kind of program that reads text input then presents an interactive menu for the user to select one line. Some line selection tools run only in the terminal and others are graphical - cz uses a tool appropriate for the situation.
1010

11-
Some line selection tools run only in the terminal and others are graphical - cz uses a tool appropriate for the situation.
12-
13-
Cz implements a plugin system. Each plugin defines a set of lines which to select and templates to use the selection.
11+
Cz implements a plugin system. Each plugin defines lines from which to select, an output template, and a command template. Creating new plugins is quick and painless in any programming language.
1412

1513
Included are over 250 plugins covering a variety of use cases. For example with some of the included plugins you can select from:
1614

0 commit comments

Comments
 (0)