From 80a7334f59a3f003a7c78780182098d9ae46bba7 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Tue, 12 Dec 2023 18:55:53 +0100
Subject: [PATCH] Improve asciinema session recording

---
 .bin/asciinema-log                       | 22 ++++++++++++++++++----
 .config/env.d/99-local##hostname.portalo |  2 +-
 .config/starship.toml                    |  8 +++++++-
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/.bin/asciinema-log b/.bin/asciinema-log
index 4062020..941c073 100755
--- a/.bin/asciinema-log
+++ b/.bin/asciinema-log
@@ -4,15 +4,29 @@ set -e
 
 if [ -x "$(command -v asciinema)" ] \
 	&& [ -z "$ASCIINEMA_REC" ] \
-	&& [ "$ASCIINEMA_LOG" -gt 0 ]
+	&& [ -n "$ASCIINEMA_LOG" ]
 then
 	ASCIINEMA_LOG_DIR="$HOME/.cache/logs"
 	mkdir -p "$ASCIINEMA_LOG_DIR"
 	_file_name_date=$(date +%Y-%m-%d_%H%M%S)
-	export ASCIINEMA_LOG_FILE="$ASCIINEMA_LOG_DIR/asciinema-$_file_name_date.txt"
-	asciinema rec --quiet --raw "$ASCIINEMA_LOG_FILE"
 
-	exit 0 # Parent script should exit
+	if [ "$ASCIINEMA_LOG" = "raw" ]
+	then
+		export ASCIINEMA_LOG_FILE="$ASCIINEMA_LOG_DIR/asciinema-$_file_name_date.txt"
+		asciinema rec --quiet --raw "$ASCIINEMA_LOG_FILE"
+		exit 0 # Parent script should exit
+	fi
+	if [ "$ASCIINEMA_LOG" = "video" ]
+	then
+		export ASCIINEMA_LOG_FILE="$ASCIINEMA_LOG_DIR/asciinema-$_file_name_date.cast"
+		asciinema rec --quiet --idle-time-limit=3 "$ASCIINEMA_LOG_FILE"
+		exit 0 # Parent script should exit
+	fi
+
+	echo "Invalid value for ASCIINEMA_LOG: $ASCIINEMA_LOG"
+	echo "Valid values are: raw, video"
+	echo "Your session will not be recorded"
+	exit 1 # Parent script should not exit
 fi
 
 exit 1 # Parent script should not exit
diff --git a/.config/env.d/99-local##hostname.portalo b/.config/env.d/99-local##hostname.portalo
index 2f6100f..41a588b 100755
--- a/.config/env.d/99-local##hostname.portalo
+++ b/.config/env.d/99-local##hostname.portalo
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 export AUTOSTART_DISPLAY="Hyprland"
-export ASCIINEMA_LOG="1"
+export ASCIINEMA_LOG="video"
 
 export MONITOR_PRIMARY="DisplayPort-0"
 export MONITOR_SECONDARY="HDMI-A-0"
diff --git a/.config/starship.toml b/.config/starship.toml
index efb672f..c96089c 100644
--- a/.config/starship.toml
+++ b/.config/starship.toml
@@ -15,7 +15,7 @@ $cmd_duration\
 [](bg:red fg:yellow)\
 $status\
 [](bg:white fg:red)\
-$shell\
+${custom.asciinema_log}$shell\
 [](bg:cyan fg:white)\
 $jobs\
 [](cyan)
@@ -36,6 +36,12 @@ show_milliseconds = true
 style = "bg:yellow fg:black"
 format = "[$duration]($style)"
 
+[custom.asciinema_log]
+command = 'case $ASCIINEMA_LOG in "raw") printf "" ;; "video") printf "" ;; esac'
+when = ''' test -n "$ASCIINEMA_LOG" && test -n "$ASCIINEMA_REC" '''
+style = "bg:white fg:black"
+format = '[$output ]($style)'
+
 [directory]
 read_only = ""
 truncation_length = 8