Skip to content

Instantly share code, notes, and snippets.

@viniciusgonmelo
Last active June 13, 2023 02:37
Show Gist options
  • Save viniciusgonmelo/b639cd5cd8d742deb9b81081d6590d8b to your computer and use it in GitHub Desktop.
Save viniciusgonmelo/b639cd5cd8d742deb9b81081d6590d8b to your computer and use it in GitHub Desktop.
Tira screenshots ou grava gifs sem sair do terminal
#!/usr/bin/env bash
# Script: term-screenshot.bash
# Descrição: Captura a tela ou grava um gif em uma nova janela do terminal.
# Exemplos de uso:
# ./term-screenshot
# ./term-screenshot exemplo.png
# ./term-screenshot exemplo.gif
show_help() {
cat << EOF
Uso: $0 [ARQUIVO]
Captura a tela ou grava um gif em uma nova janela do terminal.
Formatos suportados: gif; png; jpg; bmp.
Exemplos:
$0
$0 exemplo.png
$0 exemplo.gif
Opções:
--help Mostra esta mensagem de ajuda e sai.
EOF
}
# Trata opções e argumentos
if [[ "$#" -gt 1 ]]; then
echo "Número de argumentos inválido."
show_help
exit 1
elif [[ "$1" == "--help" ]]; then
show_help
exit 0
fi
# Detecta o terminal
pid=$$
while [[ -z "$terminal" ]]; do
cmd=$(ps -o 'cmd=' -p $pid | cut -d ' ' -f 1)
if [[ "$cmd" != "bash" ]]; then
terminal=$cmd
fi
pid=$(ps -o 'ppid=' -p $pid)
done
output_file=${1:-screenshot.png}
file_extension="${output_file##*.}"
pidfile=$(mktemp /tmp/pidfile.XXXXXX)
# Cria um script temporário para executar na nova janela
tmp_script=$(mktemp /tmp/temp-script.XXXXXX)
# Define os comandos do script com base na extensão do arquivo a ser criado
if [[ "$file_extension" == "gif" ]]; then
tty_file=$(mktemp /tmp/ttyrec.XXXXXX)
cat > "$tmp_script" << EOF
#!/bin/bash
echo \$\$ > $pidfile
ttyrec $tty_file
trap 'ttygif $tty_file; mv tty.gif $output_file; rm $tty_file' EXIT
EOF
elif [[ "$file_extension" =~ ^(png|jpg|jpeg|bmp)$ ]]; then
cat > "$tmp_script" << EOF
#!/bin/bash
echo \$\$ > $pidfile
script -q /dev/null
xdotool getactivewindow | xargs -I{} import -window {} $output_file
EOF
else
echo "Formato de arquivo não suportado: $file_extension"
exit 1
fi
chmod +x "$tmp_script"
# Verifica o número de linhas e colunas da janela
read -r term_rows term_cols < <(stty size)
# Verifica o X e o Y da janela e atribui os valores para a nova janela
geometry=$(xdotool getwindowgeometry --shell "$(xdotool getactivewindow)")
eval "$geometry"
X=$((X + WIDTH))
decor_height=$(xwininfo -id "$(xdotool getactivewindow)" | awk '/Relative upper-left Y:/ { print $4 }')
Y=$((Y - decor_height))
# Abre a nova janela do terminal
if [[ "$terminal" == *"gnome-terminal"* ]]; then
"$terminal" --geometry ${term_cols}x${term_rows}+${X}+${Y} -- "$tmp_script"
elif [[ "$terminal" == *"xfce4-terminal"* ]]; then
"$terminal" --geometry ${term_cols}x${term_rows}+${X}+${Y} -x "$tmp_script"
else
echo "Terminal não suportado: $terminal"
exit 1
fi
echo "Saia da nova janela do terminal para gravar a tela (Ctrl-d)."
# Aguarda fechar a janela para encerrar
while true; do
if [[ ! -f "$pidfile" ]]; then
continue
fi
pid=$(< "$pidfile")
if ! kill -0 "$pid" 2>/dev/null; then
break
fi
sleep 0.1
done
rm "$pidfile"
rm "$tmp_script"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment