Advertisement
txt444

Command menu (terminal,bash,fzf).

Oct 9th, 2022 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.10 KB | None | 0 0
  1. Oct 09, 2022, 17:09:47
  2.  
  3.  
  4.  
  5. .bashrc :
  6.  
  7. #   folder,export PATH : Private path where the "type -p" command will find your own files.
  8. #   source : Short .bashrc entry to make the original .bashrc more transparent. 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.  
  17. ~/Downloads/bash/bash_settings (or "$folder"/bash_settings) :
  18.  
  19. #   Command menu with multiple pages (bash, fzf).
  20. #
  21. #   fake readline (up and delete line) source :
  22. #   https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
  23. #   https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
  24. #
  25. #   fzf insert = edit source :
  26. #   https://github.com/junegunn/fzf/wiki/examples#opening-files
  27. #
  28. #   neccesary packages :
  29. #   coreutils : head, tail
  30. #   ncurses : tput
  31. #
  32. #   vaiables :
  33. #   pn : page number
  34. #   pmax : page maximum
  35. #   pages,line,cmd,key
  36. #
  37. #   The while, do, continue, done are only used to replace goto. Instead of goto, continue jumps to the beginning.
  38.  
  39.  
  40.  
  41. pn=0                                    # neccesary
  42.  
  43. function menu {
  44.  
  45. if [ $# -eq 0 ]                         # No page, then exit.
  46. then
  47. return
  48. fi
  49.  
  50. pages=($@)                              # Loading pages (.txt) into a variable (array).
  51.  
  52. let pmax=${#pages[@]}-1                 # Number of text files (or pages).
  53.  
  54. if [ $pn -gt $pmax ]
  55. then
  56. pn=$pmax
  57. fi
  58.  
  59. while :
  60. do
  61.  
  62.  
  63. line=$(< $(type -p ${pages[pn]}) fzf --reverse --exact --no-color --no-info --expect=insert,right,left --no-clear)
  64.  
  65.  
  66. if [ "$line" = "" ]                     # fzf escape, ctrl-c, empty string
  67. then
  68. tput rmcup && tput cuu1 && tput el      # restore screen (fzf --no-clear) and fake readline (up and delete line)
  69.  
  70. cmd=""
  71. break                                   # goto the end (done)
  72.  
  73. fi
  74.  
  75. key=$(head -1 <<< "$line")              # https://github.com/junegunn/fzf/wiki/examples#opening-files (example of using --expect)
  76.  
  77. if [ "$key" = right ]                   # paging right
  78. then
  79. let pn=pn+1
  80.  
  81.     if [ $pn -gt $pmax ]
  82.     then
  83.     pn=0
  84.     fi
  85.  
  86. continue                                # goto the top (do)
  87.  
  88. fi
  89.  
  90. if [ "$key" = left ]                    # paging left
  91. then
  92. let pn=pn-1
  93.  
  94.     if [ $pn -lt 0 ]
  95.     then
  96.     pn=$pmax
  97.     fi
  98.  
  99. continue                                # goto the top (do)
  100.  
  101. fi
  102.  
  103. cmd=$(head -2 <<< "$line" | tail -1)    # https://github.com/junegunn/fzf/wiki/examples#opening-files (example of using --expect)
  104.  
  105. if [ "$key" = insert ]                  # only insertion
  106. then
  107. tput rmcup && tput cuu1 && tput el      # restore screen (fzf --no-clear) and fake readline (up and delete line)
  108. read -p "${PS1@P}" -eri "$cmd" cmd
  109.  
  110. fi
  111.  
  112. history -s $FUNCNAME $@
  113. history -s $cmd
  114.  
  115. tput rmcup && tput cuu1 && tput el      # restore screen (fzf --no-clear) and fake readline (up and delete line)
  116. echo "${PS1@P}"$cmd
  117.  
  118. break                                   # goto the end (done)
  119.  
  120. done
  121.  
  122. eval $cmd
  123.  
  124. }
  125.  
  126.  
  127.  
  128. #   Personal examples.
  129.  
  130. alias m='menu cmd.txt pacman.txt backup.txt hdd.txt swap.txt install.txt'
  131.  
  132. #   Older one page settings (out of habit).
  133.  
  134. 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'
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement