Commit 4c292ea2 authored by Sebastian Gniazdowski's avatar Sebastian Gniazdowski

Initial commit of Zsh Navigation Tools

parent 9c08641d
These are skeletons, configuration is read from ~/.config/znt/*
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
# local active_text=underline
# Hotlist
local hotlist
hotlist=(
/usr/share/zsh/site-functions
/usr/share/zsh
/usr/local/share/zsh/site-functions
/usr/local/share/zsh
/usr/local/bin
/usr/lib
)
# Suppress adding (to directory stack) directories visited by n-cd
# Value 0 is the default (directories will be added to dirstack)
local NCD_DONT_PUSHD=0
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
# local active_text=underline
# Colorize last segments of the paths
# (#s) is ^, (#e) is $, # is *, ## is + (comparing to regex)
local NLIST_COLORING_PATTERN="[a-zA-Z0-9 ._-]##/#(#e)"
local NLIST_COLORING_COLOR=$'\x1b[00;33m'
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
# local active_text=underline
# (#s) is ^, (#e) is $, # is *, ## is + (comparing to regex)
local NLIST_COLORING_PATTERN="[a-zA-Z0-9_]##"
local NLIST_COLORING_MATCH_MULTIPLE=0
# Which editor to use, zed or vared
# vared is the default
local feditor="zed"
# local feditor="vared"
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
# local active_text=underline
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
local active_text=underline
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
# local active_text=underline
# Colorize first number column and last path segment
# This doesn't cover scripts named "[0-9]## *", which should be very rare
# (#s) is ^, (#e) is $, # is *, ## is + (comparing to regex)
# | is alternative, but only in ()
local NLIST_COLORING_PATTERN="((#s) #[0-9]## |[[][^]]#](#e)|[^ 0-9/?\\\\][^/\\\\]#(#e)|[^ /\\\\]##[^0-9/\\\\ ]##[^/\\\\]#(#e))"
local NLIST_COLORING_COLOR=$'\x1b[00;33m'
local NLIST_COLORING_MATCH_MULTIPLE=1
# Should the list (text, borders) be drawn in bold
# Value 1 is the default
local bold=1
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
# local active_text=underline
# How should be current element of the list drawn. Possible values: reverse,
# underline. Default (without option set) is reverse
# On Linux virtual terminal this will be enforced to reverse (because of poor
# underline support on that terminal)
# local active_text=underline
This diff is collapsed.
# Zsh Navigation Tools
http://imageshack.com/a/img633/7967/ps6rKR.png
A tool generating a selectable curses-based list of elements that has access to
current Zsh session, i.e. has broad capabilities to work together with it.
That's n-list. The files n-cd, n-env, n-kill, etc. are applications of
the tool. Feature highlights include incremental multi-word searching, ANSI
coloring, unique mode, horizontal scroll, non-selectable elements, grepping and
various integrations with Zsh.
## History Widget
To have n-history as the incremental searcher bound to Ctrl-R copy znt-*
files into the */site-functions dir (unless you use Oh My Zsh) and
add:
autoload znt-history-widget
zle -N znt-history-widget
bindkey "^R" znt-history-widget
to .zshrc. This is done automatically when using Oh My Zsh. Two other
widgets exist, znt-cd-widget and znt-kill-widget, they can be too assigned
to key combinations:
zle -N znt-cd-widget
bindkey "^T" znt-cd-widget
zle -N znt-kill-widget
bindkey "^Y" znt-kill-widget
## Introduction
The tools are:
- n-aliases - browses aliases, relegates editing to vared
- n-cd - browses dirstack and bookmarked directories, allows to enter selected directory
- n-functions - browses functions, relegates editing to zed or vared
- n-history - browses history, allows to edit and run commands from it
- n-kill - browses processes list, allows to send signal to selected process
- n-env - browses environment, relegates editing to vared
- n-options - browses options, allows to toggle their state
- n-panelize - loads output of given command into the list for browsing
All tools support horizontal scroll with <,>, {,}, h,l or left and right
cursors. Other keys are:
- [,] - jump directory bookmarks in n-cd and typical signals in n-kill
- Ctrl-d, Ctrl-u - half page up or down
- Ctrl-p, Ctrl-n - previous and next (also done with vim's j,k)
- Ctrl-l - redraw of whole display
- g, G - beginning and end of the list
- Ctrl-o, o - enter uniq mode (no duplicate lines)
- / - start incremental search
- Enter - finish incremental search, retaining filter
- Esc - exit incremental search, clearing filter
- Ctrl-w (in incremental search) - delete whole word
- Ctrl-k (in incremental search) - delete whole line
## Programming
The function n-list is used as follows:
n-list {element1} [element2] ... [elementN]
This is all that is needed to be done to have the features like ANSI coloring,
incremental multi-word search, unique mode, horizontal scroll, non-selectable
elements (grepping is done outside n-list, see the tools for how it can be
done). To set up non-selectable entries add their indices into array
NLIST_NONSELECTABLE_ELEMENTS:
typeset -a NLIST_NONSELECTABLE_ELEMENTS
NLIST_NONSELECTABLE_ELEMENTS=( 1 )
Result is stored as $reply[REPLY] ($ isn't needed before REPLY because
of arithmetic context inside []). The returned array might be different from
input arguments as n-list can process them via incremental search or uniq
mode. $REPLY is the index in that possibly processed array. If $REPLY
equals -1 it means that no selection have been made (user quitted via q
key).
To set up entries that can be jumped to with [,] keys add their indices to
NLIST_HOP_INDEXES array:
typeset -a NLIST_HOP_INDEXES
NLIST_HOP_INDEXES=( 1 10 )
n-list can automatically colorize entries according to a Zsh pattern.
Following example will colorize all numbers with blue:
local NLIST_COLORING_PATTERN="[0-9]##"
local NLIST_COLORING_COLOR=$'\x1b[00;34m'
local NLIST_COLORING_END_COLOR=$'\x1b[0m'
local NLIST_COLORING_MATCH_MULTIPLE=1
n-list "This is a number 123" "This line too has a number: 456"
Blue is the default color, it doesn't have to be set. See zshexpn man page
for more information on Zsh patterns. Briefly, comparing to regular
expressions, (#s) is ^, (#e) is $, # is *, ## is +. Alternative
will work when in parenthesis, i.e. (a|b). BTW by using this method you can
colorize output of the tools, via their config files (check out e.g. n-cd.conf,
it uses this).
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-aliases` to .zshrc
#
# This function allows to choose an alias for edition with vared
#
# Uses n-list
emulate -L zsh
setopt extendedglob
zmodload zsh/curses
zmodload zsh/parameter
local IFS="
"
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-aliases.conf ] && . ~/.config/znt/n-aliases.conf
local list
local selected
NLIST_REMEMBER_STATE=0
list=( "${(@k)aliases}" )
list=( "${(@M)list:#(#i)*$1*}" )
local NLIST_GREP_STRING="$1"
if [ "$#list" -eq 0 ]; then
echo "No matching aliases"
return 1
fi
list=( "${(@i)list}" )
n-list "$list[@]"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
echo "Editing \`$selected':"
print -rs "vared aliases\\[$selected\\]"
vared aliases\[$selected\]
fi
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-cd` to .zshrc
#
# This function allows to choose a directory from pushd stack
#
# Uses n-list
emulate -L zsh
setopt extendedglob pushdignoredups
zmodload zsh/curses
local IFS="
"
# Unset before configuration is read
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-cd.conf ] && . ~/.config/znt/n-cd.conf
local list
local selected
NLIST_REMEMBER_STATE=0
list=( `dirs -p` )
list=( "${(@M)list:#(#i)*$1*}" )
local NLIST_GREP_STRING="$1"
[ "$#list" -eq 0 ] && echo "No matching directories"
if [ "$#hotlist" -ge 1 ]; then
typeset -a NLIST_NONSELECTABLE_ELEMENTS NLIST_HOP_INDEXES
local tmp_list_size="$#list"
NLIST_NONSELECTABLE_ELEMENTS=( $(( tmp_list_size+1 )) $(( tmp_list_size+2 )) )
list=( "$list[@]" "" $'\x1b[00;31m'"Hotlist"$'\x1b[00;00m': "$hotlist[@]" )
(( tmp_list_size+=3 ))
local middle_hop=$(( (tmp_list_size+$#list) / 2 ))
[[ "$middle_hop" -eq $tmp_list_size || "$middle_hop" -eq $#list ]] && middle_hop=""
[ "$tmp_list_size" -eq $#list ] && tmp_list_size=""
NLIST_HOP_INDEXES=( 1 $tmp_list_size $middle_hop $#list )
else
[ "$#list" -eq 0 ] && return 1
fi
n-list "${list[@]}"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
selected="${selected/#\~/$HOME}"
(( NCD_DONT_PUSHD )) && setopt NO_AUTO_PUSHD
cd "$selected"
(( NCD_DONT_PUSHD )) && setopt AUTO_PUSHD
# ZLE?
if [ "${(t)CURSOR}" = "integer-local-special" ]; then
zle -M "You have selected $selected"
else
echo "You have selected $selected"
fi
else
[ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
fi
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-env` to .zshrc
#
# This function allows to choose an environment variable
# for edition with vared
#
# Uses n-list
emulate -L zsh
setopt extendedglob
unsetopt equals
zmodload zsh/curses
local IFS="
"
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-env.conf ] && . ~/.config/znt/n-env.conf
local list
local selected
NLIST_REMEMBER_STATE=0
list=( `env` )
list=( "${(@M)list:#(#i)*$1*}" )
local NLIST_GREP_STRING="$1"
if [ "$#list" -eq 0 ]; then
echo "No matching variables"
return 1
fi
list=( "${(@i)list}" )
n-list "$list[@]"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
selected="${selected%%=*}"
echo "Editing \`$selected':"
print -rs "vared \"$selected\""
vared "$selected"
fi
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-functions` to .zshrc
#
# This function allows to choose a function for edition with vared
#
# Uses n-list
emulate -L zsh
setopt extendedglob
zmodload zsh/curses
zmodload zsh/parameter
local IFS="
"
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-functions.conf ] && . ~/.config/znt/n-functions.conf
local list
local selected
NLIST_REMEMBER_STATE=0
list=( "${(@k)functions}" )
list=( "${(@M)list:#(#i)*$1*}" )
local NLIST_GREP_STRING="$1"
if [ "$#list" -eq 0 ]; then
echo "No matching functions"
return 1
fi
list=( "${(@i)list}" )
n-list "$list[@]"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
if [ "$feditor" = "zed" ]; then
echo "Editing \`$selected' (ESC ZZ or Ctrl-x-w to finish):"
autoload zed
print -rs "zed -f -- \"$selected\""
zed -f -- "$selected"
else
echo "Editing \`$selected':"
print -rs "vared functions\\[$selected\\]"
vared functions\[$selected\]
fi
fi
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-history` to .zshrc
#
# This function allows to browse Z shell's history and use the
# entries
#
# Uses n-list
emulate -L zsh
setopt extendedglob
zmodload zsh/curses
local IFS="
"
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-history.conf ] && . ~/.config/znt/n-history.conf
local list
local selected
NLIST_REMEMBER_STATE=0
list=( `builtin history -rn 1` )
list=( "${(@M)list:#(#i)*$1*}" )
local NLIST_GREP_STRING="$1"
if [ "$#list" -eq 0 ]; then
echo "No matching history entries"
return 1
fi
n-list "${list[@]}"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
# ZLE?
if [ "${(t)CURSOR}" = "integer-local-special" ]; then
zle redisplay
zle kill-whole-line
zle -U "$selected"
else
print -zr "$selected"
fi
else
[ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
fi
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-kill` to .zshrc
#
# This function allows to choose a process and a signal to send to it
#
# Uses n-list
emulate -L zsh
setopt extendedglob
zmodload zsh/curses
local IFS="
"
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-kill.conf ] && . ~/.config/znt/n-kill.conf
typeset -A signals
signals=(
1 "1 - HUP"
2 "2 - INT"
3 "3 - QUIT"
6 "6 - ABRT"
9 "9 - KILL"
14 "14 - ALRM"
15 "15 - TERM"
17 "17 - STOP"
19 "19 - CONT"
)
local list
local selected
local signal
local -a signal_names
local title
NLIST_REMEMBER_STATE=0
typeset -a NLIST_NONSELECTABLE_ELEMENTS
NLIST_NONSELECTABLE_ELEMENTS=( 1 )
type ps 2>/dev/null 1>&2 || { echo >&2 "Error: \`ps' not found"; return 1 }
case "$(uname)" in
CYGWIN*) list=( `command ps -Wa` ) ;;
*) list=( `command ps -o pid,uid,command -A` ) ;;
esac
# Ask of PID
title=$'\x1b[00;31m'"${list[1]}"$'\x1b[00;00m\0'
shift list
list=( "$title" "${(@M)list:#(#i)*$1*}" )
local NLIST_GREP_STRING="$1"
if [ "$#list" -eq 1 ]; then
echo "No matching processes"
return 1
fi
n-list "$list[@]"
# Got answer? (could be Ctrl-C or 'q')
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
selected="${selected## #}"
pid="${selected%% *}"
# Now ask of signal
signal_names=( ${(vin)signals} )
typeset -a NLIST_HOP_INDEXES
NLIST_HOP_INDEXES=( 3 6 8 )
unset NLIST_COLORING_PATTERN
n-list $'\x1b[00;31mSelect signal:\x1b[00;00m' "$signal_names[@]"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
signal="${(k)signals[(r)$selected]}"
# ZLE?
if [ "${(t)CURSOR}" = "integer-local-special" ]; then
zle redisplay
zle kill-whole-line
zle -U "kill -$signal $pid"
else
print -zr "kill -$signal $pid"
fi
else
[ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
fi
else
[ "${(t)CURSOR}" = "integer-local-special" ] && zle redisplay
fi
# vim: set filetype=zsh:
This diff is collapsed.
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-list-draw` to .zshrc
#
# This is an internal function not for direct use
emulate -L zsh
zmodload zsh/curses
setopt typesetsilent extendedglob
_nlist_print_with_ansi() {
local win="$1" text="$2" out col chunk Xout
integer text_offset="$3" max_text_len="$4" text_len=0 no_match=0 nochunk_text_len to_skip_from_chunk to_chop_off_from_chunk before_len
# 1 - non-escaped text, 2 - first number in the escaped text, with ;
# 3 - second number, 4 - text after whole escape text
typeset -a c
c=( black red green yellow blue magenta cyan white )
while [[ -n "$text" && "$no_match" -eq 0 ]]; do
if [[ "$text" = (#b)([^$'\x1b']#)$'\x1b'\[([0-9](#c0,2))(#B)(\;|)(#b)([0-9](#c0,2))m(*) ]]; then
# Text for further processing
text="$match[4]"
# Text chunk to output now
out="$match[1]"
# Save color
col="$match[2]"
(( match[3] >= 30 && match[3] <= 37 )) && col="$match[3]"
else
out="$text"
no_match=1
fi
if [ -n "$out" ]; then
################ Expand tabs ################
chunk="$out"
before_len="$text_len"
Xout=""
while [ -n "$chunk" ]; do
[[ "$chunk" = (#b)([^$'\t']#)$'\t'(*) ]] && {
(( all_text_len=((before_len+${#match[1]})/8+1)*8 ))
Xout+="${(r:all_text_len-before_len:: :)match[1]}"
before_len+=all_text_len-before_len
chunk="$match[2]"
} || {
Xout+="$chunk"
break
}
done
#############################################
# Input text length without the current chunk
nochunk_text_len=text_len
# Input text length up to current chunk
text_len+="$#Xout"
# Should start displaying with this chunk?
# I.e. stop skipping left part of the input text?
if (( text_len > text_offset )); then
to_skip_from_chunk=text_offset-nochunk_text_len
# LEFT - is chunk off the left skip boundary? +1 for 1-based index in string
(( to_skip_from_chunk > 0 )) && Xout="${Xout[to_skip_from_chunk+1,-1]}"
# RIGHT - is text off the screen?
if (( text_len-text_offset > max_text_len )); then
to_chop_off_from_chunk=0+(text_len-text_offset)-max_text_len
Xout="${Xout[1,-to_chop_off_from_chunk-1]}"
fi
[ -n "$Xout" ] && zcurses string "$win" "$Xout"
fi
fi
if (( no_match == 0 )); then
if (( col >= 30 && col <= 37 )); then
zcurses attr "$win" $c[col-29]/black
elif [[ "$col" -eq 0 ]]; then
zcurses attr "$win" white/black
fi
fi
done
}
integer highlight="$1"
integer page_height="$2"
integer page_width="$3"
local y_offset="$4"
local x_offset="$5"
local text_offset="$6"
local win="$7"
shift 7
integer max_text_len=page_width-x_offset
[ "$bold" = "0" ] && bold="" || bold="+bold"
[[ "$active_text" = "underline" || "$active_text" = "reverse" ]] || active_text="reverse"
# With Linux terminal underline won't work properly
[ "$TERM" = "linux" ] && active_text="reverse"
integer max_idx=page_height
integer end_idx=max_idx
[ "$end_idx" -gt "$#" ] && end_idx="$#"
integer y=y_offset
zcurses attr "$win" $bold white/black
integer i text_len
local text
for (( i=1; i<=end_idx; i++ )); do
zcurses move "$win" $y "$x_offset"
[ "$i" = "$highlight" ] && zcurses attr "$win" +"$active_text"
_nlist_print_with_ansi "$win" "$@[i]" "$text_offset" "$max_text_len"
zcurses clear "$win" eol
[ "$i" = "$highlight" ] && zcurses attr "$win" -"$active_text"
y+=1
done
if [ "$end_idx" -lt "$max_idx" ]; then
zcurses move "$win" $y "$x_offset"
zcurses clear "$win" eol
fi
zcurses attr "$win" white/black
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-list-input` to .zshrc
#
# This is an internal function not for direct use
emulate -L zsh
zmodload zsh/curses
setopt typesetsilent
# Compute first to show index
_nlist_compute_first_to_show_idx() {
from_what_idx_list_is_shown=0+((current_idx-1)/page_height)*page_height+1
}
typeset -ga reply
reply=( -1 '' )
integer current_idx="$1"
integer from_what_idx_list_is_shown="$2"
integer page_height="$3"
integer page_width="$4"
integer last_element="$5"
integer hscroll="$6"
local key="$7"
integer search="$8"
local buffer="$9"
integer uniq_mode="$10"
#
# Listening for input
#
if [ "$search" = "0" ]; then
case "$key" in
(UP|k|$'\C-P')
# Are there any elements before the current one?
[ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
_nlist_compute_first_to_show_idx
;;
(DOWN|j|$'\C-N')
# Are there any elements after the current one?
[ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
_nlist_compute_first_to_show_idx
;;
(PPAGE)
current_idx=current_idx-page_height
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
(NPAGE|" ")
current_idx=current_idx+page_height
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
(HOME|g)
current_idx=1
_nlist_compute_first_to_show_idx
;;
(END|G)
current_idx=last_element
_nlist_compute_first_to_show_idx
;;
($'\n')
# Is that element selectable?
# Check for this only when there is no search
if [[ "$NLIST_SEARCH_BUFFER" != "" || "$NLIST_IS_UNIQ_MODE" -eq 1 ||
${NLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
then
# Save current element in the result variable
reply=( $current_idx SELECT )
fi
;;
(q)
reply=( -1 QUIT )
;;
(/)
search=1
_nlist_cursor_visibility 1
;;
($'\t')
reply=( $current_idx LEAVE )
;;
($'\C-L')
reply=( -1 REDRAW )
;;
(\])
[[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
[ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
for idx in "${(n)NLIST_HOP_INDEXES[@]}"; do
if [ "$idx" -gt "$current_idx" ]; then
current_idx=$idx
_nlist_compute_first_to_show_idx
break
fi
done
;;
(\[)
[[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
[ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
for idx in "${(nO)NLIST_HOP_INDEXES[@]}"; do
if [ "$idx" -lt "$current_idx" ]; then
current_idx=$idx
_nlist_compute_first_to_show_idx
break
fi
done
;;
('<'|'{'|LEFT|'h')
hscroll=hscroll-7
[ "$hscroll" -lt 0 ] && hscroll=0
;;
('>'|'}'|RIGHT|'l')
hscroll+=7
;;
($'\E')
buffer=""
;;
(o|$'\C-O')
uniq_mode=1-uniq_mode
;;
(*)
;;
esac
else
case "$key" in
($'\n')
search=0
_nlist_cursor_visibility 0
;;
($'\C-L')
reply=( -1 REDRAW )
;;
#
# Slightly limited navigation
#
(UP|$'\C-P')
[ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
_nlist_compute_first_to_show_idx
;;
(DOWN|$'\C-N')
[ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
_nlist_compute_first_to_show_idx
;;
(PPAGE)
current_idx=current_idx-page_height
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
(NPAGE)
current_idx=current_idx+page_height
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
(HOME)
current_idx=1
_nlist_compute_first_to_show_idx
;;
(END)
current_idx=last_element
_nlist_compute_first_to_show_idx
;;
(LEFT)
hscroll=hscroll-7
[ "$hscroll" -lt 0 ] && hscroll=0
;;
(RIGHT)
hscroll+=7
;;
(F1|F2|F3|F4|F5|F6|F7|F8|F9|F10)
# ignore
;;
#
# The input
#
($'\b'|$'\C-?'|BACKSPACE)
buffer="${buffer%?}"
;;
($'\C-W')
[ "$buffer" = "${buffer% *}" ] && buffer="" || buffer="${buffer% *}"
;;
($'\C-K')
buffer=""
;;
($'\E')
buffer=""
search=0
_nlist_cursor_visibility 0
;;
($'\C-O')
uniq_mode=1-uniq_mode
;;
(*)
if [[ $#key == 1 && $((#key)) -lt 31 ]]; then
# ignore all other control keys
else
buffer+="$key"
fi
;;
esac
fi
reply[3]="$current_idx"
reply[4]="$from_what_idx_list_is_shown"
reply[5]="$hscroll"
reply[6]="$search"
reply[7]="$buffer"
reply[8]="$uniq_mode"
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-options` to .zshrc
#
# This function allows to browse and toggle shell's options
#
# Uses n-list
#emulate -L zsh
zmodload zsh/curses
local IFS="
"
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-options.conf ] && . ~/.config/znt/n-options.conf
# TODO restore options
unsetopt localoptions
integer kshoptionprint=0
[[ -o kshoptionprint ]] && kshoptionprint=1
setopt kshoptionprint
local list
local selected
local option
local state
# 0 - don't remember, 1 - remember, 2 - init once, then remember
NLIST_REMEMBER_STATE=2
local NLIST_GREP_STRING="${1:=}"
while (( 1 )); do
list=( `setopt` )
list=( "${(M)list[@]:#*${1:=}*}" )
list=( "${list[@]:#kshoptionprint*}" )
if [ "$#list" -eq 0 ]; then
echo "No matching options"
break
fi
local red=$'\x1b[00;31m' green=$'\x1b[00;32m' reset=$'\x1b[00;00m'
list=( "${list[@]/ off/${red} off$reset}" )
#list=( "${list[@]/ on/${green} on$reset}" )
list=( "${(i)list[@]}" )
n-list "${list[@]}"
if [ "$REPLY" -gt 0 ]; then
[[ -o ksharrays ]] && selected="${reply[$(( REPLY - 1 ))]}" || selected="${reply[$REPLY]}"
option="${selected%% *}"
state="${selected##* }"
if [[ -o globsubst ]]; then
unsetopt globsubst
state="${state%$reset}"
setopt globsubst
else
state="${state%$reset}"
fi
# Toggle the option
if [ "$state" = "on" ]; then
echo "Setting |$option| to off"
unsetopt "$option"
else
echo "Setting |$option| to on"
setopt "$option"
fi
else
break
fi
done
NLIST_REMEMBER_STATE=0
[[ "$kshoptionprint" -eq 0 ]] && unsetopt kshoptionprint
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-panelize` to .zshrc
#
# This function somewhat reminds the panelize feature from Midnight Commander
# It allows browsing output of arbitrary command. Example usage:
# v-panelize ls /usr/local/bin
#
# Uses n-list
emulate -L zsh
setopt extendedglob
zmodload zsh/curses
local IFS="
"
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-panelize.conf ] && . ~/.config/znt/n-panelize.conf
local list
local selected
NLIST_REMEMBER_STATE=0
if [ -t 0 ]; then
# Check if there is proper input
if [ "$#" -lt 1 ]; then
echo "Usage: n-panelize {command} [option|argument] ... or command | n-panelize"
return 1
fi
list=( `"$@"` )
# TODO: $? doesn't reach user
[ "$?" -eq 127 ] && return $?
else
# Check if can reattach to terminal
if [[ ! -c /dev/tty && ! -t 2 ]]; then
echo "No terminal available (no /dev/tty)"
return 1
fi
list=( "${(@f)"$(<&0)"}" )
if [[ ! -c /dev/tty ]]; then
exec <&2
else
exec </dev/tty
fi
fi
n-list "${list[@]}"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
print -zr "# $selected"
fi
# vim: set filetype=zsh:
autoload znt-usetty-wrapper n-cd
local NLIST_START_IN_SEARCH_MODE=0
local NLIST_START_IN_UNIQ_MODE=0
znt-usetty-wrapper n-cd "$@"
unset NLIST_START_IN_SEARCH_MODE
unset NLIST_START_IN_UNIQ_MODE
autoload znt-usetty-wrapper n-history
local NLIST_START_IN_SEARCH_MODE=1
local NLIST_START_IN_UNIQ_MODE=1
znt-usetty-wrapper n-history "$@"
unset NLIST_START_IN_SEARCH_MODE
unset NLIST_START_IN_UNIQ_MODE
autoload znt-usetty-wrapper n-kill
local NLIST_START_IN_SEARCH_MODE=0
local NLIST_START_IN_UNIQ_MODE=0
znt-usetty-wrapper n-kill "$@"
unset NLIST_START_IN_SEARCH_MODE
unset NLIST_START_IN_UNIQ_MODE
emulate -L zsh
zmodload zsh/curses
test_fd0() {
true <&0
}
local restore=0 FD
# Reattach to terminal
if [ ! -t 0 ]; then
# Check if can reattach to terminal in any way
if [[ ! -c /dev/tty && ! -t 2 ]]; then
echo "No terminal available (no /dev/tty and no terminal at stderr)"
return 1
fi
if test_fd0 2>/dev/null; then
exec {FD}<&0
restore=2
else
restore=1
fi
if [[ ! -c /dev/tty ]]; then
exec <&2
else
exec </dev/tty
fi
fi
# Run the command
"$@"
# Restore FD state
(( restore == 1 )) && exec <&-
(( restore == 2 )) && exec <&$FD && exec {FD}<&-
# vim: set filetype=zsh:
#!/usr/bin/env zsh
REPO_DIR="${0%/*}"
CONFIG_DIR="$HOME/.config/znt"
#
# Copy configs
#
if ! test -d "$HOME/.config"; then
mkdir "$HOME/.config"
fi
if ! test -d "$CONFIG_DIR"; then
mkdir "$CONFIG_DIR"
fi
set n-aliases.conf n-env.conf n-history.conf n-list.conf n-panelize.conf n-cd.conf n-functions.conf n-kill.conf n-options.conf
for i; do
if ! test -f "$CONFIG_DIR/$i"; then
cp "$REPO_DIR/.config/znt/$i" "$CONFIG_DIR"
fi
done
#
# Load functions
#
autoload n-aliases n-cd n-env n-functions n-history n-kill n-list n-list-draw n-list-input n-options n-panelize
autoload znt-usetty-wrapper znt-history-widget znt-cd-widget znt-kill-widget
alias naliases=n-aliases ncd=n-cd nenv=n-env nfunctions=n-functions nhistory=n-history
alias nkill=n-kill noptions=n-options npanelize=n-panelize
zle -N znt-history-widget
bindkey '^R' znt-history-widget
setopt AUTO_PUSHD HIST_IGNORE_DUPS PUSHD_IGNORE_DUPS
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment