@@ -222,64 +222,64 @@ else
222222fi
223223
224224cmd_cd () {
225- if [ $# -eq 0 ]; then
225+ if [ $# -eq 0 ]; then
226+ section=" "
227+ else
228+ new_section=$( echo " $input " | sed ' s/cd *//; s@/*$@@; s@^/*@@' )
229+ if [ -z " $new_section " ] || [ " .." = " $new_section " ]; then
226230 section=" "
227231 else
228- new_section=$( echo " $input " | sed ' s/cd *//; s@/*$@@; s@^/*@@' )
229- if [ -z " $new_section " ] || [ " .." = " $new_section " ]; then
230- section=" "
232+ valid_sections=$( get_list_of_sections)
233+ valid=no; for q in $valid_sections ; do [ " $q " = " $new_section /" ] && { valid=yes; break ; }; done
234+ if [ " $valid " = no ]; then
235+ echo " Invalid section: $new_section "
236+ echo " Valid sections:"
237+ echo $valid_sections | xargs printf " %-10s\n" | tr ' ' . | xargs -n 10 | sed ' s/\./ /g; s/^/ /'
231238 else
232- valid_sections=$( get_list_of_sections)
233- valid=no; for q in $valid_sections ; do [ " $q " = " $new_section /" ] && { valid=yes; break ; }; done
234- if [ " $valid " = no ]; then
235- echo " Invalid section: $new_section "
236- echo " Valid sections:"
237- echo $valid_sections | xargs printf " %-10s\n" | tr ' ' . | xargs -n 10 | sed ' s/\./ /g; s/^/ /'
238- else
239- section=" $new_section "
240- fi
239+ section=" $new_section "
241240 fi
242241 fi
242+ fi
243243}
244244
245245cmd_copy () {
246- if [ -z " $DISPLAY " ]; then
247- echo copy: supported only in the Desktop version
248- elif [ -z " $input " ]; then
249- echo copy: Make at least one query first.
250- else
251- curl -s " ${CHTSH_URL} " /" $( get_query_options " $query " ? T) " > " $TMP1 "
252- if [ " $is_macos " != yes ]; then
253- xsel -bi < " $TMP1 "
254- else
255- cat " $TMP1 " | pbcopy
256- fi
257- echo " copy: $( wc -l " $TMP1 " | awk ' {print $1}' ) lines copied to the selection"
258- fi
246+ if [ -z " $DISPLAY " ]; then
247+ echo copy: supported only in the Desktop version
248+ elif [ -z " $input " ]; then
249+ echo copy: Make at least one query first.
250+ else
251+ curl -s " ${CHTSH_URL} " /" $( get_query_options " $query " ? T) " > " $TMP1 "
252+ if [ " $is_macos " != yes ]; then
253+ xsel -bi < " $TMP1 "
254+ else
255+ cat " $TMP1 " | pbcopy
256+ fi
257+ echo " copy: $( wc -l " $TMP1 " | awk ' {print $1}' ) lines copied to the selection"
258+ fi
259259}
260260
261261cmd_ccopy () {
262- if [ -z " $DISPLAY " ]; then
263- echo copy: supported only in the Desktop version
264- elif [ -z " $input " ]; then
265- echo copy: Make at least one query first.
266- else
267- curl -s " ${CHTSH_URL} " /" $( get_query_options " $query " ? TQ) " > " $TMP1 "
268- if [ " $is_macos " != yes ]; then
269- xsel -bi < " $TMP1 "
270- else
271- cat " $TMP1 " | pbcopy
272- fi
273- echo " copy: $( wc -l " $TMP1 " | awk ' {print $1}' ) lines copied to the selection"
274- fi
262+ if [ -z " $DISPLAY " ]; then
263+ echo copy: supported only in the Desktop version
264+ elif [ -z " $input " ]; then
265+ echo copy: Make at least one query first.
266+ else
267+ curl -s " ${CHTSH_URL} " /" $( get_query_options " $query " ? TQ) " > " $TMP1 "
268+ if [ " $is_macos " != yes ]; then
269+ xsel -bi < " $TMP1 "
270+ else
271+ cat " $TMP1 " | pbcopy
272+ fi
273+ echo " copy: $( wc -l " $TMP1 " | awk ' {print $1}' ) lines copied to the selection"
274+ fi
275275}
276276
277277cmd_exit () {
278- exit 0
278+ exit 0
279279}
280280
281281cmd_help () {
282- cat << EOF
282+ cat << EOF
283283help - show this help
284284hush - do not show the 'help' string at start anymore
285285cd LANG - change the language context
@@ -299,54 +299,54 @@ EOF
299299}
300300
301301cmd_hush () {
302- mkdir -p $HOME /.cht.sh/ && touch $HOME /.cht.sh/.hushlogin && echo " Initial 'use help' message was disabled"
302+ mkdir -p $HOME /.cht.sh/ && touch $HOME /.cht.sh/.hushlogin && echo " Initial 'use help' message was disabled"
303303}
304304
305305cmd_id () {
306- id_file=" $HOME /.cht.sh/id"
306+ id_file=" $HOME /.cht.sh/id"
307307
308- if [ id = " $input " ]; then
309- new_id=" "
310- else
311- new_id=$( echo " $input " | sed ' s/id *//; s/ *$//; s/ /+/g' )
312- fi
313- if [ " $new_id " = remove ]; then
314- if [ -e " $id_file " ]; then
315- rm -f -- " $id_file " && echo " id is removed"
316- else
317- echo " id was not set, so you can't remove it"
318- fi
319- return
320- fi
321- if [ -n " $new_id " ] && [ reset != " $new_id " ] && [ $( /bin/echo -n " $new_id " | wc -c) -lt 16 ]; then
322- echo " ERROR: $new_id : Too short id. Minimal id length is 16. Use 'id reset' for a random id"
323- return
324- fi
325- if [ -z " $new_id " ]; then
326- # if new_id is not specified check if we have some id already
327- # if yes, just show it
328- # if not, generate a new id
329- if [ -e " $id_file " ]; then
330- echo $( awk ' $6 == "id" {print $NF}' < " $id_file " | tail -n 1)
331- return
332- else
333- new_id=reset
334- fi
335- fi
336- if [ " $new_id " = reset ]; then
337- new_id=$( gen_random_str 12)
338- else
339- echo WARNING: if someone gueses your id, he can read your cht.sh search history
340- fi
341- if [ -e " $id_file " ] && grep -q ' \tid\t[^\t][^\t]*$' " $id_file " 2> /dev/null; then
342- sed -i ' s/\tid\t[^\t][^\t]*$/ id ' " $new_id " ' /' " $id_file "
343- else
344- if ! [ -e " $id_file " ]; then
345- printf ' #\n\n' > " $id_file "
346- fi
347- printf " .cht.sh\tTRUE\t/\tTRUE\t0\tid\t$new_id \n" >> " $id_file "
348- fi
349- echo " $new_id "
308+ if [ id = " $input " ]; then
309+ new_id=" "
310+ else
311+ new_id=$( echo " $input " | sed ' s/id *//; s/ *$//; s/ /+/g' )
312+ fi
313+ if [ " $new_id " = remove ]; then
314+ if [ -e " $id_file " ]; then
315+ rm -f -- " $id_file " && echo " id is removed"
316+ else
317+ echo " id was not set, so you can't remove it"
318+ fi
319+ return
320+ fi
321+ if [ -n " $new_id " ] && [ reset != " $new_id " ] && [ $( /bin/echo -n " $new_id " | wc -c) -lt 16 ]; then
322+ echo " ERROR: $new_id : Too short id. Minimal id length is 16. Use 'id reset' for a random id"
323+ return
324+ fi
325+ if [ -z " $new_id " ]; then
326+ # if new_id is not specified check if we have some id already
327+ # if yes, just show it
328+ # if not, generate a new id
329+ if [ -e " $id_file " ]; then
330+ echo $( awk ' $6 == "id" {print $NF}' < " $id_file " | tail -n 1)
331+ return
332+ else
333+ new_id=reset
334+ fi
335+ fi
336+ if [ " $new_id " = reset ]; then
337+ new_id=$( gen_random_str 12)
338+ else
339+ echo WARNING: if someone gueses your id, he can read your cht.sh search history
340+ fi
341+ if [ -e " $id_file " ] && grep -q ' \tid\t[^\t][^\t]*$' " $id_file " 2> /dev/null; then
342+ sed -i ' s/\tid\t[^\t][^\t]*$/ id ' " $new_id " ' /' " $id_file "
343+ else
344+ if ! [ -e " $id_file " ]; then
345+ printf ' #\n\n' > " $id_file "
346+ fi
347+ printf " .cht.sh\tTRUE\t/\tTRUE\t0\tid\t$new_id \n" >> " $id_file "
348+ fi
349+ echo " $new_id "
350350}
351351
352352cmd_query () {
@@ -355,74 +355,74 @@ cmd_query() {
355355}
356356
357357cmd_stealth () {
358- if [ " $input " != stealth ]; then
359- arguments=$( echo " $input " | sed ' s/stealth //; s/ /\&/' )
360- fi
361- trap break INT
362- if [ " $is_macos " = yes ]; then
363- past=$( pbpaste)
364- else
365- past=$( xsel -o)
366- fi
367- printf " \033[0;31mstealth:\033[0m you are in the stealth mode; select any text in any window for a query\n"
368- printf " \033[0;31mstealth:\033[0m selections longer than $STEALTH_MAX_SELECTION_LENGTH words are ignored\n"
369- if [ -n " $arguments " ]; then
370- printf " \033[0;31mstealth:\033[0m query arguments: ?$arguments \n"
371- fi
372- printf " \033[0;31mstealth:\033[0m use ^C to leave this mode\n"
373- while true ; do
374- if [ " $is_macos " = yes ]; then
375- current=$( pbpaste)
376- else
377- current=$( xsel -o)
378- fi
379- if [ " $past " != " $current " ]; then
380- past=$current
381- current_text=" $( echo $current | tr -c ' [a-zA-Z0-9]' ' ' ) "
382- if [ $( echo $current_text | wc -w) -gt " $STEALTH_MAX_SELECTION_LENGTH " ]; then
383- echo " \033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
384- return
385- else
386- printf " \n\033[0;31mstealth: \033[7m $current_text \033[0m\n"
387- query=$( prepare_query " $section " " $current_text " " $arguments " )
388- do_query " $query "
389- fi
390- fi
391- sleep 1;
392- done
393- trap - INT
358+ if [ " $input " != stealth ]; then
359+ arguments=$( echo " $input " | sed ' s/stealth //; s/ /\&/' )
360+ fi
361+ trap break INT
362+ if [ " $is_macos " = yes ]; then
363+ past=$( pbpaste)
364+ else
365+ past=$( xsel -o)
366+ fi
367+ printf " \033[0;31mstealth:\033[0m you are in the stealth mode; select any text in any window for a query\n"
368+ printf " \033[0;31mstealth:\033[0m selections longer than $STEALTH_MAX_SELECTION_LENGTH words are ignored\n"
369+ if [ -n " $arguments " ]; then
370+ printf " \033[0;31mstealth:\033[0m query arguments: ?$arguments \n"
371+ fi
372+ printf " \033[0;31mstealth:\033[0m use ^C to leave this mode\n"
373+ while true ; do
374+ if [ " $is_macos " = yes ]; then
375+ current=$( pbpaste)
376+ else
377+ current=$( xsel -o)
378+ fi
379+ if [ " $past " != " $current " ]; then
380+ past=$current
381+ current_text=" $( echo $current | tr -c ' [a-zA-Z0-9]' ' ' ) "
382+ if [ $( echo $current_text | wc -w) -gt " $STEALTH_MAX_SELECTION_LENGTH " ]; then
383+ echo " \033[0;31mstealth:\033[0m selection length is longer than $STEALTH_MAX_SELECTION_LENGTH words; ignoring"
384+ return
385+ else
386+ printf " \n\033[0;31mstealth: \033[7m $current_text \033[0m\n"
387+ query=$( prepare_query " $section " " $current_text " " $arguments " )
388+ do_query " $query "
389+ fi
390+ fi
391+ sleep 1;
392+ done
393+ trap - INT
394394}
395395
396396cmd_update () {
397- [ -w " $0 " ] || { echo " The script is readonly; please update manually: curl -s " ${CHTSH_URL} " /:bash | sudo tee $0 " ; return ; }
398- TMP2=$( mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
399- curl -s " ${CHTSH_URL} " /:cht.sh > " $TMP2 "
400- if ! cmp " $0 " " $TMP2 " > /dev/null 2>&1 ; then
401- if grep -q ^__CHTSH_VERSION= " $TMP2 " ; then
402- # section was vaildated by us already
403- args=" --shell $section "
404- cp " $TMP2 " " $0 " && echo " Updated. Restarting..." && rm " $TMP2 " && CHEATSH_RESTART=1 exec " $0 " $args
405- else
406- echo " Something went wrong. Please update manually"
407- fi
408- else
409- echo " cht.sh is up to date. No update needed"
410- fi
411- rm -f " $TMP2 " > /dev/null 2>&1
397+ [ -w " $0 " ] || { echo " The script is readonly; please update manually: curl -s " ${CHTSH_URL} " /:bash | sudo tee $0 " ; return ; }
398+ TMP2=$( mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
399+ curl -s " ${CHTSH_URL} " /:cht.sh > " $TMP2 "
400+ if ! cmp " $0 " " $TMP2 " > /dev/null 2>&1 ; then
401+ if grep -q ^__CHTSH_VERSION= " $TMP2 " ; then
402+ # section was vaildated by us already
403+ args=" --shell $section "
404+ cp " $TMP2 " " $0 " && echo " Updated. Restarting..." && rm " $TMP2 " && CHEATSH_RESTART=1 exec " $0 " $args
405+ else
406+ echo " Something went wrong. Please update manually"
407+ fi
408+ else
409+ echo " cht.sh is up to date. No update needed"
410+ fi
411+ rm -f " $TMP2 " > /dev/null 2>&1
412412}
413413
414414cmd_version () {
415- insttime=$( ls -l -- " $0 " | sed ' s/ */ /g' | cut -d ' ' -f 6-8)
416- echo " cht.sh version $__CHTSH_VERSION of $__CHTSH_DATETIME ; installed at: $insttime "
417- TMP2=$( mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
418- if curl -s " ${CHTSH_URL} " /:cht.sh > " $TMP2 " ; then
419- if ! cmp " $0 " " $TMP2 " > /dev/null 2>&1 ; then
420- echo " Update needed (type 'update' for that)" .
421- else
422- echo " Up to date. No update needed"
423- fi
424- fi
425- rm -f " $TMP2 " > /dev/null 2>&1
415+ insttime=$( ls -l -- " $0 " | sed ' s/ */ /g' | cut -d ' ' -f 6-8)
416+ echo " cht.sh version $__CHTSH_VERSION of $__CHTSH_DATETIME ; installed at: $insttime "
417+ TMP2=$( mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
418+ if curl -s " ${CHTSH_URL} " /:cht.sh > " $TMP2 " ; then
419+ if ! cmp " $0 " " $TMP2 " > /dev/null 2>&1 ; then
420+ echo " Update needed (type 'update' for that)" .
421+ else
422+ echo " Up to date. No update needed"
423+ fi
424+ fi
425+ rm -f " $TMP2 " > /dev/null 2>&1
426426}
427427
428428TMP1=$( mktemp /tmp/cht.sh.XXXXXXXXXXXXX)
0 commit comments