-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.bash
executable file
·116 lines (90 loc) · 3.25 KB
/
application.bash
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
set -xueo pipefail
# TODO: Implement
_escape_command_for_apple_script() {
local -r sh_command="$1"
echo "${sh_command}"
}
readonly DELAY_BETWEEN_COMMANDS="${DELAY_BETWEEN_COMMANDS:-"0.5"}" # In seconds
# Meant to be called in a prefix form , like `_with_delay echo 'Yo!''`
_with_delay() {
"$@"; sleep "${DELAY_BETWEEN_COMMANDS}"
}
# Returns window ID
execute_command_in_new_terminal_window() {
local -r sh_command="${1:-""}"
local -r escaped_sh_command="$(_escape_command_for_apple_script "${sh_command}")"
local -r as_command="tell application \"Terminal\" to do script \"${escaped_sh_command}\""
# The output looks like 'tab 1 of window id 686'
local -r raw_output="$(osascript -e "${as_command}")"
# 'r' MUST be before 'a' here
read -ra parsed_output <<< "${raw_output}"
_with_delay echo "${parsed_output[5]}"
}
launch_new_terminal_window() {
execute_command_in_new_terminal_window
}
execute_command_in_existing_terminal_window() {
local -r window_id="$1"
local -r sh_command="$2"
local -r escaped_sh_command="$(_escape_command_for_apple_script "${sh_command}")"
local -r as_command="tell application \"Terminal\" to do script \"${escaped_sh_command}\" in window id ${window_id}"
_with_delay osascript -e "${as_command}" > /dev/null
}
close_terminal_window() {
local -r window_id="$1"
execute_command_in_existing_terminal_window "${window_id}" "logout"
# Handles the case that a window closes on logout gracefully
osascript << EOF
tell application "Terminal"
set anEmptyList to {}
set myWindows to windows whose id is equal to ${window_id}
if myWindows is not equal to anEmptyList then
close first item of myWindows
end if
end tell
EOF
}
capture_window() {
local -r window_id="$1"
local -r output_path="${2:-""}"
local arguments=("-w" "-l" "${window_id}")
if [[ -z "${output_path}" ]]; then
arguments+=("-P") # Show in Preview instead
else
arguments+=("${output_path}")
fi
screencapture "${arguments[@]}"
}
# 1. Creates a new Terminal window
# 2. Executes every command with DELAY_BETWEEN_COMMANDS between them
# 3. Makes a screenshot of it saving into specified location
# 4. Closes the window it created
capture_terminal_window_after_commands() {
local -r output_path="$1"; shift
local -r sh_commands=("$@")
local -r window_id="$(launch_new_terminal_window)"
for sh_command in "${sh_commands[@]}"; do
execute_command_in_existing_terminal_window "${window_id}" "${sh_command}"
done
capture_window "${window_id}" "${output_path}"
close_terminal_window "${window_id}"
}
readonly THEMES_DIR="${THEMES_DIR:-"${ZSH}/themes"}"
readonly THEME_SUFFIX=".zsh-theme"
readonly OUTPUT_DIR="${OUTPUT_DIR:-"$(mktemp -d)"}"
_get_theme_name() {
local -r theme_path="$1"
basename "${theme_path}" "${THEME_SUFFIX}"
}
echo "The images will be saved into ${OUTPUT_DIR}"
for theme_path in "${THEMES_DIR}"/*"${THEME_SUFFIX}"; do
theme_name="$(_get_theme_name "${theme_path}")"
output_path="${OUTPUT_DIR}/${theme_name}.png"
commands=(
"source ${theme_path} && clear"
"cd \$ZSH"
"ll"
)
capture_terminal_window_after_commands "${output_path}" "${commands[@]}"
done