dumbsh

Mahdi's build of dumbsh
git clone git://mahdi.pw/dumbsh.git
Log | Files | Refs | README | LICENSE

dumb.sh (2208B)


      1 #!/usr/bin/env bash
      2 #
      3 # dumbsh: Dumb Shell
      4 #
      5 # You'll need to set your keyboard layout to "us,*" in order to generate a layout table.
      6 # Example:
      7 #         setxkbmap -layout us,ir
      8 #
      9 
     10 # Default variables
     11 DUMBSH_KEY_INDEX="${DUMBSH_KEY_INDEX:-${XDG_DATA_HOME:-$HOME/.local/share}/dumbsh_table}"
     12 DUMBSH_ASK_REPLACE="${DUMBSH_ASK_REPLACE:-TRUE}"
     13 
     14 # Generete layout table
     15 generateTable() {
     16 	KEYDEFS=$(cat /usr/local/include/X11/keysymdef.h)
     17 	getUnicode() {
     18 		echo "$KEYDEFS" | grep " XK_$1 " | grep -o "U+[0-9a-zA-Z]*" | sed 's/+//'
     19 	}
     20 	xmodmap -pke | awk '$4 != $6 {print $4" "$5" "$6" "$7}' | while read -r A B C D; do
     21 		if [ $(printf "$A" | wc -m) -ge 2 ]; then
     22 			A="\\$(getUnicode "$A")"
     23 		fi
     24 		if [ $(printf "$B" | wc -m) -ge 2 ]; then
     25 			B="\\$(getUnicode "$B")"
     26 		fi
     27 		if [ $(printf "$C" | wc -m) -ge 2 ]; then
     28 			C="\\$(getUnicode "$C")"
     29 		fi
     30 		if [ $(printf "$D" | wc -m) -ge 2 ]; then
     31 			D="\\$(getUnicode "$D")"
     32 		fi
     33 		[ -z "$C" ] || printf "$C=$A\n"
     34 		[ -z "$D" ] || printf "$D=$B\n"
     35 		unset A B C D
     36 	done
     37 }
     38 
     39 [ -f "$DUMBSH_KEY_INDEX" ] || generateTable > "$DUMBSH_KEY_INDEX"
     40 
     41 # Translate 2nd layout to English
     42 translate() {
     43 	INPUT=$1
     44 	OUTPUT=$INPUT
     45 	for (( i=0; i<${#INPUT}; i++ )); do
     46 		CHAR=${INPUT:$i:1}
     47 		NEWCHAR=$(grep -- "${CHAR}=" "$DUMBSH_KEY_INDEX" | cut -d '=' -f 2)
     48 		OUTPUT=${OUTPUT//${CHAR}/${NEWCHAR:-$CHAR}}
     49 		unset CHAR NEWCHAR
     50 	done
     51 	printf "$OUTPUT"
     52 }
     53 
     54 # Handle command not found
     55 command_not_found_handler() {
     56 	INPUT=$*
     57 	OUTPUT=$(translate "$INPUT")
     58 
     59 	if [ -z "$(diff <(printf "%s" "$INPUT") <(printf "%s" "$OUTPUT"))" ]; then
     60 		printf "\033[31mbash: %s: command not found\033[0m\n" "$INPUT"
     61 	else
     62 		case "$DUMBSH_ASK_REPLACE" in
     63 			[Tt][Rr][Uu][Ee]|1)
     64 				printf "\033[34mDid you mean %s? [y/N]\033[0m " "$OUTPUT"
     65 				read ANS
     66 				case "$ANS" in
     67 					[Yy][Ee][Ss]|Y|y)
     68 						printf "\033[32mRunning:\033[0m %s...\n" "$OUTPUT"
     69 						eval $OUTPUT
     70 					;;
     71 					*)
     72 						return 1;
     73 					;;
     74 				esac
     75 			;;
     76 			*)
     77 				printf "\033[33m%s -> %s\033[0m\n" "$INPUT" "$OUTPUT"
     78 				printf "\033[32mRunning:\033[0m %s...\n" "$OUTPUT"
     79 				eval $OUTPUT
     80 			;;
     81 		esac
     82 	fi
     83 
     84 	unset INPUT OUTPUT
     85 }
     86 
     87 # Handle command not found - bash version
     88 command_not_found_handle() { command_not_found_handler "$*"; }