txt444

Command menu (terminal,bash,fzf_reload)

Oct 21st, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.31 KB | None | 0 0
  1. #   Oct 21, 2022, 13:12:29  Command menu (terminal,bash,fzf_reload)
  2.  
  3.  
  4. .bashrc :
  5.  
  6. #   folder,export PATH : Private path where the "type -p" command will find your own files.
  7. #   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.
  8. #   $folder content :
  9. #   $ ls $folder
  10. #   backup.txt  bash_settings  cmd.txt  hdd.txt  install.txt  menupagedec  menupageinc  pacman.txt  swap.txt
  11.  
  12. folder=~/Downloads/bash && export PATH="$folder:$PATH" && source bash_settings
  13.  
  14.  
  15.  
  16. #   fzf reload subroutines bash script :
  17.  
  18. $folder/menupageinc :
  19.  
  20. #!/bin/bash
  21. pages=($(< ~/pages));i=$(< ~/pagen);((i++));[ $i -gt $(< ~/pagem) ] && i=0 || :
  22. echo $i > ~/pagen;cat $(type -p ${pages[i]})
  23.  
  24. $folder/menupagedec :
  25.  
  26. #!/bin/bash
  27. pages=($(< ~/pages));i=$(< ~/pagen);((i--));[ $i -lt 0 ] && i=$(< ~/pagem) || :
  28. echo $i > ~/pagen;cat $(type -p ${pages[i]})
  29.  
  30.  
  31.  
  32. ~/Downloads/bash/bash_settings (or "$folder"/bash_settings) :
  33.  
  34. #   Command menu with multiple pages (bash, fzf).
  35. #
  36. #   fake readline (up and delete line) source :
  37. #   https://stackoverflow.com/questions/18362837/how-to-display-and-refresh-multiple-lines-in-bash
  38. #   https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
  39. #
  40. #   fzf insert = edit source :
  41. #   https://github.com/junegunn/fzf/wiki/examples#opening-files
  42. #
  43. #   neccesary packages :
  44. #   coreutils : head, tail
  45. #   ncurses : tput
  46. #
  47. #   variables :
  48. #   pn : page number
  49. #   pmax : page maximum
  50. #   pages,line,cmd,key
  51. #
  52. #   variables with files (for fzf reload) :
  53. #   ~/pagen : page number
  54. #   ~/pages : pages
  55. #   ~/pagem : page maximum
  56.  
  57.  
  58. echo 0 > ~/pagen                        # neccesary
  59.  
  60.  
  61. function menu {
  62.  
  63. if [ $# -eq 0 ]                         # No page, then exit.
  64. then
  65. return
  66. fi
  67.  
  68. pages=($@)                              # Loading pages (.txt) into a variable (array).
  69. let pmax=${#pages[@]}-1                 # Number of text files (or pages).
  70.  
  71. if [ $(cat ~/pagen) -gt $pmax ]
  72. then
  73. echo $pmax > ~/pagen
  74. fi
  75.  
  76. echo "$@" > ~/pages
  77. echo $pmax > ~/pagem
  78.  
  79. tty=""
  80. if [ "$TERM" = linux ]                  #   tty : --height=50% --border=vertical
  81. then
  82. tty="--height=50% --border=vertical"
  83. fi
  84.  
  85. line=$(< $(type -p ${pages[$(< ~/pagen)]}) fzf --reverse --exact --no-color --no-info --expect=insert --cycle --bind "right:reload(menupageinc)" --bind "left:reload(menupagedec)" $tty)
  86.  
  87.  
  88. if [ "$line" = "" ]                     # fzf escape, ctrl-c, empty string
  89. then
  90. tput cuu1 && tput el                    # fake readline (up and delete line)
  91. return
  92. fi
  93.  
  94. key=$(head -1 <<< "$line")              # https://github.com/junegunn/fzf/wiki/examples#opening-files (example of using --expect)
  95. cmd=$(head -2 <<< "$line" | tail -1)    # https://github.com/junegunn/fzf/wiki/examples#opening-files (example of using --expect)
  96.  
  97. if [ "$key" = insert ]                  # only insertion
  98. then
  99. tput cuu1 && tput el                    # fake readline (up and delete line)
  100. read -p "${PS1@P}" -eri "$cmd" cmd
  101. fi
  102.  
  103. history -s $FUNCNAME $@
  104. history -s $cmd
  105.  
  106. tput cuu1 && tput el                    # fake readline (up and delete line)
  107. echo "${PS1@P}"$cmd
  108.  
  109. eval $cmd
  110.  
  111. }
  112.  
  113.  
  114. #   Personal examples.
  115.  
  116. alias m='menu cmd.txt pacman.txt backup.txt hdd.txt swap.txt install.txt'
  117.  
  118. #   Older one page settings (out of habit).
  119.  
  120. 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'
  121.  
  122.  
Advertisement
Add Comment
Please, Sign In to add comment