txt444

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

Oct 28th, 2022 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.46 KB | None | 0 0
  1. #   Oct 28, 2022, 11:18:18  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. #               To collate pages, use the alias command.
  15. #              
  16. #               alias m='menu file1.txt file2.txt file3.txt'
  17. #
  18. #
  19. #   Oct 28, 2022, 11:18:43 :    Return to the array variable (ps like pages) instead of copying.
  20. #       Main motivation         Use mapfile command instead of head/tail command.
  21. #
  22. #   fake readline (up and delete line) source :
  23. #   https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
  24. #   https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
  25. #
  26. #   fzf insert = edit source :
  27. #   https://github.com/junegunn/fzf/wiki/examples#git (under fstash)
  28. #
  29. #   neccesary packages :
  30. #   ncurses : tput
  31. #
  32. #   variables (using shared memory) :
  33. #   /tmp/pm : page maximum
  34. #   /tmp/pn : page number
  35. #   /tmp/pd : page decrease
  36. #   /tmp/pi : page increase
  37. #   /tmp/ps : pages
  38. #   https://stackoverflow.com/questions/13207292/bash-background-process-modify-global-variable
  39. #   https://github.com/junegunn/fzf/issues/2976#issuecomment-1254529831
  40.  
  41.  
  42. function menu {
  43.  
  44. if [ $# -eq 0 ]                         #   Empty command does nothing (quits).
  45. then                                    #   (or print help/usage ?)
  46. return
  47. fi
  48.  
  49. if [ ! -s /tmp/pn ]                     #   Define page number, if neccesary.
  50. then                                    #   (Page number test, if missing then 0.)
  51. echo 0 >/tmp/pn
  52. fi
  53.  
  54. echo "$@">/tmp/ps                       #   Define pages array.
  55. ps=($@)
  56.                                         #   page increase and page decrease commands copy to /tmp folder for fzf_reload
  57.  
  58. echo 'ps=($(</tmp/ps));i=$(</tmp/pn);((++i));[ $i -gt $(</tmp/pm) ]&&i=0||:;echo $i>/tmp/pn;cat ${ps[i]}'>/tmp/pi
  59. echo 'ps=($(</tmp/ps));i=$(</tmp/pn);((--i));[ $i -lt 0 ]&&i=$(</tmp/pm)||:;echo $i>/tmp/pn;cat ${ps[i]}'>/tmp/pd
  60.  
  61. echo $(($#-1))>/tmp/pm                  #   Define page maximum.
  62. #   echo $((${#ps[@]}-1))>/tmp/pm       #   Define page maximum. Another option.
  63.  
  64. if [ $(</tmp/pn) -gt $(</tmp/pm) ]      #   Page number adjustment, if neccesary.
  65. then                                    #   (or zero ?)
  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=$(< ${ps[$(</tmp/pn)]} fzf --reverse --exact --no-color --no-info --expect=insert --cycle --bind "right:reload(. /tmp/pi),left:reload(. /tmp/pd)" $tty)
  77.  
  78. if [ "$line" = "" ]                     #   fzf escape, ctrl-c, empty string
  79. then
  80. tput cuu1 && tput el                    #   fake readline (up and delete line)
  81. return
  82. fi
  83.  
  84. mapfile -t line <<< "$line"             #   https://github.com/junegunn/fzf/wiki/examples#git
  85. key="${line[0]}"                        #   under fstash
  86. cmd="${line[1]}"
  87.  
  88. if [ "$key" = insert ]                  #   Just insert for editing.
  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. #   Example.
  106.  
  107. alias m='menu ~/.bash_history ~/.bashrc'
  108.  
  109. #   Personal examples.
  110. #
  111. #   alias m='menu ~/cmd.txt ~/pacman.txt ~/backup.txt ~/hdd.txt ~/swap.txt ~/install.txt'
  112. #
  113. #   Older one page settings (out of habit).
  114. #
  115. #   alias b='menu ~/backup.txt' s='menu ~/cmd.txt' pk='menu ~/pacman.txt' hdd='menu ~/hdd.txt'
  116. #   alias swap='menu ~/swap.txt' i='menu ~/install.txt'
  117.  
Advertisement
Add Comment
Please, Sign In to add comment