From 41bdeb88009dd232808edee9fc0e4ae94358d8d6 Mon Sep 17 00:00:00 2001 From: Arpit Chakladar Date: Sat, 29 Aug 2026 02:02:23 +0530 Subject: refactor: moving script into modules that makes sense - instead of collecting all scripts into modules/scripts we are moving them to modules that makes sense (yazi-file-chooser.sh into yazi) --- modules/communication/neomutt/default.nix | 47 +++++++++++++++++--- modules/communication/neomutt/macros.nix | 2 +- modules/communication/neomutt/neomutt-sync.sh | 62 +++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 modules/communication/neomutt/neomutt-sync.sh (limited to 'modules/communication') diff --git a/modules/communication/neomutt/default.nix b/modules/communication/neomutt/default.nix index 8613032..8e5d51b 100644 --- a/modules/communication/neomutt/default.nix +++ b/modules/communication/neomutt/default.nix @@ -2,8 +2,32 @@ { config, lib, + pkgs, ... }: +let + inherit ((import ../../lib/script.nix { inherit lib pkgs; })) mkScriptModule; + neomuttSync = mkScriptModule { + scope = [ + "communication" + "neomutt" + ]; + name = "neomutt-sync"; + path = ./neomutt-sync.sh; + description = "Neomutt-sync - Interactive mail sync with dialog progress bar"; + deps = [ + pkgs.bash + pkgs.dialog + pkgs.coreutils + pkgs.gawk + pkgs.gnused + pkgs.util-linux + config.programs.mbsync.package + config.programs.notmuch.package + ]; + inherit config; + }; +in { imports = [ ./account @@ -23,10 +47,23 @@ default = config.programs.neomutt.package; description = "The neomutt package to use, wrapped with urlscan in PATH."; }; - }; + } + // neomuttSync.options.communication.neomutt; - config = lib.mkIf config.communication.neomutt.enable { - accounts.email.maildirBasePath = "${config.home.homeDirectory}/.local/share/mail"; - home.sessionVariables.MAILDIR = config.accounts.email.maildirBasePath; - }; + config = lib.mkMerge [ + (lib.mkIf config.communication.neomutt.enable { + accounts.email.maildirBasePath = "${config.home.homeDirectory}/.local/share/mail"; + home.sessionVariables.MAILDIR = config.accounts.email.maildirBasePath; + }) + neomuttSync.config + { + assertions = [ + { + assertion = + !config.communication.neomutt.neomutt-sync.enable || config.communication.neomutt.enable; + message = "communication.neomutt.neomutt-sync is enabled but requires `communication.neomutt.enable`."; + } + ]; + } + ]; } diff --git a/modules/communication/neomutt/macros.nix b/modules/communication/neomutt/macros.nix index 57444d9..e5ff437 100644 --- a/modules/communication/neomutt/macros.nix +++ b/modules/communication/neomutt/macros.nix @@ -9,7 +9,7 @@ "pager" ]; key = "gs"; - action = "set my_wait_key=$wait_key wait_key=no${lib.getExe config.scripts.neomutt-sync.package}set wait_key=$my_wait_key"; + action = "set my_wait_key=$wait_key wait_key=no${lib.getExe config.communication.neomutt.neomutt-sync.package}set wait_key=$my_wait_key"; } { # Extract every URL from the message via urlscan into a picker menu diff --git a/modules/communication/neomutt/neomutt-sync.sh b/modules/communication/neomutt/neomutt-sync.sh new file mode 100644 index 0000000..d0dac63 --- /dev/null +++ b/modules/communication/neomutt/neomutt-sync.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +DIALOGRC=$(mktemp) +NOTMUCH_LOG=$(mktemp) +TITLE="[󰇮 SYNCING MAIL]" +export DIALOGRC +trap 'rm -f "$DIALOGRC" "$NOTMUCH_LOG"' EXIT # Clean up everything on exit + +cat << 'EOF' > "$DIALOGRC" +use_shadow = ON +use_colors = ON +screen_color = (WHITE,BLACK,OFF) +dialog_color = (WHITE,BLACK,ON) +title_color = (YELLOW,BLACK,ON) +gauge_color = (GREEN,BLACK,ON) +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 +) | 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') + +# Display final results and wait for user input +dialog --title "$TITLE" --msgbox "Done!\n$CLEAN_OUT" 8 80 + +clear -- cgit v1.2.3