aboutsummaryrefslogtreecommitdiffstats
path: root/modules/communication/neomutt/neomutt-sync.sh
diff options
context:
space:
mode:
Diffstat (limited to 'modules/communication/neomutt/neomutt-sync.sh')
-rw-r--r--modules/communication/neomutt/neomutt-sync.sh108
1 files changed, 74 insertions, 34 deletions
diff --git a/modules/communication/neomutt/neomutt-sync.sh b/modules/communication/neomutt/neomutt-sync.sh
index 67e007d..ff43039 100644
--- a/modules/communication/neomutt/neomutt-sync.sh
+++ b/modules/communication/neomutt/neomutt-sync.sh
@@ -1,14 +1,31 @@
#!/usr/bin/env bash
-# Sync mail with mbsync and index new messages with notmuch,
-# showing progress via a dialog gauge.
+# Sync one mbsync channel and index new messages with notmuch.
+
+set -uo pipefail
+
+MBSYNCRC="${MBSYNCRC:-$HOME/.config/mbsync/.mbsyncrc}"
+TITLE="[󰇮 SYNCING MAIL]"
DIALOGRC=$(mktemp)
+MBSYNC_LOG=$(mktemp)
NOTMUCH_LOG=$(mktemp)
-TITLE="[󰇮 SYNCING MAIL]"
+
export DIALOGRC
-trap 'rm -f "$DIALOGRC" "$NOTMUCH_LOG"' EXIT # Clean up everything on exit
-cat << 'EOF' > "$DIALOGRC"
+trap 'rm -f "$DIALOGRC" "$MBSYNC_LOG" "$NOTMUCH_LOG"' EXIT
+
+if [[ $# -ne 1 ]]; then
+ echo "Usage: $0 <mbsync-channel>"
+ echo
+ echo "Example:"
+ echo " $0 arpitchakladar-gmail_com-quick"
+ echo " $0 arpitchakladar-gmail_com-full"
+ exit 2
+fi
+
+CHANNEL="$1"
+
+cat <<'EOF' > "$DIALOGRC"
use_shadow = ON
use_colors = ON
screen_color = (WHITE,BLACK,OFF)
@@ -19,47 +36,70 @@ border_color = (CYAN,BLACK,ON)
border2_color = (BLUE,BLACK,ON)
shadow_color = (BLACK,BLACK,ON)
-# Active buttons: magenta emphasis, matching Base16 base0E
button_active_color = (MAGENTA,BLACK,ON)
button_key_active_color = (MAGENTA,BLACK,ON)
button_label_active_color = (MAGENTA,BLACK,ON)
-# Inactive buttons: cyan secondary emphasis, matching Base16 base0C
button_inactive_color = (CYAN,BLACK,OFF)
button_key_inactive_color = (CYAN,BLACK,OFF)
button_label_inactive_color = (CYAN,BLACK,OFF)
EOF
-# Run the sync and indexing progress bar
(
- # mbsync phase
- script -q -e -c "mbsync -a" /dev/null | awk -v RS='\r' '
- {
- c_str = ""
- b_str = ""
- for(i=1; i<=NF; i++) {
- if ($i == "C:") c_str = $(i+1)
- if ($i == "B:") b_str = $(i+1)
- }
- if (b_str != "") {
- split(b_str, arr, "/")
- if (arr[2] > 0) {
- percent = int((arr[1] / arr[2]) * 100)
- printf "XXX\n%d\nSyncing | Channel: %s | Mailbox: %s\nXXX\n", percent, c_str, b_str
- }
- }
- fflush()
- }'
-
- # notmuch phase
- printf "XXX\n100\nIndexing new mail with notmuch...\nXXX\n"
- notmuch new > "$NOTMUCH_LOG" 2>&1
+ printf "XXX\n0\nSyncing: %s...\nXXX\n" "$CHANNEL"
+
+ mbsync -c "$MBSYNCRC" "$CHANNEL" > "$MBSYNC_LOG" 2>&1 &
+ PID=$!
+
+ # Keep the gauge alive while mbsync runs.
+ while kill -0 "$PID" 2>/dev/null; do
+ printf "XXX\n50\nSyncing: %s...\nXXX\n" "$CHANNEL"
+ sleep 0.5
+ done
+
+ wait "$PID"
+ MBSYNC_STATUS=$?
+
+ if (( MBSYNC_STATUS != 0 )); then
+ printf "XXX\n100\nSync failed: %s\nXXX\n" "$CHANNEL"
+ exit "$MBSYNC_STATUS"
+ fi
+
+ printf "XXX\n90\nIndexing new mail with notmuch...\nXXX\n"
+
+ notmuch new --quiet > "$NOTMUCH_LOG" 2>&1
+ NOTMUCH_STATUS=$?
+
+ printf "XXX\n100\nDone.\nXXX\n"
+
+ if (( NOTMUCH_STATUS != 0 )); then
+ exit "$NOTMUCH_STATUS"
+ fi
) | dialog --title "$TITLE" --gauge "Initializing..." 8 80 0
-# Format the captured notmuch output into a single clean line
-CLEAN_OUT=$(tr '\n' ' ' < "$NOTMUCH_LOG" | sed 's/ */ /g')
+STATUS=${PIPESTATUS[0]}
+
+MBSYNC_OUT=$(
+ tr '\n' ' ' < "$MBSYNC_LOG" |
+ sed 's/ */ /g'
+)
+
+NOTMUCH_OUT=$(
+ tr '\n' ' ' < "$NOTMUCH_LOG" |
+ sed 's/ */ /g'
+)
-# Display final results and wait for user input
-dialog --title "$TITLE" --msgbox "Done!\n$CLEAN_OUT" 8 80
+if (( STATUS == 0 )); then
+ dialog \
+ --title "$TITLE" \
+ --msgbox "Done!\n\nChannel: $CHANNEL\n\nmbsync: ${MBSYNC_OUT:-OK}\n\nnotmuch: ${NOTMUCH_OUT:-OK}" \
+ 12 80
+else
+ dialog \
+ --title "$TITLE" \
+ --msgbox "Sync failed!\n\nChannel: $CHANNEL\n\nmbsync: ${MBSYNC_OUT:-no output}\n\nnotmuch: ${NOTMUCH_OUT:-not run}" \
+ 12 80
+fi
clear
+exit "$STATUS"