Files
luk/run_himalaya.sh
2025-04-30 10:03:15 -04:00

141 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
# Check if an argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <message_number>"
exit 1
fi
# Function to refresh the vim-himalaya buffer
refresh_vim_himalaya() {
nvim --server /tmp/nvim-server --remote-send "Himalaya<CR>"
}
# Function to read a single character without waiting for Enter
read_char() {
stty -echo -icanon time 0 min 1
char=$(dd bs=1 count=1 2>/dev/null)
stty echo icanon
echo "$char"
}
# Function to safely run the himalaya command and handle failures
run_himalaya_message_read() {
himalaya message read "$1" | glow -p
if [ $? -ne 0 ]; then
echo "Failed to open message $1."
return 1
fi
return 0
}
# Function to prompt the user for an action
prompt_action() {
echo "What would you like to do?"
# Step 1: Ask if the user wants to create a task
echo -n "Would you like to create a task for this message? (y/n): "
create_task=$(read_char)
echo "$create_task" # Echo the character for feedback
if [[ "$create_task" == "y" || "$create_task" == "Y" ]]; then
read -p "Task title: " task_title
task add "Followup on email $1 - $task_title" --project "Email" --due "$(date -d '+1 week' +%Y-%m-%d)" --priority "P3" --tags "email"
echo "Task created for message $1."
fi
# Step 2: Ask if the user wants to delete or archive the message
echo "d) Delete the message"
echo "a) Move the message to the archive folder"
echo "x) Skip delete/archive step"
echo -n "Enter your choice (d/a/x): "
archive_or_delete=$(read_char)
echo "$archive_or_delete" # Echo the character for feedback
case $archive_or_delete in
d)
echo "Deleting message $1..."
himalaya message delete "$1"
refresh_vim_himalaya
;;
a)
echo "Archiving message $1..."
himalaya message move Archives "$1"
refresh_vim_himalaya
;;
*)
echo "Invalid choice. Skipping delete/archive step."
;;
esac
# Step 3: Ask if the user wants to open the next message or exit
echo -e "\n"
echo "n) Open the next message"
echo "p) Open the previous message"
echo "x) Exit"
echo -n "Enter your choice (o/x): "
next_or_exit=$(read_char)
echo "$next_or_exit" # Echo the character for feedback
case $next_or_exit in
n)
# Try opening the next message, retrying up to 5 times if necessary
attempts=0
success=false
while [ $attempts -lt 5 ]; do
next_id=$(( $1 + attempts + 1 ))
echo "Attempting to open next message: $next_id"
if run_himalaya_message_read "$next_id"; then
success=true
break
else
echo "Failed to open message $next_id. Retrying..."
attempts=$((attempts + 1))
fi
done
if [ "$success" = false ]; then
echo "Unable to open any messages after 5 attempts. Exiting."
exit 1
fi
;;
p)
# Try opening the previous message, retrying up to 5 times if necessary
attempts=0
success=false
while [ $attempts -lt 5 ]; do
prev_id=$(( $1 - attempts - 1 ))
echo "Attempting to open previous message: $prev_id"
if $0 $prev_id; then
success=true
break
else
echo "Failed to open message $prev_id. Retrying..."
attempts=$((attempts + 1))
fi
done
if [ "$success" = false ]; then
echo "Unable to open any messages after 5 attempts. Exiting."
fi
;;
x)
echo "Exiting."
exit 0
;;
*)
echo "Invalid choice. Exiting."
exit 1
;;
esac
}
# Run the himalaya command with the provided message number
run_himalaya_message_read "$1"
if [ $? -ne 0 ]; then
echo "Error reading message $1. Exiting."
exit 1
fi
# Prompt the user for the next action
prompt_action "$1"