-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a red
!
to the prompt when system is reserved
This is done by updating the global `RESMAN_ALERT` every time the prompt is refreshed. This variable can also be included in custom prompts. It updates the variable by checking the global lock file. This can be costly as it has to parse JSON, so it therefore truncates the file when the previous reservation has ended, making future checks faster. However, it does introduce some overhead when the server is reserved. Instead of parsing JSON, you could hard-code the assumption that the end_time is first in the record, doing it all in bash, but that is less robust.
- Loading branch information
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Set RESMAN_ALERT to a red `!` when the system is reserved | ||
update_resman_alert() { | ||
local lock_file="/etc/res_lock" | ||
|
||
# Check if the file exists and is not empty | ||
if [ -s "$lock_file" ]; then | ||
# Check the status of the lock | ||
current_time=$(date +%s) | ||
is_locked=$(python3 -c "import sys, json; data=json.load(sys.stdin); print(data.get('duration', 0) == -1 or data.get('start_time', 0) + data.get('duration', 0) > $current_time)" < "$lock_file") | ||
|
||
if [ "$is_locked" = "True" ]; then | ||
RESMAN_ALERT=$'\e[31m!\e[0m' | ||
else | ||
# Remove the old lock here, for faster future checks | ||
truncate -s 0 /etc/res_lock | ||
RESMAN_ALERT="" | ||
fi | ||
else | ||
RESMAN_ALERT="" | ||
fi | ||
} | ||
|
||
PS1='${RESMAN_ALERT}\u@\h:\w> ' | ||
|
||
# Recompute RESMAN_ALERT every time the prompt is refreshed | ||
PROMPT_COMMAND="update_resman_alert;${PROMPT_COMMAND:+$PROMPT_COMMAND}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters