v1ral_ITS

Call this program in a shell followed by any command and it will return helpful command examples

Mar 15th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 22.54 KB | Source Code | 0 0
  1. #!/bin/bash
  2. # shellcheck disable=SC1117,SC2001
  3. #
  4. # [X] open section
  5. # [X] one shot mode
  6. # [X] usage info
  7. # [X] dependencies check
  8. # [X] help
  9. # [X] yank/y/copy/c
  10. # [X] Y/C
  11. # [X] eof problem
  12. # [X] more
  13. # [X] stealth mode
  14. #
  15. # here are several examples for the stealth mode:
  16. #
  17. # zip lists
  18. # list permutation
  19. # random list element
  20. # reverse a list
  21. # read json from file
  22. # append string to a file
  23. # run process in background
  24. # count words in text counter
  25. # group elements list
  26.  
  27. __CHTSH_VERSION=6
  28. __CHTSH_DATETIME="2019-06-05 18:00:46 +0200"
  29.  
  30. # cht.sh configuration loading
  31. #
  32. # configuration is stored in ~/.cht.sh/ (can be overridden by CHTSH_HOME)
  33. #
  34. CHTSH_HOME=${CHTSH:-"$HOME"/.cht.sh}
  35. [ -z "$CHTSH_CONF" ] && CHTSH_CONF=$CHTSH_HOME/cht.sh.conf
  36. # shellcheck disable=SC1090,SC2002
  37. [ -e "$CHTSH_CONF" ] && source "$CHTSH_CONF"
  38. [ -z "$CHTSH_URL" ] && CHTSH_URL=https://cht.sh
  39.  
  40. # currently we support only two modes:
  41. # * lite = access the server using curl
  42. # * auto = try standalone usage first
  43. CHTSH_MODE="$(cat "$CHTSH_HOME"/mode 2> /dev/null)"
  44. [ "$CHTSH_MODE" != lite ] && CHTSH_MODE=auto
  45. CHEATSH_INSTALLATION="$(cat "$CHTSH_HOME/standalone" 2> /dev/null)"
  46.  
  47.  
  48. export LESSSECURE=1
  49. STEALTH_MAX_SELECTION_LENGTH=5
  50.  
  51. case "$(uname -s)" in
  52.   Darwin) is_macos=yes ;;
  53.   *) is_macos=no ;;
  54. esac
  55.  
  56. # for KSH93
  57. # shellcheck disable=SC2034,SC2039,SC2168
  58. if echo "$KSH_VERSION" | grep -q ' 93' && ! local foo 2>/dev/null; then
  59.   alias local=typeset
  60. fi
  61.  
  62. fatal()
  63. {
  64.   echo "ERROR: $*" >&2
  65.   exit 1
  66. }
  67.  
  68. _say_what_i_do()
  69. {
  70.   [ -n "$LOG" ] && echo "$(date '+[%Y-%m-%d %H:%M%S]') $*" >> "$LOG"
  71.  
  72.   local this_prompt="\033[0;1;4;32m>>\033[0m"
  73.   printf "\n${this_prompt}%s\033[0m\n" " $* "
  74. }
  75.  
  76. cheatsh_standalone_install()
  77. {
  78.   # the function installs cheat.sh with the upstream repositories
  79.   # in the standalone mode
  80.   local installdir; installdir="$1"
  81.   local default_installdir="$HOME/.cheat.sh"
  82.  
  83.   if [ "$installdir" = help ]; then
  84.     cat <<EOF
  85. Install cheat.sh in the standalone mode.
  86.  
  87. After the installation, cheat.sh can be used locally, without accessing
  88. the public cheat.sh service, or it can be used in the server mode,
  89. where the newly installed server could be accessed by external clients
  90. in the same fashion as the public cheat.sh server.
  91.  
  92. During the installation, cheat.sh code as well as the cheat.sh upstream
  93. cheat sheets repositories will be fetched.
  94.  
  95. It takes approximately 1G of the disk space.
  96.  
  97. Default installation location:  ~/.cheat.sh/
  98. It can be overridden by a command line parameter to this script:
  99.  
  100.     ${0##*/} --standalone-install DIR
  101.  
  102. See cheat.sh/:standalone or https://github.com/chubin/cheat.sh/README.md
  103. for more information:
  104.  
  105.     cht.sh :standalone
  106.     curl cheat.sh/:standalone
  107.  
  108. After the installation is finished, the cht.sh shell client is switched
  109. to the auto mode, where it uses the local cheat.sh installation if possible.
  110. You can switch the mode with the --mode switch:
  111.  
  112.     cht.sh --mode lite      # use https://cheat.sh/ only
  113.     cht.sh --mode auto      # use local installation
  114.  
  115. For intallation and standalone usage, you need \`git\`, \`python\`,
  116. and \`virtualenv\` to be installed locally.
  117. EOF
  118.     return
  119.   fi
  120.  
  121.   local _exit_code=0
  122.  
  123.   local dependencies=(python git virtualenv)
  124.   for dep in "${dependencies[@]}"; do
  125.     command -v "$dep" >/dev/null || \
  126.     { echo "DEPENDENCY: \"$dep\" is needed to install cheat.sh in the standalone mode" >&2; _exit_code=1; }
  127.   done
  128.   [ "$_exit_code" -ne 0 ] && return "$_exit_code"
  129.  
  130.   while true; do
  131.     echo -n "Where should cheat.sh be installed [$default_installdir]? "; read -r installdir
  132.     [ -n "$installdir" ] || installdir="$default_installdir"
  133.  
  134.     if [ "$installdir" = y ] \
  135.         || [ "$installdir" = Y ] \
  136.         || [ "$(echo "$installdir" | tr "[:upper:]" "[:lower:]")" = yes ]
  137.     then
  138.       echo Please enter the directory name
  139.       echo If it was the directory name already, please prepend it with \"./\": "./$installdir"
  140.     else
  141.       break
  142.     fi
  143.   done
  144.  
  145.   if [ -e "$installdir" ]; then
  146.     echo "ERROR: Installation directory [$installdir] exists already"
  147.     echo "Please remove it first before continuing"
  148.     return 1
  149.   fi
  150.  
  151.   if ! mkdir -p "$installdir"; then
  152.     echo "ERROR: Could not create the installation directory \"$installdir\""
  153.     echo "ERROR: Please check the permissions and start the script again"
  154.     return 1
  155.   fi
  156.  
  157.   local space_needed=700
  158.   local space_available; space_available=$(($(df -k "$installdir" | awk '{print $4}' | tail -1)/1024))
  159.  
  160.   if [ "$space_available" -lt "$space_needed" ]; then
  161.     echo "ERROR: Installation directory has no enough space (needed: ${space_needed}M, available: ${space_available}M"
  162.     echo "ERROR: Please clean up and start the script again"
  163.     rmdir "$installdir"
  164.     return 1
  165.   fi
  166.  
  167.   _say_what_i_do Cloning cheat.sh locally
  168.   local url=https://github.com/chubin/cheat.sh
  169.   rmdir "$installdir"
  170.   git clone "$url" "$installdir" || fatal Could not clone "$url" with git into "$installdir"
  171.   cd "$installdir" || fatal "Cannot cd into $installdir"
  172.  
  173.   # after the repository cloned, we may have the log directory
  174.   # and we can write our installation log into it
  175.   mkdir -p "$installdir/log/"
  176.   LOG="$installdir/log/install.log"
  177.  
  178.   # we use tee everywhere so we should set -o pipefail
  179.   set -o pipefail
  180.  
  181.   # currently the script uses python 2,
  182.   # but cheat.sh supports python 3 too
  183.   # if you want to switch it to python 3
  184.   # set PYTHON2 to NO:
  185.   # PYTHON2=NO
  186.   #
  187.   PYTHON2=YES
  188.   if [[ $PYTHON2 = YES ]]; then
  189.     python="python2"
  190.     pip="pip"
  191.     virtualenv_python3_option=()
  192.   else
  193.     python="python3"
  194.     pip="pip3"
  195.     virtualenv_python3_option=(-p python3)
  196.   fi
  197.  
  198.   _say_what_i_do Creating virtual environment
  199.   "$python" "$(command -v virtualenv)" "${virtualenv_python3_option[@]}" ve \
  200.       || fatal Could not create virtual environment with "python2 $(command -v virtualenv) ve"
  201.  
  202.   # rapidfuzz does not support Python 2,
  203.   # so if we are using Python 2, install fuzzywuzzy instead
  204.   if [[ $PYTHON2 = YES ]]; then
  205.     sed -i s/rapidfuzz/fuzzywuzzy/ requirements.txt
  206.     echo "python-Levenshtein" >> requirements.txt
  207.   fi
  208.  
  209.   _say_what_i_do Installing python requirements into the virtual environment
  210.   ve/bin/"$pip" install -r requirements.txt > "$LOG" \
  211.       || {
  212.  
  213.     echo "ERROR:"
  214.     echo "---"
  215.     tail -n 10 "$LOG"
  216.     echo "---"
  217.     echo "See $LOG for more"
  218.     fatal Could not install python dependencies into the virtual environment
  219.   }
  220.   echo "$(ve/bin/"$pip" freeze | wc -l) dependencies were successfully installed"
  221.  
  222.   _say_what_i_do Fetching the upstream cheat sheets repositories
  223.   ve/bin/python lib/fetch.py fetch-all | tee -a "$LOG"
  224.  
  225.   _say_what_i_do Running self-tests
  226.   (
  227.     cd tests || exit
  228.  
  229.     if CHEATSH_TEST_STANDALONE=YES \
  230.        CHEATSH_TEST_SKIP_ONLINE=NO \
  231.        CHEATSH_TEST_SHOW_DETAILS=NO \
  232.        PYTHON=../ve/bin/python bash run-tests.sh | tee -a "$LOG"
  233.     then
  234.       printf "\033[0;32m%s\033[0m\n" "SUCCESS"
  235.     else
  236.       printf "\033[0;31m%s\033[0m\n" "FAILED"
  237.       echo "Some tests were failed. Run the tests manually for further investigation:"
  238.       echo "  cd $PWD; bash run-tests.sh)"
  239.     fi
  240.   )
  241.  
  242.   mkdir -p "$CHTSH_HOME"
  243.   echo "$installdir" > "$CHTSH_HOME/standalone"
  244.   echo auto > "$CHTSH_HOME/mode"
  245.  
  246.   _say_what_i_do Done
  247.  
  248.   local v1; v1=$(printf "\033[0;1;32m")
  249.   local v2; v2=$(printf "\033[0m")
  250.  
  251.   cat <<EOF | sed "s/{/$v1/; s/}/$v2/"
  252.  
  253. {      _      }
  254. {     \\ \\   }     The installation is successfully finished.
  255. {      \\ \\  }
  256. {      / /    }   Now you can use cheat.sh in the standalone mode,
  257. {     /_/     }   or you can start your own cheat.sh server.
  258.  
  259.  
  260. Now the cht.sh shell client is switched to the auto mode, where it uses
  261. the local cheat.sh installation if possible.
  262. You can switch the mode with the --mode switch:
  263.  
  264.     cht.sh --mode lite      # use https://cheat.sh/ only
  265.     cht.sh --mode auto      # use local installation
  266.  
  267. You can add your own cheat sheets repository (config is in \`etc/config.yaml\`),
  268. or create new cheat sheets adapters (in \`lib/adapters\`).
  269.  
  270. To update local copies of cheat sheets repositores on a regular basis,
  271. add the following line to your user crontab (crontab -e):
  272.  
  273.     10 * * * * $installdir/ve/bin/python $installdir/lib/fetch.py update-all
  274.  
  275. All cheat sheets will be automatically actualized each hour.
  276.  
  277. If you are running a server reachable from the Internet, it can be instantly
  278. notified via a HTTP request about any cheat sheets changes. For that, please
  279. open an issue on the cheat.sh project repository [github.com/chubin/cheat.sh]
  280. with the ENTRY-POINT from the URL https://ENTRY-POINT/:actualize specified
  281. EOF
  282. }
  283.  
  284. chtsh_mode()
  285. {
  286.   local mode="$1"
  287.  
  288.   local text; text=$(
  289.     echo "  auto    use the standalone installation first"
  290.     echo "  lite    use the cheat sheets server directly"
  291.   )
  292.  
  293.   if [ -z "$mode" ]; then
  294.     echo "current mode: $CHTSH_MODE ($(printf "%s" "$text" | grep "$CHTSH_MODE" | sed "s/$CHTSH_MODE//; s/^ *//; s/ \+/ /"))"
  295.     if [ -d "$CHEATSH_INSTALLATION" ]; then
  296.       echo "cheat.sh standalone installation: $CHEATSH_INSTALLATION"
  297.     else
  298.       echo 'cheat.sh standalone installation not found; falling back to the "lite" mode'
  299.     fi
  300.   elif [ "$mode" = auto ] || [ "$mode" = lite ]; then
  301.     if [ "$mode" = "$CHTSH_MODE" ]; then
  302.       echo "The configured mode was \"$CHTSH_MODE\"; nothing changed"
  303.     else
  304.       mkdir -p "$CHTSH_HOME"
  305.       echo "$mode" > "$CHTSH_HOME/mode"
  306.       echo "Configured mode: $mode"
  307.     fi
  308.   else
  309.     echo "Unknown mode: $mode"
  310.     echo Supported modes:
  311.     echo "  auto    use the standalone installation first"
  312.     echo "  lite    use the cheat sheets server directly"
  313.   fi
  314. }
  315.  
  316. get_query_options()
  317. {
  318.   local query="$*"
  319.   if [ -n "$CHTSH_QUERY_OPTIONS" ]; then
  320.     case $query in
  321.       *\?*)   query="$query&${CHTSH_QUERY_OPTIONS}";;
  322.       *)      query="$query?${CHTSH_QUERY_OPTIONS}";;
  323.     esac
  324.   fi
  325.   printf "%s" "$query"
  326. }
  327.  
  328. do_query()
  329. {
  330.   local query="$*"
  331.   local b_opts=
  332.   local uri="${CHTSH_URL}/\"\$(get_query_options $query)\""
  333.  
  334.   if [ -e "$HOME/.cht.sh/id" ]; then
  335.     b_opts="-b \"\$HOME/.cht.sh/id\""
  336.   fi
  337.  
  338.   eval curl "$b_opts" -s "$uri" > "$TMP1"
  339.  
  340.   if [ -z "$lines" ] || [ "$(wc -l "$TMP1" | awk '{print $1}')" -lt "$lines" ]; then
  341.     cat "$TMP1"
  342.   else
  343.     ${PAGER:-$defpager} "$TMP1"
  344.   fi
  345. }
  346.  
  347. prepare_query()
  348. {
  349.   local section="$1"; shift
  350.   local input="$1"; shift
  351.   local arguments="$1"
  352.  
  353.   local query
  354.   if [ -z "$section" ] || [ x"${input}" != x"${input#/}" ]; then
  355.     query=$(printf %s "$input" | sed 's@ @/@; s@ @+@g')
  356.   else
  357.     query=$(printf %s "$section/$input" | sed 's@ @+@g')
  358.   fi
  359.  
  360.   [ -n "$arguments" ] && arguments="?$arguments"
  361.   printf %s "$query$arguments"
  362. }
  363.  
  364. get_list_of_sections()
  365. {
  366.   curl -s "${CHTSH_URL}"/:list | grep -v '/.*/' | grep '/$' | xargs
  367. }
  368.  
  369. gen_random_str()
  370. (
  371.   len=$1
  372.   if command -v openssl >/dev/null; then
  373.     openssl rand -base64 $((len*3/4)) | awk -v ORS='' //
  374.   else
  375.     rdev=/dev/urandom
  376.     for d in /dev/{srandom,random,arandom}; do
  377.       test -r "$d" && rdev=$d
  378.     done
  379.     if command -v hexdump >/dev/null; then
  380.       hexdump -vn $((len/2)) -e '1/1 "%02X" 1 ""' "$rdev"
  381.     elif command -v xxd >/dev/null; then
  382.       xxd -l $((len/2)) -ps "$rdev" | awk -v ORS='' //
  383.     else
  384.       cd /tmp || { echo Cannot cd into /tmp >&2; exit 1; }
  385.       s=
  386.       # shellcheck disable=SC2000
  387.       while [ "$(echo "$s" | wc -c)" -lt "$len" ]; do
  388.         s="$s$(mktemp -u XXXXXXXXXX)"
  389.       done
  390.       printf "%.${len}s" "$s"
  391.     fi
  392.   fi
  393. )
  394.  
  395. if [ "$CHTSH_MODE" = auto ] && [ -d "$CHEATSH_INSTALLATION" ]; then
  396.   curl() {
  397.     # ignoring all options
  398.     # currently the standalone.py does not support them anyway
  399.     local opt
  400.     while getopts "b:s" opt; do
  401.       :
  402.     done
  403.     shift $((OPTIND - 1))
  404.  
  405.     local url; url="$1"; shift
  406.     PYTHONIOENCODING=UTF-8 "$CHEATSH_INSTALLATION/ve/bin/python" "$CHEATSH_INSTALLATION/lib/standalone.py" "${url#"$CHTSH_URL"}" "$@"
  407.   }
  408. elif [ "$(uname -s)" = OpenBSD ] && [ -x /usr/bin/ftp ]; then
  409.   # any better test not involving either OS matching or actual query?
  410.   curl() {
  411.     local opt args="-o -"
  412.     while getopts "b:s" opt; do
  413.       case $opt in
  414.         b) args="$args -c $OPTARG";;
  415.         s) args="$args -M -V";;
  416.         *) echo "internal error: unsupported cURL option '$opt'" >&2; exit 1;;
  417.       esac
  418.     done
  419.     shift $((OPTIND - 1))
  420.     /usr/bin/ftp "$args" "$@"
  421.   }
  422. else
  423.   command -v curl   >/dev/null || { echo 'DEPENDENCY: install "curl" to use cht.sh' >&2; exit 1; }
  424.   _CURL=$(command -v curl)
  425.   if [ x"$CHTSH_CURL_OPTIONS" != x ]; then
  426.     curl() {
  427.       $_CURL "${CHTSH_CURL_OPTIONS}" "$@"
  428.     }
  429.   fi
  430. fi
  431.  
  432. if [ "$1" = --read ]; then
  433.   read -r a || a="exit"
  434.   printf "%s\n" "$a"
  435.   exit 0
  436. elif [ x"$1" = x--help ] || [ -z "$1" ]; then
  437.  
  438.   n=${0##*/}
  439.   s=$(echo "$n" | sed "s/./ /"g)
  440.  
  441.   cat <<EOF
  442. Usage:
  443.  
  444.     $n [OPTIONS|QUERY]
  445.  
  446. Options:
  447.  
  448.     QUERY                   process QUERY and exit
  449.  
  450.     --help                  show this help
  451.     --shell [LANG]          shell mode (open LANG if specified)
  452.  
  453.     --standalone-install [DIR|help]
  454.                             install cheat.sh in the standalone mode
  455.                             (by default, into ~/.cheat.sh/)
  456.  
  457.     --mode [auto|lite]      set (or display) mode of operation
  458.                             * auto - prefer the local installation
  459.                             * lite - use the cheat sheet server
  460.  
  461. EOF
  462.   exit 0
  463. elif [ x"$1" = x--shell ]; then
  464.   shell_mode=yes
  465.   shift
  466. elif [ x"$1" = x--standalone-install ]; then
  467.   shift
  468.   cheatsh_standalone_install "$@"
  469.   exit "$?"
  470. elif [ x"$1" = x--mode ]; then
  471.   shift
  472.   chtsh_mode "$@"
  473.   exit "$?"
  474. fi
  475.  
  476. prompt="cht.sh"
  477. opts=""
  478. input=""
  479. for o; do
  480.   if [ x"$o" != x"${o#-}" ]; then
  481.     opts="${opts}${o#-}"
  482.   else
  483.     input="$input $o"
  484.   fi
  485. done
  486. query=$(echo "$input" | sed 's@ *$@@; s@^ *@@; s@ @/@; s@ @+@g')
  487.  
  488. if [ "$shell_mode" != yes ]; then
  489.   curl -s "${CHTSH_URL}"/"$(get_query_options "$query")"
  490.   exit 0
  491. else
  492.   new_section="$1"
  493.   valid_sections=$(get_list_of_sections)
  494.   valid=no; for q in $valid_sections; do [ "$q" = "$new_section/" ] && { valid=yes; break; }; done
  495.  
  496.   if [ "$valid" = yes ]; then
  497.     section="$new_section"
  498.     # shellcheck disable=SC2001
  499.     this_query="$(echo "$input" | sed 's@ *[^ ]* *@@')"
  500.     this_prompt="\033[0;32mcht.sh/$section>\033[0m "
  501.   else
  502.     this_query="$input"
  503.     this_prompt="\033[0;32mcht.sh>\033[0m "
  504.   fi
  505.   if [ -n "$this_query" ] && [ -z "$CHEATSH_RESTART" ]; then
  506.     printf "$this_prompt$this_query\n"
  507.     curl -s "${CHTSH_URL}"/"$(get_query_options "$query")"
  508.   fi
  509. fi
  510.  
  511. if [ "$is_macos" != yes ]; then
  512.   command -v xsel >/dev/null ||   echo 'DEPENDENCY: please install "xsel" for "copy"' >&2
  513. fi
  514. command -v rlwrap >/dev/null || { echo 'DEPENDENCY: install "rlwrap" to use cht.sh in the shell mode' >&2; exit 1; }
  515.  
  516. mkdir -p "$HOME/.cht.sh/"
  517. lines=$(tput lines)
  518.  
  519. if command -v less >/dev/null; then
  520.   defpager="less -R"
  521. elif command -v more >/dev/null; then
  522.   defpager="more"
  523. else
  524.   defpager="cat"
  525. fi
  526.  
  527. cmd_cd() {
  528.   if [ $# -eq 0 ]; then
  529.     section=""
  530.   else
  531.     new_section=$(echo "$input" | sed 's/cd  *//; s@/*$@@; s@^/*@@')
  532.     if [ -z "$new_section" ] || [ ".." = "$new_section" ]; then
  533.       section=""
  534.     else
  535.       valid_sections=$(get_list_of_sections)
  536.       valid=no; for q in $valid_sections; do [ "$q" = "$new_section/" ] && { valid=yes; break; }; done
  537.       if [ "$valid" = no ]; then
  538.         echo "Invalid section: $new_section"
  539.         echo "Valid sections:"
  540.         echo "$valid_sections" \
  541.             | xargs printf "%-10s\n" \
  542.             | tr ' ' .  \
  543.             | xargs -n 10 \
  544.             | sed 's/\./ /g; s/^/  /'
  545.       else
  546.         section="$new_section"
  547.       fi
  548.     fi
  549.   fi
  550. }
  551.  
  552. cmd_copy() {
  553.   if [ -z "$DISPLAY" ]; then
  554.     echo copy: supported only in the Desktop version
  555.   elif [ -z "$input" ]; then
  556.     echo copy: Make at least one query first.
  557.   else
  558.     curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?T)" > "$TMP1"
  559.     if [ "$is_macos" != yes ]; then
  560.       xsel -bi < "$TMP1"
  561.     else
  562.       pbcopy < "$TMP1"
  563.     fi
  564.     echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
  565.   fi
  566. }
  567.  
  568. cmd_ccopy() {
  569.   if [ -z "$DISPLAY" ]; then
  570.     echo copy: supported only in the Desktop version
  571.   elif [ -z "$input" ]; then
  572.     echo copy: Make at least one query first.
  573.   else
  574.     curl -s "${CHTSH_URL}"/"$(get_query_options "$query"?TQ)" > "$TMP1"
  575.     if [ "$is_macos" != yes ]; then
  576.       xsel -bi < "$TMP1"
  577.     else
  578.       pbcopy < "$TMP1"
  579.     fi
  580.     echo "copy: $(wc -l "$TMP1" | awk '{print $1}') lines copied to the selection"
  581.   fi
  582. }
  583.  
  584. cmd_exit() {
  585.   exit 0
  586. }
  587.  
  588. cmd_help() {
  589.   cat <<EOF
  590. help    - show this help
  591. hush    - do not show the 'help' string at start anymore
  592. cd LANG - change the language context
  593. copy    - copy the last answer in the clipboard (aliases: yank, y, c)
  594. ccopy   - copy the last answer w/o comments (cut comments; aliases: cc, Y, C)
  595. exit    - exit the cheat shell (aliases: quit, ^D)
  596. id [ID] - set/show an unique session id ("reset" to reset, "remove" to remove)
  597. stealth - stealth mode (automatic queries for selected text)
  598. update  - self update (only if the scriptfile is writeable)
  599. version - show current cht.sh version
  600. /:help  - service help
  601. QUERY   - space separated query staring (examples are below)
  602.               cht.sh> python zip list
  603.               cht.sh/python> zip list
  604.               cht.sh/go> /python zip list
  605. EOF
  606. }
  607.  
  608. cmd_hush() {
  609.   mkdir -p "$HOME/.cht.sh/" && touch "$HOME/.cht.sh/.hushlogin" && echo "Initial 'use help' message was disabled"
  610. }
  611.  
  612. cmd_id() {
  613.   id_file="$HOME/.cht.sh/id"
  614.  
  615.   if [ id = "$input" ]; then
  616.     new_id=""
  617.   else
  618.     new_id=$(echo "$input" | sed 's/id  *//; s/ *$//; s/ /+/g')
  619.   fi
  620.   if [ "$new_id" = remove ]; then
  621.     if [ -e "$id_file" ]; then
  622.       rm -f -- "$id_file" && echo "id is removed"
  623.     else
  624.       echo "id was not set, so you can't remove it"
  625.     fi
  626.     return
  627.   fi
  628.   if [ -n "$new_id" ] && [ reset != "$new_id" ] && [ "$(/bin/echo -n "$new_id" | wc -c)" -lt 16 ]; then
  629.     echo "ERROR: $new_id: Too short id. Minimal id length is 16. Use 'id reset' for a random id"
  630.     return
  631.   fi
  632.   if [ -z "$new_id" ]; then
  633.     # if new_id is not specified check if we have some id already
  634.     # if yes, just show it
  635.     # if not, generate a new id
  636.     if [ -e "$id_file" ]; then
  637.       awk '$6 == "id" {print $NF}' <"$id_file" | tail -n 1
  638.       return
  639.     else
  640.       new_id=reset
  641.     fi
  642.   fi
  643.   if [ "$new_id" = reset ]; then
  644.     new_id=$(gen_random_str 12)
  645.   else
  646.     echo WARNING: if someone gueses your id, he can read your cht.sh search history
  647.   fi
  648.   if [ -e "$id_file" ] && grep -q '\tid\t[^\t][^\t]*$' "$id_file" 2> /dev/null; then
  649.     sed -i 's/\tid\t[^\t][^\t]*$/ id '"$new_id"'/' "$id_file"
  650.   else
  651.     if ! [ -e "$id_file" ]; then
  652.       printf '#\n\n' > "$id_file"
  653.     fi
  654.     printf ".cht.sh\tTRUE\t/\tTRUE\t0\tid\t$new_id\n" >> "$id_file"
  655.   fi
  656.   echo "$new_id"
  657. }
  658.  
  659. cmd_query() {
  660.   query=$(prepare_query "$section" "$input")
  661.   do_query "$query"
  662. }
  663.  
  664. cmd_stealth() {
  665.   if [ "$input" != stealth ]; then
  666.     arguments=$(echo "$input" | sed 's/stealth //; s/ /\&/')
  667.   fi
  668.   trap break INT
  669.   if [ "$is_macos" = yes ]; then
  670.     past=$(pbpaste)
  671.   else
  672.     past=$(xsel -o)
  673.   fi
  674.   printf "\033[0;31mstealth:\033[0m you are in the stealth mode; select any text in any window for a query\n"
  675.   printf "\033[0;31mstealth:\033[0m selections longer than $STEALTH_MAX_SELECTION_LENGTH words are ignored\n"
  676.   if [ -n "$arguments" ]; then
  677.     printf "\033[0;31mstealth:\033[0m query arguments: ?$arguments\n"
  678.   fi
  679.   printf "\033[0;31mstealth:\033[0m use ^C to leave this mode\n"
  680.   while true; do
  681.     if [ "$is_macos" = yes ]; then
  682.       current=$(pbpaste)
  683.     else
  684.       current=$(xsel -o)
  685.     fi
  686.     if [ "$past" != "$current" ]; then
  687.       past=$current
  688.       current_text="$(echo $current | tr -c '[a-zA-Z0-9]' ' ')"
  689.       if [ "$(echo "$current_text" | wc -w)" -gt "$STEALTH_MAX_SELECTION_LENGTH" ]; then
  690.         echo "\033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
  691.         continue
  692.       else
  693.         printf "\n\033[0;31mstealth: \033[7m $current_text\033[0m\n"
  694.         query=$(prepare_query "$section" "$current_text" "$arguments")
  695.         do_query "$query"
  696.       fi
  697.     fi
  698.     sleep 1;
  699.   done
  700.   trap - INT
  701. }
  702.  
  703. cmd_update() {
  704.   [ -w "$0" ] || { echo "The script is read only; please update manually: curl -s ${CHTSH_URL}/:cht.sh | sudo tee $0"; return; }
  705.   TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
  706.   curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"
  707.   if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
  708.     if grep -q ^__CHTSH_VERSION= "$TMP2"; then
  709.       # section was vaildated by us already
  710.       args=(--shell "$section")
  711.       cp "$TMP2" "$0" && echo "Updated. Restarting..." && rm "$TMP2" && CHEATSH_RESTART=1 exec "$0" "${args[@]}"
  712.     else
  713.       echo "Something went wrong. Please update manually"
  714.     fi
  715.   else
  716.     echo "cht.sh is up to date. No update needed"
  717.   fi
  718.   rm -f "$TMP2" > /dev/null 2>&1
  719. }
  720.  
  721. cmd_version() {
  722.   insttime=$(ls -l -- "$0" | sed 's/  */ /g' | cut -d ' ' -f 6-8)
  723.   echo "cht.sh version $__CHTSH_VERSION of $__CHTSH_DATETIME; installed at: $insttime"
  724.   TMP2=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
  725.   if curl -s "${CHTSH_URL}"/:cht.sh > "$TMP2"; then
  726.     if ! cmp "$0" "$TMP2" > /dev/null 2>&1; then
  727.       echo "Update needed (type 'update' for that)".
  728.     else
  729.       echo "Up to date. No update needed"
  730.     fi
  731.   fi
  732.   rm -f "$TMP2" > /dev/null 2>&1
  733. }
  734.  
  735. TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
  736. trap 'rm -f $TMP1 $TMP2' EXIT
  737. trap 'true' INT
  738.  
  739. if ! [ -e "$HOME/.cht.sh/.hushlogin" ] && [ -z "$this_query" ]; then
  740.   echo "type 'help' for the cht.sh shell help"
  741. fi
  742.  
  743. while true; do
  744.   if [ "$section" != "" ]; then
  745.     full_prompt="$prompt/$section> "
  746.   else
  747.     full_prompt="$prompt> "
  748.   fi
  749.  
  750.   input=$(
  751.     rlwrap -H "$HOME/.cht.sh/history" -pgreen -C cht.sh -S "$full_prompt" bash "$0" --read | sed 's/ *#.*//'
  752.   )
  753.  
  754.   cmd_name=${input%% *}
  755.   cmd_args=${input#* }
  756.   case $cmd_name in
  757.     "")             continue;;   # skip empty input lines
  758.     '?'|h|help)     cmd_name=help;;
  759.     hush)           cmd_name=hush;;
  760.     cd)             cmd_name="cd";;
  761.     exit|quit)      cmd_name="exit";;
  762.     copy|yank|c|y)  cmd_name=copy;;
  763.     ccopy|cc|C|Y)   cmd_name=ccopy;;
  764.     id)             cmd_name=id;;
  765.     stealth)        cmd_name=stealth;;
  766.     update)         cmd_name=update;;
  767.     version)        cmd_name=version;;
  768.     *)              cmd_name="query"; cmd_args="$input";;
  769.   esac
  770.   "cmd_$cmd_name" $cmd_args
  771. done
  772.  
Add Comment
Please, Sign In to add comment