txt444

Command_menu_.bashrc

Nov 6th, 2022
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.09 KB | None | 0 0
  1. #   Nov 06, 2022, 15:19:43  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.                                         #   fzf engine
  84.  
  85. 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)
  86.  
  87. if [ "$line" = "" ]                     #   fzf escape, ctrl-c, empty string
  88. then
  89. tput cuu1 && tput el                    #   fake readline (up and delete line)
  90. return
  91. fi
  92.  
  93. mapfile -t line <<< "$line"             #   https://github.com/junegunn/fzf/wiki/examples#git
  94. key="${line[0]}"                        #   under fstash
  95. cmd="${line[1]}"
  96.  
  97.                                         #   Just insert for editing. Tab like dvorka/hstr.
  98.  
  99. if [ "$key" = insert ] || [ "$key" = tab ]
  100. then
  101. tput cuu1 && tput el                    #   fake readline (up and delete line)
  102. read -p "${PS1@P}" -eri "$cmd" cmd
  103. history -s $FUNCNAME $@
  104. history -s $cmd
  105. eval $cmd
  106. return
  107. fi
  108.  
  109. history -s $FUNCNAME $@
  110. history -s $cmd
  111.  
  112. tput cuu1 && tput el                    #   fake readline (up and delete line)
  113. echo "${PS1@P}"$cmd
  114.  
  115. eval $cmd
  116.  
  117. }
  118.  
  119.  
  120. #   Example.
  121.  
  122. alias m='menu ~/.bash_history ~/.bashrc'
  123.  
  124. #   Personal examples.
  125. #
  126. #   alias m='menu ~/cmd.txt ~/pacman.txt ~/backup.txt ~/hdd.txt ~/swap.txt ~/install.txt'
  127. #
  128. #   Older one page settings (out of habit).
  129. #
  130. #   alias b='menu ~/backup.txt' s='menu ~/cmd.txt' pk='menu ~/pacman.txt' hdd='menu ~/hdd.txt'
  131. #   alias swap='menu ~/swap.txt' i='menu ~/install.txt'
  132. #
  133. #   mefo = menu folder  #   Nov 06, 2022, 15:26:19
  134. #
  135. #   alias m="menu $mefo/*.txt ~/.bash_history"
  136. #   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"
  137.  
Advertisement
Add Comment
Please, Sign In to add comment