Skip to content

Instantly share code, notes, and snippets.

@stephan-t
Last active July 4, 2020 22:49
Show Gist options
  • Save stephan-t/561d0e473dddb270be3da6a8ca994306 to your computer and use it in GitHub Desktop.
Save stephan-t/561d0e473dddb270be3da6a8ca994306 to your computer and use it in GitHub Desktop.
Fixes texture corruption in VS Code terminal after resuming from suspend
#!/bin/bash
# Fixes texture corruption in VS Code terminal after resuming from suspend.
#
# Place script in /lib/systemd/system-sleep/ so it can run after resuming.
#
# Dependencies: jq
case "$1" in
post)
# Check if VS Code is running
if [ -z "$(pgrep -x code)" ]; then
exit 0
fi
user=$(cut -d " " -f 1 <<< $(users))
settings_file="/home/$user/.config/Code/User/settings.json"
# Check if settings file is a symlink
if [ -L "$settings_file" ]; then
settings_link=$settings_file
settings_file=$(readlink $settings_file)
fi
# Create backup of settings
backup_file="/home/$user/.config/Code/User/settings.json.bak"
if [ ! -e "$backup_file" ]; then
cp $settings_file $backup_file
fi
# Before overwriting, ensure settings file is not truncated from errors
tmp_file='/tmp/settings.json'
overwrite() {
local tmp_file_lines=$(wc -l $tmp_file | cut -d " " -f 1)
if [ "$tmp_file_lines" -le 1 ]; then
rm $tmp_file
exit 1
else
chown $user:$user $tmp_file
mv $tmp_file $settings_file
if [ ! -z ${settings_link+x} ]; then
touch -h $settings_link
fi
fi
}
# Wait for screen unlock
tail -f /var/log/auth.log -n 1 | sed '/unlocked login keyring/ q;/Power key pressed/ q'
# Get current zoom level
zoom_level=$(jq '.["window.zoomLevel"]' $settings_file)
# Add missing key if required
if [ "$zoom_level" = "null" ]; then
jq '. + { "window.zoomLevel": 0 }' $settings_file > $tmp_file
overwrite
fi
# Change zoom level
let zoom_level++
jq '.["window.zoomLevel"] = $zoom' --argjson zoom $zoom_level \
$settings_file > $tmp_file
overwrite
sleep 0.5
let zoom_level--
jq '.["window.zoomLevel"] = $zoom' --argjson zoom $zoom_level \
$settings_file > $tmp_file
overwrite
;;
esac
@Fmstrat
Copy link

Fmstrat commented Jul 3, 2020

@stephan-t I've noticed one other thing that I've corrected in my version of the script. Users who assign their power button to pm-hibernate never have to "re-login", and thus nothing shows up in the Auth log. So the tail command just runs forever. The fix is this line in my version if it helps: https://gist.github.com/Fmstrat/7de01996206537b9dbc8f673b2c5ced2#file-vscode-texture-fix-sh-L20

@stephan-t
Copy link
Author

Good catch @Fmstrat, thank you. I've updated my script to include that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment