n-options
1934 bytes
1# Copy this file into /usr/share/zsh/site-functions/
2# and add 'autoload n-options` to .zshrc
3#
4# This function allows to browse and toggle shell's options
5#
6# Uses n-list
7
8#emulate -L zsh
9
10zmodload zsh/curses
11
12local IFS="
13"
14
15unset NLIST_COLORING_PATTERN
16
17[ -f ~/.config/znt/n-list.conf ] && builtin source ~/.config/znt/n-list.conf
18[ -f ~/.config/znt/n-options.conf ] && builtin source ~/.config/znt/n-options.conf
19
20# TODO restore options
21unsetopt localoptions
22
23integer kshoptionprint=0
24[[ -o kshoptionprint ]] && kshoptionprint=1
25setopt kshoptionprint
26
27local list
28local selected
29local option
30local state
31
32# 0 - don't remember, 1 - remember, 2 - init once, then remember
33NLIST_REMEMBER_STATE=2
34
35local NLIST_GREP_STRING="${1:=}"
36
37while (( 1 )); do
38 list=( `setopt` )
39 list=( "${(M)list[@]:#*${1:=}*}" )
40 list=( "${list[@]:#kshoptionprint*}" )
41
42 if [ "$#list" -eq 0 ]; then
43 echo "No matching options"
44 break
45 fi
46
47 local red=$'\x1b[00;31m' green=$'\x1b[00;32m' reset=$'\x1b[00;00m'
48 list=( "${list[@]/ off/${red} off$reset}" )
49 #list=( "${list[@]/ on/${green} on$reset}" )
50 list=( "${(i)list[@]}" )
51
52 n-list "${list[@]}"
53
54 if [ "$REPLY" -gt 0 ]; then
55 [[ -o ksharrays ]] && selected="${reply[$(( REPLY - 1 ))]}" || selected="${reply[$REPLY]}"
56 option="${selected%% *}"
57 state="${selected##* }"
58
59 if [[ -o globsubst ]]; then
60 unsetopt globsubst
61 state="${state%$reset}"
62 setopt globsubst
63 else
64 state="${state%$reset}"
65 fi
66
67 # Toggle the option
68 if [ "$state" = "on" ]; then
69 echo "Setting |$option| to off"
70 unsetopt "$option"
71 else
72 echo "Setting |$option| to on"
73 setopt "$option"
74 fi
75 else
76 break
77 fi
78done
79
80NLIST_REMEMBER_STATE=0
81
82[[ "$kshoptionprint" -eq 0 ]] && unsetopt kshoptionprint
83
84# vim: set filetype=zsh: