From d13db2149b032ea6c3b88a028baa11bb93c51dbf Mon Sep 17 00:00:00 2001 From: Steven Saus Date: Wed, 19 Aug 2020 16:46:29 -0400 Subject: [PATCH] Mansearch - to search man and cheatsheets, myman is a (FLAWED) display of multiple man and cheat --- mansearch-fzf-preview | 5 ++ mansearch.sh | 138 ++++++++++++++++++++++++++++++++++++++++++ myman.sh | 90 ++++++++++++++++++++++----- 3 files changed, 218 insertions(+), 15 deletions(-) create mode 100755 mansearch-fzf-preview create mode 100755 mansearch.sh mode change 100644 => 100755 myman.sh diff --git a/mansearch-fzf-preview b/mansearch-fzf-preview new file mode 100755 index 0000000..6a0db7d --- /dev/null +++ b/mansearch-fzf-preview @@ -0,0 +1,5 @@ +#!/bin/bash + +Instring="$@" +Command=$(echo "$Instring" | sed 's/ (/./g' | sed 's/)//g' | sed 's/:man:/:man -Pcat:/g' | awk -F ':' '{print $2 " " $1}') +eval "$Command" diff --git a/mansearch.sh b/mansearch.sh new file mode 100755 index 0000000..9d623d6 --- /dev/null +++ b/mansearch.sh @@ -0,0 +1,138 @@ +#!/bin/bash + +CacheFile=$HOME/.config/mansearch_cachefile +TempFile=$(mktemp) +Long=0 +NumResults=0 +declare -a ManName +declare -a ManDescription +declare -a TldrName +declare -a TldrDescription +declare -a CheatName +declare -a CheatDescription +SearchTerm="$@" +CliOnly="true" +Rebuild="false" +Run="true" +SCRIPTDIR="$( cd "$(dirname "$0")" ; pwd -P )" + +building_our_database (){ + #do this first + RawResults="" + echo "§ Reading in manpages" + RawResults=$(apropos . | grep -v -E '^.+ \(0\)') + #zsoelim (1) - satisfy .so requests in roff input + mapfile -t ManName < <(echo "$RawResults" | awk -F ' - ' '{print $1 }' | sed 's/[[:space:]]*$//') + mapfile -t ManDescription < <(echo "$RawResults" | awk -F ' - ' '{print $2 }' | sed 's/[[:space:]]*$//') + + #Now for cheatsheets + #Follow the same template for any other + echo "§ Reading in cheatsheets" + mapfile -t CheatName < <(cheat -l | cat | awk '{print $1 }'| sed 's/[[:space:]]*$//') + echo "§ Reading in tldr" + mapfile -t TldrName < <(tldr -l 2>/dev/null | awk '{print $1 }'| sed 's/[[:space:]]*$//') + echo "§ Adding descriptions to cheats" + # Now we're going to add descriptions to the cheatsheets... + for ((i = 0; i < ${#CheatName[@]}; i++));do + CheatDescription[$i]=$(echo "$RawResults" | rg -e "^${CheatName[$i]} "| awk -F '-' '{print $2 }'|head -1) + done + echo "§ Adding descriptions to tldr" + for ((i = 0; i < ${#TldrName[@]}; i++));do + TldrDescription[$i]=$(echo "$RawResults" | rg -e "^${TldrName[$i]} "| awk -F '-' '{print $2 }'|head -1) + done + echo "§ Finished supplementing cheatsheets and tldr" +} + +make_choices () { + echo "§ Compiling options" + for ((i = 0; i < ${#TldrName[@]}; i++));do + echo -e "${TldrName[$i]} :tldr: ${TldrDescription[$i]}" >> "$TempFile" + done + for ((i = 0; i < ${#CheatName[@]}; i++));do + echo -e "${CheatName[$i]} :cheats: ${CheatDescription[$i]}" >> "$TempFile" + done + for ((i = 0; i < ${#ManName[@]}; i++));do + echo -e "${ManName[$i]} :man: ${ManDescription[$i]}" >> "$TempFile" + done + echo "§ Sorting options" + cat "$TempFile" | sort | uniq -u > "$CacheFile" + rm -rf "$TempFile" +} + +############################################################################## +# Show the Help +############################################################################## +display_help(){ + echo "###################################################################" + echo "# mansearch.sh [-h|-c]" + echo "# -h show help " + echo "# -g GUI interface only. Default is CLI/TUI. " + echo "# NOTE: gui display is a bit wonky for some reason. " + echo "# -p Prefetch and compile options. " + echo "# -r Rebuild available options and run (raw mode). " + echo "###################################################################" +} + +choose_page (){ + + #cat mansearch_cachefile | fzf --no-hscroll -m --height 60% --border --ansi --no-bold --preview="/home/steven/documents/programming/multiple_scripts/mansearch-fzf-preview {}" + if [ "$CliOnly" == "true" ];then + #No Preview + #SelectedFile=$(cat "$CacheFile" | fzf --no-hscroll -m --height 60% --border --ansi --no-bold) + #With Preview + SelectedFile=$(cat "$CacheFile" | fzf --no-hscroll -m --height 60% --border --ansi --no-bold --preview="$SCRIPTDIR/mansearch-fzf-preview {}" | sed 's/ (/./g' | sed 's/)//g' | sed 's/:man:/:man -Pcat:/g' | awk -F ':' '{print $2 " " $1}') + if [ -z "$SelectedFile" ];then + exit 0 + else + eval "$SelectedFile" + fi + else + #use ROFI, not zenity + SelectedFile=$(cat "$CacheFile" | rofi -i -dmenu -p "Which manual?" -theme DarkBlue | sed 's/ (/./g' | sed 's/)//g' | sed 's/:man:/:man -Pcat:/g' | awk -F ':' '{print $2 " " $1}') + if [ -z "$SelectedFile" ];then + exit 0 + else + OutPut=$(eval "$SelectedFile") + rofi -location 1 -click-to-exit -lines 15 -fixed-num-lines -theme DarkBlue -e "$OutPut" + fi + fi +} + + +#TODO: Have some way to annotate cheatsheets yourself? +#TODO: have preview helper put in with the rest of this file +#TODO: GUI output formatted properly +############################################################################## +# Get options +############################################################################## + while [ $# -gt 0 ]; do + option="$1" + case $option in + -h) display_help + exit + shift ;; + -r) echo "§ Rebuilding cache before run; please be patient." + Rebuild="true" + Run="true" + shift ;; + -p) echo "§ Rebuilding cache only." + Rebuild="true" + Run="false" + shift ;; + -g) CliOnly="false" + shift ;; + esac + done +if [ ! -f "$CacheFile" ];then + echo "§ Cachefile not found; rebuilding." + Rebuild="true" +fi +if [ "$Rebuild" == "true" ];then + building_our_database + make_choices +fi +if [ "$Run" == "true" ];then + choose_page +else + exit 0 +fi diff --git a/myman.sh b/myman.sh old mode 100644 new mode 100755 index 31c4f49..7915d0a --- a/myman.sh +++ b/myman.sh @@ -1,17 +1,77 @@ +#!/bin/bash -#do this first -apropos . | grep -v -E '^.+ \(0\)' -#feed into array. Split on '-' everything to right is description with left side's $1 in awk -#then get -cheat -l | cat -#then run through array, see if any match from apropos array, and append definition array -#then do the same with -tldr -l -# then present them with the source available (man|cheat|tldr) -# if there was argv then pre-grep via that as well -# get howto do preview from macho - -#if nothing selected -#then use help to see if there's a return -=$(help "$@" 2> /dev/null) +Long=0 +NumResults=0 +declare -a short_array +SearchTerm="$@" +function print_header { +printf "%s" "" +printf "%s%s%s" "

" "$SearchTerm" "

" +} + +function print_first_short { +printf "%s%s%s" "" +} + + +function print_long { + if [ "${#short_array[@]}" -gt 0 ];then + ColSpanning="${#short_array[@]}" + ((ColSpanning++)) + printf "%s%s%s" "" +} + +function print_rest_short { + + for ((i = 1; i < ${#short_array[@]}; i++));do + printf "%s%s%s" "" + done + +} + +function print_end { + printf "%s" "
" "${short_array[0]}" "" + else + printf "%s" "" + fi + printf "%s%s" "$ManResult" "
" "${short_array[$i]}" "
" +} + + +#yes, I could do this in an array as well, I guess. +CheatResult=$(cheat "$@" 2> /dev/null) +if [ "$?" != "0" ];then + CheatResult="" +else + short_array[${#short_array[@]}]="$CheatResult" +fi +TldrResult=$(tldr "$@" 2> /dev/null) +if [ "$?" != "0" ];then + TldrResult="" +else + short_array[${#short_array[@]}]="$TldrResult" +fi +HelpResult=$(help "$@" 2> /dev/null) +if [ "$?" != "0" ];then + HelpResult="" +else + short_array[${#short_array[@]}]="$HelpResult" +fi +ManResult=$(/usr/bin/man $@ | mandoc -T html -O fragment 2> /dev/null) +if [ "$?" != "0" ];then + ManResult="" +else + ((Long++)) +fi + + +print_header +if [ "${#short_array[@]}" -gt 0 ];then + print_first_short +fi +if [ "$Long" -gt 0 ];then + print_long +fi +print_rest_short +print_end