txt444

Command_menu_.bashrc_Mar-11-2023

Mar 11th, 2023 (edited)
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.71 KB | None | 0 0
  1. #   Mar 11, 2023, 14:19:24  By using .bashrc.
  2. #
  3. #
  4. #   Command menu with multiple pages (bash, fzf).
  5. #
  6. #   Usage :     menu file1.txt file2.txt file3.txt ...
  7. #               menu <only .txt files with path>
  8. #
  9. #   Examples :  menu ~/.bash_history
  10. #               menu ~/.bash_history ~/.bashrc
  11. #
  12. #               To collate pages, use the alias command.
  13. #              
  14. #               alias m="menu file1.txt file2.txt file3.txt"
  15. #               alias m="menu file1.txt file2.txt file3.txt ~/.bash_history"
  16. #
  17. #               or *           
  18. #
  19. #               alias m="menu /path/*.txt"
  20. #               alias m="menu /path/*.txt ~/.bash_history"
  21. #              
  22. #               In the case of a star, the order can be determined by starting the file names with a serial number.
  23. #               (For example : 0_cmd.txt 1_pacman.txt 2_backup.txt ...)
  24. #
  25. #   Keys :      right, left :   paging right, left
  26. #               enter :         run, execution
  27. #               insert, tab :   paste to edit
  28. #               esc, ctrl+c :   exit
  29. #               down, up
  30. #
  31. #   Code sources :
  32. #
  33. #   fake readline (up and delete line) :
  34. #   https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
  35. #   https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
  36. #
  37. #   fzf keys :  https://github.com/junegunn/fzf/wiki/examples#git (under fstash)
  38. #
  39. #   neccesary package : ncurses (tput)
  40. #
  41. #   variables (using shared memory) :
  42. #   /tmp/pm : page maximum
  43. #   /tmp/pn : page number
  44. #   /tmp/pd : page decrease
  45. #   /tmp/pi : page increase
  46. #   /tmp/ps : pages
  47. #   https://stackoverflow.com/questions/13207292/bash-background-process-modify-global-variable
  48. #   https://github.com/junegunn/fzf/issues/2976#issuecomment-1254529831
  49.  
  50.  
  51. function menu {
  52.  
  53. if [ $# -eq 0 ]                         #   Empty command does nothing (quits).
  54. then                                    #   (or print help/usage ?)
  55. return
  56. fi
  57.  
  58. if [ ! -s /tmp/pn ]                     #   Define page number, if neccesary.
  59. then                                    #   (Page number test, if missing then 0.)
  60. echo 0 >/tmp/pn
  61. fi
  62.  
  63. echo "$@">/tmp/ps                       #   Define pages array.
  64. ps=($@)
  65.                                         #   Page increase and page decrease commands copy to /tmp folder for fzf_reload.
  66.  
  67. echo 'ps=($(</tmp/ps));i=$(</tmp/pn);((++i));[ $i -gt $(</tmp/pm) ]&&i=0||:;echo $i>/tmp/pn;cat ${ps[i]}'>/tmp/pi
  68. echo 'ps=($(</tmp/ps));i=$(</tmp/pn);((--i));[ $i -lt 0 ]&&i=$(</tmp/pm)||:;echo $i>/tmp/pn;cat ${ps[i]}'>/tmp/pd
  69.  
  70. echo $(($#-1))>/tmp/pm                  #   Define page maximum.
  71. #   echo $((${#ps[@]}-1))>/tmp/pm       #   Define page maximum. Another option.
  72.  
  73. if [ $(</tmp/pn) -gt $(</tmp/pm) ]      #   Page number adjustment, if neccesary.
  74. then                                    #   (or zero ?)
  75. echo $(</tmp/pm)>/tmp/pn
  76. fi
  77.  
  78. tty=""                                  #   tty : --height=50% --border=vertical
  79. if [ "$TERM" = linux ]
  80. then
  81. tty="--height=50% --border=vertical"
  82. fi
  83.  
  84. local -                                 #   wildcard off
  85. set -o noglob                           #   https://unix.stackexchange.com/questions/406216/check-for-bash-options
  86.  
  87.                                         #   fzf engine
  88.  
  89. 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)
  90.  
  91. if [ "$line" = "" ]                     #   fzf escape, ctrl-c, empty string
  92. then
  93. tput cuu1 && tput el                    #   fake readline (up and delete line)
  94. return
  95. fi
  96.  
  97. mapfile -t line <<< "$line"             #   https://github.com/junegunn/fzf/wiki/examples#git
  98. key="${line[0]}"                        #   under fstash
  99. cmd="${line[1]}"
  100.  
  101.                                         #   Just insert for editing. Tab like dvorka/hstr.
  102.  
  103. if [ "$key" = insert ] || [ "$key" = tab ]
  104. then
  105. tput cuu1 && tput el                    #   fake readline (up and delete line)
  106. read -p "${PS1@P}" -eri "$cmd" cmd
  107. history -s $FUNCNAME $@
  108. history -s $cmd
  109. set +o noglob                           #   wildcard on
  110. eval $cmd
  111. return
  112. fi
  113.  
  114. history -s $FUNCNAME $@
  115. history -s $cmd
  116.  
  117. tput cuu1 && tput el                    #   fake readline (up and delete line)
  118. echo "${PS1@P}"$cmd
  119.  
  120. set +o noglob                           #   wildcard on
  121.  
  122. eval $cmd
  123.  
  124. }
  125.  
  126.  
  127. #   Example.
  128.  
  129. alias m='menu ~/.bash_history ~/.bashrc'
  130.  
  131. #   Personal examples.
  132. #
  133. #   alias m='menu ~/cmd.txt ~/pacman.txt ~/backup.txt ~/hdd.txt ~/swap.txt ~/install.txt'
  134. #
  135. #   Older one page settings (out of habit).
  136. #
  137. #   alias b='menu ~/backup.txt' s='menu ~/cmd.txt' pk='menu ~/pacman.txt' hdd='menu ~/hdd.txt'
  138. #   alias swap='menu ~/swap.txt' i='menu ~/install.txt'
  139. #
  140. #   mefo = menu folder  #   Nov 06, 2022, 15:26:19
  141. #
  142. #   alias m="menu $mefo/*.txt ~/.bash_history"
  143. #   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"
  144. #
  145. #
  146. #   Changelog :
  147. #
  148. #   Mar 11, 2023, 13:52:19  Disable wildcard. Add
  149. #
  150. #   local -                                 #   wildcard off
  151. #   set -o noglob                           #   https://unix.stackexchange.com/questions/406216/check-for-bash-options
  152. #   set +o noglob                           #   wildcard on
  153. #
  154. #   commands. Code sources :    https://stackoverflow.com/questions/11456403/stop-shell-wildcard-character-expansion
  155. #                               https://unix.stackexchange.com/questions/406216/check-for-bash-options
  156.  
Advertisement
Add Comment
Please, Sign In to add comment