I forgot to push changes, and it's been a while. I'm pretty sure this is game fixes.
This commit is contained in:
@@ -7,23 +7,30 @@
|
||||
# set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/#:~:text=set%20%2Du,is%20often%20highly%20desirable%20behavior.
|
||||
shopt -s expand_aliases
|
||||
|
||||
# Debug logging
|
||||
#DEBUG_LOG="${XDG_CACHE_HOME:-$HOME/.cache}/audiogame-manager/translator-debug.log"
|
||||
#exec 2>>"$DEBUG_LOG"
|
||||
#echo "=== Translator started at $(date) ===" >&2
|
||||
#echo "Args: $@" >&2
|
||||
#set -x
|
||||
|
||||
if [[ $# -ne 2 ]]; then
|
||||
echo "Usage: $0 \"application name\" \"file name\"."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
scriptDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
pythonScript="${scriptDir}/translate_clipboard.py"
|
||||
if [[ -z "${AGM_TRANSLATOR_FORCE_SHELL:-}" ]] && command -v python3 &> /dev/null && [[ -f "$pythonScript" ]]; then
|
||||
exec python3 "$pythonScript" "$@"
|
||||
fi
|
||||
|
||||
is_app_running() {
|
||||
local appPattern="$1"
|
||||
local selfPid="$$"
|
||||
|
||||
pgrep -af -u "$USER" "$appPattern" \
|
||||
| awk -v selfPid="$selfPid" '$1 != selfPid && $0 !~ /clipboard_translator.sh/ { found=1 } END { exit !found }'
|
||||
}
|
||||
|
||||
# Wait for the application to start
|
||||
while ! pgrep -f -u "$USER" "$1" &> /dev/null ; do
|
||||
while ! is_app_running "$1"; do
|
||||
sleep 0.05
|
||||
done
|
||||
|
||||
|
||||
fileName="${2,,}"
|
||||
fileName="${fileName//[[:space:]]/-}.sqlite"
|
||||
translationFile="${XDG_CACHE_HOME:-$HOME/.cache}/audiogame-manager/${fileName}"
|
||||
@@ -44,26 +51,8 @@ CREATE INDEX translations_text_idx ON translations (text);
|
||||
EOF
|
||||
fi
|
||||
|
||||
# Define a function to safely query the database
|
||||
query_database() {
|
||||
local dbFile="$1"
|
||||
local sqlQuery="$2"
|
||||
sqlite3 -line "$dbFile" "$sqlQuery"
|
||||
}
|
||||
|
||||
# Define a function to safely insert into the database
|
||||
insert_database() {
|
||||
local dbFile="$1"
|
||||
local text="$2"
|
||||
local translation="$3"
|
||||
|
||||
# Use sqlite3 .import feature which is more robust for special characters
|
||||
echo "$text|$translation" | sqlite3 -separator "|" "$dbFile" ".import /dev/stdin temp_import"
|
||||
sqlite3 "$dbFile" "INSERT OR IGNORE INTO translations SELECT * FROM temp_import; DROP TABLE IF EXISTS temp_import;"
|
||||
}
|
||||
|
||||
# Read so long as the application is running
|
||||
while pgrep -f -u "$USER" "$1" &> /dev/null ; do
|
||||
while is_app_running "$1"; do
|
||||
sleep 0.05
|
||||
text="$(xclip -d "${DISPLAY:-:0}" -selection clipboard -o 2> /dev/null)"
|
||||
if [[ -f ~/.agmsilent ]]; then
|
||||
@@ -80,46 +69,50 @@ while pgrep -f -u "$USER" "$1" &> /dev/null ; do
|
||||
|
||||
# https://en.wikipedia.org/wiki/Unicode_equivalence#Combining_and_precomposed_characters
|
||||
# https://www.effectiveperlprogramming.com/2011/09/normalize-your-perl-source/
|
||||
alias nfc="perl -MUnicode::Normalize -CS -ne 'print NFC(\$_)'" # composed characters
|
||||
nfc() {
|
||||
perl -MUnicode::Normalize -CS -ne 'print NFC($_)'
|
||||
}
|
||||
|
||||
# Normalize different unicode space characters to the same space
|
||||
# https://stackoverflow.com/a/43640405
|
||||
alias normalizeSpaces="perl -CSDA -plE 's/[^\\S\\t]/ /g'"
|
||||
alias normalizeUnicode="normalizeSpaces | nfc"
|
||||
normalize_spaces() {
|
||||
perl -CSDA -plE 's/[^\S\t]/ /g'
|
||||
}
|
||||
# Remove zero-width spaces (U+200B) that some games insert between characters
|
||||
strip_zwsp() {
|
||||
perl -CSDA -plE 's/\x{200B}//g'
|
||||
}
|
||||
normalize_unicode() {
|
||||
strip_zwsp | normalize_spaces | nfc
|
||||
}
|
||||
|
||||
# Normalize text
|
||||
normalizedText="$(echo "$text" | normalizeUnicode)"
|
||||
|
||||
normalizedText="$(echo "$text" | normalize_unicode)"
|
||||
|
||||
# Create a temporary database for import
|
||||
sqlite3 "$translationFile" "CREATE TABLE IF NOT EXISTS temp_import(text TEXT, translation TEXT);"
|
||||
|
||||
|
||||
# Check if we already have a translation
|
||||
translated=$(sqlite3 "$translationFile" "SELECT translation FROM translations WHERE text = '$normalizedText' LIMIT 1;" 2>/dev/null)
|
||||
echo "DEBUG: Database lookup result: '$translated'" >&2
|
||||
|
||||
if [[ -z "$translated" ]]; then
|
||||
echo "DEBUG: No cached translation, calling trans command" >&2
|
||||
# Get translation from the trans utility
|
||||
translated="$(trans -no-autocorrect -no-warn -brief "$normalizedText" | head -1 | sed 's/\s*$//' | normalizeUnicode)"
|
||||
echo "DEBUG: trans returned: '$translated'" >&2
|
||||
translated="$(trans -no-autocorrect -no-warn -brief "$normalizedText" | head -1 | sed 's/\s*$//' | normalize_unicode)"
|
||||
|
||||
if [[ -n "$translated" ]]; then
|
||||
# Insert using echo piping to avoid escaping issues
|
||||
echo "$normalizedText|$translated" | sqlite3 -separator "|" "$translationFile" ".import /dev/stdin temp_import"
|
||||
sqlite3 "$translationFile" "INSERT OR IGNORE INTO translations SELECT * FROM temp_import; DELETE FROM temp_import;"
|
||||
echo "DEBUG: Saved to database" >&2
|
||||
fi
|
||||
fi
|
||||
|
||||
# If we got a translation, speak it
|
||||
if [[ -n "$translated" ]]; then
|
||||
echo "DEBUG: Speaking translation: '$translated'" >&2
|
||||
spd-say -- "$translated"
|
||||
else
|
||||
echo "DEBUG: No translation available, speaking original: '$text'" >&2
|
||||
spd-say -- "$text"
|
||||
spd-say -- "$normalizedText"
|
||||
fi
|
||||
|
||||
|
||||
# Clear clipboard
|
||||
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user