txt444

Command menu (terminal,bash,fzf_reload,/tmp)

Oct 24th, 2022 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.39 KB | None | 0 0
  1. #   Oct 24, 2022, 08:52:28  Command menu (terminal,bash,fzf_reload,/tmp)
  2.  
  3.  
  4. .bashrc :
  5.  
  6. #   folder,export PATH : Private path where the "type -p" command will find your own files.
  7. #   source : Short .bashrc entry to make the original .bashrc more transparent.
  8. #   Custom settings and files in a separate directory. Easily portable, i.e. modular architecture.
  9. #   $folder content :
  10. #   $ ls $folder
  11. #   backup.txt  bash_settings  cmd.txt  hdd.txt  install.txt  pacman.txt  swap.txt
  12.  
  13. folder=~/Downloads/bash && export PATH="$folder:$PATH" && source bash_settings
  14.  
  15.  
  16. ~/Downloads/bash/bash_settings (or "$folder"/bash_settings) :
  17.  
  18. #   Command menu with multiple pages (bash, fzf).
  19. #
  20. #   fake readline (up and delete line) source :
  21. #   https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
  22. #   https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
  23. #
  24. #   fzf insert = edit source :
  25. #   https://github.com/junegunn/fzf/wiki/examples#opening-files
  26. #
  27. #   neccesary packages :
  28. #   coreutils : head, tail
  29. #   ncurses : tput
  30. #
  31. #   variables (using shared memory) :
  32. #   /tmp/pm : page maximum
  33. #   /tmp/pn : page number
  34. #   /tmp/pd : page decrease
  35. #   /tmp/pi : page increase
  36. #   https://stackoverflow.com/questions/13207292/bash-background-process-modify-global-variable
  37.  
  38.  
  39. function menu {
  40.  
  41. if [ $# -eq 0 ]                         #   Empty command does nothing (quits).
  42. then
  43. return
  44. fi
  45.  
  46. if [ ! -s /tmp/pn ]                     #   Page number test, if missing then 0.
  47. then
  48. echo 0 >/tmp/pn
  49. fi
  50.  
  51. i=0                                     #   Copy pages to the /tmp folder, numbered for simplicity (/tmp/$i).
  52. for page in "$@"                        #   No need to use an array variable and search in it.
  53. do
  54. cp $(type -p $page) /tmp/$i
  55. ((++i))                                 #   let i=i+1
  56. done
  57.                                         #   page increase and page decrease commands copy to /tmp folder for fzf_reload
  58.  
  59. echo 'echo $(($(</tmp/pn)+1))>/tmp/pn;[ $(</tmp/pn) -gt $(</tmp/pm) ] && echo 0 >/tmp/pn || :;cat /tmp/$(</tmp/pn)'>/tmp/pi
  60. echo 'echo $(($(</tmp/pn)-1))>/tmp/pn;[ $(</tmp/pn) -lt 0 ] && echo $(</tmp/pm) >/tmp/pn || :;cat /tmp/$(</tmp/pn)'>/tmp/pd
  61.  
  62. echo $(($#-1))>/tmp/pm                  #   define page maximum
  63.  
  64. if [ $(</tmp/pn) -gt $(</tmp/pm) ]      #   minor adjustment
  65. then
  66. echo $(</tmp/pm)>/tmp/pn
  67. fi
  68.  
  69. tty=""                                  #   tty : --height=50% --border=vertical
  70. if [ "$TERM" = linux ]
  71. then
  72. tty="--height=50% --border=vertical"
  73. fi
  74.                                         #   fzf engine
  75.  
  76. line=$(< /tmp/$(</tmp/pn) fzf --reverse --exact --no-color --no-info --expect=insert --cycle --bind "right:reload(. /tmp/pi),left:reload(. /tmp/pd)" $tty)
  77.  
  78.  
  79. if [ "$line" = "" ]                     #   fzf escape, ctrl-c, empty string
  80. then
  81. tput cuu1 && tput el                    #   fake readline (up and delete line)
  82. return
  83. fi
  84.  
  85. key=$(head -1 <<< "$line")              #   https://github.com/junegunn/fzf/wiki/examples#opening-files
  86. cmd=$(head -2 <<< "$line" | tail -1)    #   (example of using --expect)
  87.  
  88. if [ "$key" = insert ]                  #   only insertion
  89. then
  90. tput cuu1 && tput el                    #   fake readline (up and delete line)
  91. read -p "${PS1@P}" -eri "$cmd" cmd
  92. fi
  93.  
  94. history -s $FUNCNAME $@
  95. history -s $cmd
  96.  
  97. tput cuu1 && tput el                    #   fake readline (up and delete line)
  98. echo "${PS1@P}"$cmd
  99.  
  100. eval $cmd
  101.  
  102. }
  103.  
  104.  
  105. #   Personal examples.
  106.  
  107. alias m='menu cmd.txt pacman.txt backup.txt hdd.txt swap.txt install.txt'
  108.  
  109. #   Older one page settings (out of habit).
  110.  
  111. 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'
  112.  
Advertisement
Add Comment
Please, Sign In to add comment