-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi3-save
executable file
·142 lines (120 loc) · 3.97 KB
/
i3-save
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
#
# Save the layout + programs of the current i3 session
set -e
IFS=$'\n\t'
CURR_DIR="$(dirname "$0")"
I3_RESTORE_SAVE_SCRIPT="$CURR_DIR/programs/i3_save.py"
# Import common variables and functions
source "$CURR_DIR/utils/common.bash"
# Handle errors
source "$CURR_DIR/utils/error_handling.bash"
parse_flags "$@"
check_dependencies
# Start logger
source "$CURR_DIR/utils/logs.bash"
rotate_log
#####################################
# Get the names of all workspaces currently used and
# the displays they are on. For each workspace, the
# first line will be the workspace name and the
# second will be the display name.
# Arguments:
# None
# Outputs:
# A string of workspace and display names. The
# workspace name is the first line and the display
# the workspace is on is the second line
# e.g.
# workspace1
# display1
# workspace2
# display2
#####################################
get_workspaces() {
# Match only the workspace name and the output it is on
i3-msg --type get_workspaces | jq -r '.[] | .name, .output'
}
#####################################
# Remove the session files from the previous saved session
# Globals:
# i3_PATH
# Arguments:
# None
#####################################
remove_previous_session() {
# --force is used so it doesn't exit if a file isn't found
rm --force "$i3_PATH"/*_layout.json
rm --force "$i3_PATH"/*_programs.sh
rm --force "$i3_PATH"/*_subprocess_*.sh
rm --force "$i3_PATH"/web_browsers.sh
rm --force "$i3_PATH"/kitty-session-*
rm --force "$i3_PATH"/kitty-scrollback-*
}
#####################################
# Save the layout of a workspace
# Globals:
# i3_PATH
# Arguments:
# Workspace name
# Display name the workspace is on
# Returns:
# 0 if either the workspace is empty or programs
# were saved correctly. Non-zero on error.
#####################################
save_workspace_layout() {
local workspace display sanitized_ws_name workspace_tree file_name
workspace="$1"
display="$2"
log "Saving layout for Workspace $workspace"
# Replace slash in workspace name as file names cannot have slashes
sanitized_ws_name="${workspace//\//\{slash\}}"
workspace_tree="$(i3-save-tree --workspace "$workspace")"
# If workspace is empty (i.e doesn't contain any actual configuration lines)
if [[ $workspace_tree != *"{"* ]]; then
log "Empty layout for Workspace $workspace. Skipping..."
return 0
fi
file_name="$i3_PATH/workspace_${sanitized_ws_name}_${display}_layout.json"
echo "$workspace_tree" >"$file_name"
# Automatically edit the file
sed --in-place 's|^\(\s*\)// "|\1"|g; /^\s*\/\//d' "$file_name"
}
#####################################
# Save the layouts of all the currently used workspaces.
# Globals:
# i3_PATH
# Arguments:
# None
#####################################
save_workspace_layouts() {
local workspaces workspaces_arr
# Get the workspaces and convert the output into an array
workspaces="$(get_workspaces)"
readarray -t workspaces_arr <<<"$workspaces"
remove_previous_session
# Save a mark on the focused window so it can be restored later
i3-msg --quiet 'mark --add _i3_restore_focus'
for ((idx = 0; idx < ${#workspaces_arr[@]}; idx += 2)); do
local workspace="${workspaces_arr[idx]}"
local display="${workspaces_arr[idx + 1]}"
save_workspace_layout "$workspace" "$display"
done
i3-msg --quiet 'unmark _i3_restore_focus'
}
#####################################
# Execute the python save script to save the
# session's programs.
# Globals:
# I3_RESTORE_SAVE_FILE
# Arguments:
# None
#####################################
save_workspace_programs() {
I3_RESTORE_VERBOSE="$I3_RESTORE_VERBOSE" python3 "$I3_RESTORE_SAVE_SCRIPT" ||
error "An error occurred saving the session's programs. View the logs for more details" 1
}
log "Saving current i3wm session"
save_workspace_layouts
save_workspace_programs
log "Finished saving current i3wm session"