txt444

Command menu (terminal,bash,fzf_reload,/tmp,only .bashrc/function)

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