Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Oct 24, 2022, 08:52:28 Command menu (terminal,bash,fzf_reload,/tmp)
- .bashrc :
- # folder,export PATH : Private path where the "type -p" command will find your own files.
- # source : Short .bashrc entry to make the original .bashrc more transparent.
- # Custom settings and files in a separate directory. Easily portable, i.e. modular architecture.
- # $folder content :
- # $ ls $folder
- # backup.txt bash_settings cmd.txt hdd.txt install.txt pacman.txt swap.txt
- folder=~/Downloads/bash && export PATH="$folder:$PATH" && source bash_settings
- ~/Downloads/bash/bash_settings (or "$folder"/bash_settings) :
- # Command menu with multiple pages (bash, fzf).
- #
- # fake readline (up and delete line) source :
- # https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
- # https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
- #
- # fzf insert = edit source :
- # https://github.com/junegunn/fzf/wiki/examples#opening-files
- #
- # neccesary packages :
- # coreutils : head, tail
- # ncurses : tput
- #
- # variables (using shared memory) :
- # /tmp/pm : page maximum
- # /tmp/pn : page number
- # /tmp/pd : page decrease
- # /tmp/pi : page increase
- # https://stackoverflow.com/questions/13207292/bash-background-process-modify-global-variable
- function menu {
- if [ $# -eq 0 ] # Empty command does nothing (quits).
- then
- return
- fi
- if [ ! -s /tmp/pn ] # Page number test, if missing then 0.
- then
- echo 0 >/tmp/pn
- fi
- i=0 # Copy pages to the /tmp folder, numbered for simplicity (/tmp/$i).
- for page in "$@" # No need to use an array variable and search in it.
- do
- cp $(type -p $page) /tmp/$i
- ((++i)) # let i=i+1
- done
- # page increase and page decrease commands copy to /tmp folder for fzf_reload
- echo 'echo $(($(</tmp/pn)+1))>/tmp/pn;[ $(</tmp/pn) -gt $(</tmp/pm) ] && echo 0 >/tmp/pn || :;cat /tmp/$(</tmp/pn)'>/tmp/pi
- echo 'echo $(($(</tmp/pn)-1))>/tmp/pn;[ $(</tmp/pn) -lt 0 ] && echo $(</tmp/pm) >/tmp/pn || :;cat /tmp/$(</tmp/pn)'>/tmp/pd
- echo $(($#-1))>/tmp/pm # define page maximum
- if [ $(</tmp/pn) -gt $(</tmp/pm) ] # minor adjustment
- then
- echo $(</tmp/pm)>/tmp/pn
- fi
- tty="" # tty : --height=50% --border=vertical
- if [ "$TERM" = linux ]
- then
- tty="--height=50% --border=vertical"
- fi
- # fzf engine
- line=$(< /tmp/$(</tmp/pn) fzf --reverse --exact --no-color --no-info --expect=insert --cycle --bind "right:reload(. /tmp/pi),left:reload(. /tmp/pd)" $tty)
- if [ "$line" = "" ] # fzf escape, ctrl-c, empty string
- then
- tput cuu1 && tput el # fake readline (up and delete line)
- return
- fi
- key=$(head -1 <<< "$line") # https://github.com/junegunn/fzf/wiki/examples#opening-files
- cmd=$(head -2 <<< "$line" | tail -1) # (example of using --expect)
- if [ "$key" = insert ] # only insertion
- then
- tput cuu1 && tput el # fake readline (up and delete line)
- read -p "${PS1@P}" -eri "$cmd" cmd
- fi
- history -s $FUNCNAME $@
- history -s $cmd
- tput cuu1 && tput el # fake readline (up and delete line)
- echo "${PS1@P}"$cmd
- eval $cmd
- }
- # Personal examples.
- alias m='menu cmd.txt pacman.txt backup.txt hdd.txt swap.txt install.txt'
- # Older one page settings (out of habit).
- alias b='menu backup.txt' s='menu cmd.txt' pk='menu pacman.txt' hdd='menu hdd.txt' swap='menu swap.txt' i='menu install.txt'
Advertisement
Add Comment
Please, Sign In to add comment