Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Oct 27, 2022, 13:47:48 Command menu (terminal,bash,fzf_reload,/tmp,only .bashrc/function)
- .bashrc :
- # Command menu with multiple pages (bash, fzf).
- #
- # Usage : menu file1.txt file2.txt file3.txt ...
- # menu <only .txt files with path>
- #
- # Example : menu ~/.bash_history
- # menu ~/.bash_history ~/.bashrc
- #
- # 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 # (or print help/usage ?)
- return
- fi
- if [ ! -s /tmp/pn ] # Define page number, if neccesary.
- then # (Page number test, if missing then 0.)
- 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 $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) ] # Page number adjustment, if neccesary.
- then # (or zero ?)
- 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 ] # Just insert for editing.
- 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
- }
- # Example.
- alias m='menu ~/.bash_history ~/.bashrc'
- # 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'
- # alias swap='menu ~/swap.txt' i='menu ~/install.txt'
Advertisement
Add Comment
Please, Sign In to add comment