n-panelize
1500 bytes
1# Copy this file into /usr/share/zsh/site-functions/
2# and add 'autoload n-panelize` to .zshrc
3#
4# This function somewhat reminds the panelize feature from Midnight Commander
5# It allows browsing output of arbitrary command. Example usage:
6# v-panelize ls /usr/local/bin
7#
8# Uses n-list
9
10emulate -L zsh
11
12setopt extendedglob
13zmodload zsh/curses
14
15local IFS="
16"
17
18unset NLIST_COLORING_PATTERN
19
20[ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
21[ -f ~/.config/znt/n-panelize.conf ] && builtin source ~/.config/znt/n-panelize.conf
22
23local list
24local selected
25
26NLIST_REMEMBER_STATE=0
27
28if [ -t 0 ]; then
29 # Check if there is proper input
30 if [ "$#" -lt 1 ]; then
31 echo "Usage: n-panelize {command} [option|argument] ... or command | n-panelize"
32 return 1
33 fi
34
35 # This loop makes script faster on some Zsh's (e.g. 5.0.8)
36 repeat 1; do
37 list=( `"$@"` )
38 done
39
40 # TODO: $? doesn't reach user
41 [ "$?" -eq 127 ] && return $?
42else
43 # Check if can reattach to terminal
44 if [[ ! -c /dev/tty && ! -t 2 ]]; then
45 echo "No terminal available (no /dev/tty)"
46 return 1
47 fi
48
49 # This loop makes script faster on some Zsh's (e.g. 5.0.8)
50 repeat 1; do
51 list=( "${(@f)"$(<&0)"}" )
52 done
53
54 if [[ ! -c /dev/tty ]]; then
55 exec <&2
56 else
57 exec </dev/tty
58 fi
59fi
60
61n-list "${list[@]}"
62
63if [ "$REPLY" -gt 0 ]; then
64 selected="$reply[REPLY]"
65 print -zr "# $selected"
66fi
67
68# vim: set filetype=zsh: