Skip to content

Instantly share code, notes, and snippets.

@tyjak
Last active March 24, 2024 14:18
Show Gist options
  • Save tyjak/c4ff29e8052a5be453f81951e8ffcb7f to your computer and use it in GitHub Desktop.
Save tyjak/c4ff29e8052a5be453f81951e8ffcb7f to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# The script automates task creation in Taskwarrior using the title of the
# active window. It allows for tag extraction from the window title, annotation
# creation from text selection, and Taskwarrior option customization through
# command-line arguments. Errors are logged for debugging purposes, and
# notifications are provided for task creation success and failure.
#
# Exemple : task_add -a -t +cal due:2025-04-04T1700
#
# Options:
# -a # to add the active selection as an annotation to the task
# -t # to have hour in the due date as YYYY-MM-DDTHHNN
#
# Dependencies:
# - xdotool: Used for retrieving the title of the active window.
# - xclip: Used for obtaining text selection from the clipboard.
# - Taskwarrior: Command-line task management tool.
# - notify-send: Used for displaying desktop notifications.
#
# Actually used with dmemu
#
# GistID: c4ff29e8052a5be453f81951e8ffcb7f
# Chemin vers le fichier de log d'erreur
ERROR_LOG="/tmp/tw_add.error.log"
# Options par défaut pour Taskwarrior
DEFAULT_OPTIONS="+dmenu"
# Fonction pour afficher une notification de succès
show_success_notification() {
title="$1"
message="$2"
notify-send "$title" "$message"
}
# Fonction pour afficher une notification d'erreur
show_error_notification() {
title="$1"
message="$2"
notify-send -u critical "$title" "$message"
}
# Fonction pour enregistrer une erreur dans le fichier de log
log_error() {
error_message="$1"
command_executed="$2"
echo "$(date +'%Y-%m-%d %H:%M:%S') - $error_message - Commande exécutée : $command_executed" >> "$ERROR_LOG"
}
# Fonction pour transformer un tag en slug
transform_tag() {
tag="$1"
# Convertir en minuscules
tag=$(echo "$tag" | tr '[:upper:]' '[:lower:]')
# Remplacer les espaces par des underscores
tag=$(echo "$tag" | tr ' ' '_')
# Ajouter le signe plus
echo "+$tag"
}
# Fonction pour obtenir la sélection du texte
get_selection() {
xclip -o -selection primary | sed ':a;N;$!ba;s/\n/\\n/g'
}
# Fonction pour vérifier si une chaîne est une date
is_date() {
date -d "$1" &>/dev/null
}
# Obtenir le titre de la fenêtre active
window_title=$(xdotool getactivewindow getwindowname)
# Diviser le titre en utilisant le caractère "|"
IFS='|' read -r title tags_raw <<< "$window_title"
# Transformer les tags en format Taskwarrior
tags=()
if [ -n "$tags_raw" ]; then
tags=("$(transform_tag "$tags_raw")")
fi
# Initialiser les options de Taskwarrior
task_options=()
# Traiter les options passées en argument
for arg in "$@"; do
if [[ "$arg" == "-a" ]]; then
# Si l'option -a est spécifiée, obtenir la sélection du texte
annotation=$(get_selection)
elif [[ "$arg" == "-t" ]]; then
# Si l'option -t est spécifiée, ajouter l'option Taskwarrior rc.dateformat:Y-M-DTHN
task_options+=("rc.dateformat:Y-M-DTHN")
else
# Si une autre option est spécifiée, la passer à Taskwarrior
task_options+=("$arg")
fi
done
# Si aucune option n'est spécifiée, utiliser les options par défaut
if [ ${#task_options[@]} -eq 0 ]; then
task_options=($DEFAULT_OPTIONS)
fi
# Si une annotation a été obtenue, vérifier si elle est une date
if [ -n "$annotation" ]; then
if is_date "$annotation"; then
task_options+=("due:$annotation")
else
task_command="task add \"$title\" ${task_options[@]} ${tags[@]}"
task_output=$(eval "$task_command")
if [ $? -ne 0 ]; then
show_error_notification "Erreur" "Impossible d'ajouter la tâche \"$title\"."
log_error "Impossible d'ajouter la tâche \"$title\"." "$task_command"
exit 1
fi
task_id=$(echo "$task_output" | grep -oE '[0-9]+')
if [ -n "$task_id" ]; then
task annotate $task_id "$annotation"
if [ $? -ne 0 ]; then
show_error_notification "Erreur" "Impossible d'ajouter l'annotation à la tâche \"$title\"."
log_error "Impossible d'ajouter l'annotation à la tâche \"$title\"." "task annotate $task_id \"$annotation\""
exit 1
fi
else
show_error_notification "Erreur" "Impossible de récupérer l'ID de la tâche ajoutée."
log_error "Impossible de récupérer l'ID de la tâche ajoutée." "$task_command"
exit 1
fi
fi
else
# Créer une tâche avec le titre et les tags
task_command="task add \"$title\" ${task_options[@]} ${tags[@]}"
task_output=$(eval "$task_command")
if [ $? -ne 0 ]; then
show_error_notification "Erreur" "Impossible de créer la tâche \"$title\"."
log_error "Impossible de créer la tâche \"$title\"." "$task_command"
exit 1
fi
fi
show_success_notification "Tâche ajoutée" "La tâche \"$title\" a été ajoutée avec succès."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment