Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Mar 11, 2023, 14:19:24 By using .bashrc.
- #
- #
- # Command menu with multiple pages (bash, fzf).
- #
- # Usage : menu file1.txt file2.txt file3.txt ...
- # menu <only .txt files with path>
- #
- # Examples : menu ~/.bash_history
- # menu ~/.bash_history ~/.bashrc
- #
- # To collate pages, use the alias command.
- #
- # alias m="menu file1.txt file2.txt file3.txt"
- # alias m="menu file1.txt file2.txt file3.txt ~/.bash_history"
- #
- # or *
- #
- # alias m="menu /path/*.txt"
- # alias m="menu /path/*.txt ~/.bash_history"
- #
- # In the case of a star, the order can be determined by starting the file names with a serial number.
- # (For example : 0_cmd.txt 1_pacman.txt 2_backup.txt ...)
- #
- # Keys : right, left : paging right, left
- # enter : run, execution
- # insert, tab : paste to edit
- # esc, ctrl+c : exit
- # down, up
- #
- # Code sources :
- #
- # fake readline (up and delete line) :
- # 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 keys : https://github.com/junegunn/fzf/wiki/examples#git (under fstash)
- #
- # neccesary package : ncurses (tput)
- #
- # variables (using shared memory) :
- # /tmp/pm : page maximum
- # /tmp/pn : page number
- # /tmp/pd : page decrease
- # /tmp/pi : page increase
- # /tmp/ps : pages
- # https://stackoverflow.com/questions/13207292/bash-background-process-modify-global-variable
- # https://github.com/junegunn/fzf/issues/2976#issuecomment-1254529831
- 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
- echo "$@">/tmp/ps # Define pages array.
- ps=($@)
- # Page increase and page decrease commands copy to /tmp folder for fzf_reload.
- echo 'ps=($(</tmp/ps));i=$(</tmp/pn);((++i));[ $i -gt $(</tmp/pm) ]&&i=0||:;echo $i>/tmp/pn;cat ${ps[i]}'>/tmp/pi
- echo 'ps=($(</tmp/ps));i=$(</tmp/pn);((--i));[ $i -lt 0 ]&&i=$(</tmp/pm)||:;echo $i>/tmp/pn;cat ${ps[i]}'>/tmp/pd
- echo $(($#-1))>/tmp/pm # Define page maximum.
- # echo $((${#ps[@]}-1))>/tmp/pm # Define page maximum. Another option.
- 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
- local - # wildcard off
- set -o noglob # https://unix.stackexchange.com/questions/406216/check-for-bash-options
- # fzf engine
- line=$(< ${ps[$(</tmp/pn)]} fzf --reverse --exact --no-color --no-info --expect=insert,tab --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
- mapfile -t line <<< "$line" # https://github.com/junegunn/fzf/wiki/examples#git
- key="${line[0]}" # under fstash
- cmd="${line[1]}"
- # Just insert for editing. Tab like dvorka/hstr.
- if [ "$key" = insert ] || [ "$key" = tab ]
- then
- tput cuu1 && tput el # fake readline (up and delete line)
- read -p "${PS1@P}" -eri "$cmd" cmd
- history -s $FUNCNAME $@
- history -s $cmd
- set +o noglob # wildcard on
- eval $cmd
- return
- fi
- history -s $FUNCNAME $@
- history -s $cmd
- tput cuu1 && tput el # fake readline (up and delete line)
- echo "${PS1@P}"$cmd
- set +o noglob # wildcard on
- 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'
- #
- # mefo = menu folder # Nov 06, 2022, 15:26:19
- #
- # alias m="menu $mefo/*.txt ~/.bash_history"
- # alias b="menu $mefo/2_backup.txt" s="menu $mefo/0_cmd.txt" pk="menu $mefo/1_pacman.txt" hdd="menu $mefo/3_hdd.txt" swap="menu $mefo/4_swap.txt" i="menu $mefo/5_install.txt"
- #
- #
- # Changelog :
- #
- # Mar 11, 2023, 13:52:19 Disable wildcard. Add
- #
- # local - # wildcard off
- # set -o noglob # https://unix.stackexchange.com/questions/406216/check-for-bash-options
- # set +o noglob # wildcard on
- #
- # commands. Code sources : https://stackoverflow.com/questions/11456403/stop-shell-wildcard-character-expansion
- # https://unix.stackexchange.com/questions/406216/check-for-bash-options
Advertisement
Add Comment
Please, Sign In to add comment