-
Notifications
You must be signed in to change notification settings - Fork 1
/
layout-inverter
executable file
·40 lines (29 loc) · 1.97 KB
/
layout-inverter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
# First attemtp to mimic Caramba Switcher for Linux
# There is an issue:
# only selected by mouse goes to primary, but not selection by ctrl+shift+arrow.
# Because I did not found a way how to write to primary using keyboard selection
# I will temporary use ctrl+c to clipboard.
xsel --clipboard --output | xsel --secondary --input # save user's clipboard
sleep 0.2
xdotool key ctrl+c
sleep 0.2
xsel --clipboard --output | xsel --primary --input # send clipboard to primary
sleep 0.2
xsel --secondary --output | xsel --clipboard --input # and restore user's clipboard
# xsel -sc # just to keep it clear, not necessary
SOURCE_STRING=`xclip -o`
# SOURCE_STRING="Ghbdtn привет" # for debugging
# echo $SOURCE_STRING
# https://unix.stackexchange.com/questions/360571/is-there-any-library-or-tool-to-translate-string-from-one-keyboard-layout-to-a
RESULT_STRING=$( echo "$SOURCE_STRING" | iconv -t cp1251 | tr \
$(echo "[йцукенгшщзхъфывапролджэ\\ячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭ/ЯЧСМИТЬБЮ,\"№;:?Ёёqwertyuiop\[\]asdfghjkl;'\\zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:\"|ZXCVBNM<>?@#$^&~\`" | iconv -t cp1251) \
$(echo "[qwertyuiop\[\]asdfghjkl;'\\zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:\"|ZXCVBNM<>?@#$^&~\`йцукенгшщзхъфывапролджэ\\ячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭ/ЯЧСМИТЬБЮ,\"№;:?Ёё]" | iconv -t cp1251) \
| iconv -f WINDOWS-1251 -t UTF-8 )
# echo ghbdtn | iconv -t cp1251 | tr g $(echo "п" | iconv -t cp1251) | iconv -f WINDOWS-1251 -t UTF-8 # это работает!
# echo RESULT_STRING=$RESULT_STRING
xclip -i <<< $RESULT_STRING
sleep 0.5 # do not remove
xdotool key Delete type --clearmodifiers --delay 0 "`xsel -o`" # without delete key it messes up the first letter of insertion
# This is for watching at selections (for debugging). Run from separate terminal.
# watch -n0.2 'echo -e "CLIPBOARD=`xsel -bo`\nPRIMARY=`xsel -po`\nSECONDARY=`xsel -so`"'