commit 4796739cca836132f237e5c184bc7485352d5962
Author: Mahdi Mirzade <[email protected]>
Date: Sat, 16 Jul 2022 18:39:25 +0430
initial commit
Diffstat:
A | LICENSE | | | 21 | +++++++++++++++++++++ |
A | README | | | 61 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | dumb.sh | | | 88 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 170 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,21 @@
+MIT/X Consortium License
+
+© 2021-2022 Mahdi Mirzade <[email protected]>
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/README b/README
@@ -0,0 +1,61 @@
+dumbsh - dumb shell
+===================
+Your shell is dumb, make it smarter.
+
+
+Description
+-----------
+If you have multiple layouts in your keyboard mappings, sometimes
+you are typing in your second layout by mistake, like this:
+
+ عدشپث
+
+But you meant:
+
+ uname
+
+By default, your shell will not understand what you wanted to say (of
+course shell is the dumb one in this situation, you're never wrong)
+
+After installing this script, a layout table will be generated from
+your second layout to your first layout, using xmodmap. (Your first
+layout is english and your second layout is your other language)
+
+
+Installation
+------------
+Enter the following command to source the dumb.sh script once you've
+entered to your shell: (bash in this example)
+
+ echo "source path/to/dumb.sh" >> ~/.bashrc
+
+or if you're a zsh user:
+
+ echo "source path/to/dumb.sh" >> ~/.zshrc
+
+Restart your shell. Now when you are typing on your second layout, your
+commands will be translated using the layout table which you've generated
+on the first execution.
+
+This script is supported both on zsh and bash, but if you are using
+something else, you'll need to find a way to handle non-found commands
+in your shell, if there is a function for it in your shell, edit the script
+and add like how bash is added as a support.
+
+
+Configuration
+-------------
+This script can be configured through environmental variables:
+
+ DUMBSH_KEY_INDEX:
+ If set, the layout table will be generated to the place
+ you've set, but if not it sets to:
+ $XDG_DATA_HOME/dumbsh_table
+ $HOME/.local/share/dumbsh_table
+
+ DUMBSH_ASK_REPLACE:
+ If set to true, It'll ask before running the command,
+ or else it'll Automatically run the translated command.
+
+You can set these variables in your shell configuration/login profile in
+order to config this script.
diff --git a/dumb.sh b/dumb.sh
@@ -0,0 +1,88 @@
+#!/usr/bin/env bash
+#
+# dumbsh: Dumb Shell
+#
+# You'll need to set your keyboard layout to "us,*" in order to generate a layout table.
+# Example:
+# setxkbmap -layout us,ir
+#
+
+# Default variables
+DUMBSH_KEY_INDEX="${DUMBSH_KEY_INDEX:-${XDG_DATA_HOME:-$HOME/.local/share}/dumbsh_table}"
+DUMBSH_ASK_REPLACE="${DUMBSH_ASK_REPLACE:-TRUE}"
+
+# Generete layout table
+generateTable() {
+ KEYDEFS=$(cat /usr/local/include/X11/keysymdef.h)
+ getUnicode() {
+ echo "$KEYDEFS" | grep " XK_$1 " | grep -o "U+[0-9a-zA-Z]*" | sed 's/+//'
+ }
+ xmodmap -pke | awk '$4 != $6 {print $4" "$5" "$6" "$7}' | while read -r A B C D; do
+ if [ $(printf "$A" | wc -m) -ge 2 ]; then
+ A="\\$(getUnicode "$A")"
+ fi
+ if [ $(printf "$B" | wc -m) -ge 2 ]; then
+ B="\\$(getUnicode "$B")"
+ fi
+ if [ $(printf "$C" | wc -m) -ge 2 ]; then
+ C="\\$(getUnicode "$C")"
+ fi
+ if [ $(printf "$D" | wc -m) -ge 2 ]; then
+ D="\\$(getUnicode "$D")"
+ fi
+ [ -z "$C" ] || printf "$C=$A\n"
+ [ -z "$D" ] || printf "$D=$B\n"
+ unset A B C D
+ done
+}
+
+[ -f "$DUMBSH_KEY_INDEX" ] || generateTable > "$DUMBSH_KEY_INDEX"
+
+# Translate 2nd layout to English
+translate() {
+ INPUT=$1
+ OUTPUT=$INPUT
+ for (( i=0; i<${#INPUT}; i++ )); do
+ CHAR=${INPUT:$i:1}
+ NEWCHAR=$(grep -- "${CHAR}=" "$DUMBSH_KEY_INDEX" | cut -d '=' -f 2)
+ OUTPUT=${OUTPUT//${CHAR}/${NEWCHAR:-$CHAR}}
+ unset CHAR NEWCHAR
+ done
+ printf "$OUTPUT"
+}
+
+# Handle command not found
+command_not_found_handler() {
+ INPUT=$*
+ OUTPUT=$(translate "$INPUT")
+
+ if [ -z "$(diff <(printf "%s" "$INPUT") <(printf "%s" "$OUTPUT"))" ]; then
+ printf "\033[31mbash: %s: command not found\033[0m\n" "$INPUT"
+ else
+ case "$DUMBSH_ASK_REPLACE" in
+ [Tt][Rr][Uu][Ee]|1)
+ printf "\033[34mDid you mean %s? [y/N]\033[0m " "$OUTPUT"
+ read ANS
+ case "$ANS" in
+ [Yy][Ee][Ss]|Y|y)
+ printf "\033[32mRunning:\033[0m %s...\n" "$OUTPUT"
+ eval $OUTPUT
+ ;;
+ *)
+ return 1;
+ ;;
+ esac
+ ;;
+ *)
+ printf "\033[33m%s -> %s\033[0m\n" "$INPUT" "$OUTPUT"
+ printf "\033[32mRunning:\033[0m %s...\n" "$OUTPUT"
+ eval $OUTPUT
+ ;;
+ esac
+ fi
+
+ unset INPUT OUTPUT
+}
+
+# Handle command not found - bash version
+command_not_found_handle() { command_not_found_handler "$*"; }