From d359fa0314e6859efb50d9b32504cd9f00f67961 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sun, 20 Jul 2025 19:24:17 +0200 Subject: Add todoist t alias --- zsh/functions/todoist.zsh | 135 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 zsh/functions/todoist.zsh (limited to 'zsh') diff --git a/zsh/functions/todoist.zsh b/zsh/functions/todoist.zsh new file mode 100644 index 0000000..9feaddf --- /dev/null +++ b/zsh/functions/todoist.zsh @@ -0,0 +1,135 @@ +function t() { + case "$1" in + "t") + shift + local tasks_today=$(todoist --project-namespace list --filter today) + local selected_index=$(echo "$tasks_today" | awk '{$1=$2=$3=$4=""; print substr($0,5)}' | nl -nln | fzf \ + --preview-window=up \ + --with-nth=2.. \ + --preview "echo '$tasks_today' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist --project-namespace show" \ + --bind "ctrl-x:execute(echo '$tasks_today' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist close)+abort" \ + --header "Enter: show task | complete" | awk '{print $1}') + + if [[ -n "$selected_index" ]]; then + local task_id=$(echo "$tasks_today" | sed -n "${selected_index}p" | awk '{print $1}') + todoist --project-namespace show "$task_id" + fi + ;; + "a") + shift + todoist quick "$@" + ;; + "l") + shift + local all_tasks=$(todoist --project-namespace list) + local selected_index=$(echo "$all_tasks" | awk '{$1=$4=""; print substr($0,2)}' | nl -nln | fzf \ + --preview-window=up \ + --with-nth=2.. \ + --preview "echo '$all_tasks' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist --project-namespace show" \ + --bind "ctrl-t:execute(echo '$all_tasks' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist modify --date $(date +%Y/%m/%d))+reload(echo '$all_tasks' | awk '{\$1=\$4=\"\"; print substr(\$0,2)}' | nl -nln)" \ + --bind "ctrl-x:execute(echo '$all_tasks' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist close)+abort" \ + --header "Enter: show task | today | complete" | awk '{print $1}') + + if [[ -n "$selected_index" ]]; then + local task_id=$(echo "$all_tasks" | sed -n "${selected_index}p" | awk '{print $1}') + todoist --project-namespace show "$task_id" + fi + ;; + "p") + shift + local all_projects=$(todoist --project-namespace projects) + local selected_index=$(echo "$all_projects" | awk '{$1=""; print substr($0,2)}' | nl -nln | fzf \ + --preview-window=up:1 \ + --with-nth=2.. \ + --preview "echo 'Project:' {2..}" \ + --header "Select project to view tasks" | awk '{print $1}') + + if [[ -n "$selected_index" ]]; then + local project_name=$(echo "$all_projects" | sed -n "${selected_index}p" | cut -d' ' -f2-) + local clean_name=$(echo "$project_name" | sed 's/^#//') + echo "Debug: project_name='$project_name'" >&2 + echo "Debug: clean_name='$clean_name'" >&2 + + local filter_query + if [[ "$clean_name" == *":"* ]]; then + # Convert namespace format: "a:b:c:d" -> "##a & ##b & ##c & #d" + local segments=(${(s/:/)clean_name}) + echo "Debug: segments count=${#segments}" >&2 + filter_query="" + for ((i=1; i<=${#segments}; i++)); do + local segment=$(echo "${segments[i]}" | tr '[:upper:]' '[:lower:]' | awk '{print $1}') + echo "Debug: segment $i = '$segment'" >&2 + + # Add separator if not first segment + if [[ $i -gt 1 ]]; then + filter_query="${filter_query} & " + fi + + # Last segment gets single #, all others get ## + if [[ $i -eq ${#segments} ]]; then + filter_query="${filter_query}#${segment}" + else + filter_query="${filter_query}##${segment}" + fi + done + else + filter_query="#$(echo "$clean_name" | tr '[:upper:]' '[:lower:]')" + fi + echo "Debug: filter_query='$filter_query'" >&2 + local project_tasks=$(todoist --project-namespace list --filter "$filter_query") + local formatted_tasks=$(echo "$project_tasks" | awk -v proj="$project_name" '{ + # Split line into fields, find project column and remove it + id = $1 + prio = $2 + date = $3 + time = $4 + + # Find where project ends by looking for the project name + rest = substr($0, length(id " " prio " " date " " time " ") + 1) + proj_idx = index(rest, proj) + if (proj_idx > 0) { + task_part = substr(rest, proj_idx + length(proj)) + gsub(/^ +/, "", task_part) # trim leading spaces + print id, prio, date, task_part + } else { + # Fallback: assume project is in field 5 and task starts from field 6 + task = "" + for(i=6; i<=NF; i++) task = task $i (i today | complete" | awk '{print $1}') + + if [[ -n "$selected_index" ]]; then + local task_id=$(echo "$project_tasks" | sed -n "${selected_index}p" | awk '{print $1}') + todoist --project-namespace show "$task_id" + fi + fi + ;; + "i") + shift + local inbox_tasks=$(todoist list --filter '#inbox') + local selected_index=$(echo "$inbox_tasks" | awk '{$1=$2=$3=""; print substr($0,4)}' | nl -nln | fzf \ + --preview-window=up \ + --with-nth=2.. \ + --preview "echo '$inbox_tasks' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist --project-namespace show" \ + --bind "ctrl-t:execute(echo '$inbox_tasks' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist modify --date $(date +%Y/%m/%d))+reload(echo '$inbox_tasks' | awk '{\$1=\$2=\$3=\"\"; print substr(\$0,4)}' | nl -nln)" \ + --bind "ctrl-x:execute(echo '$inbox_tasks' | sed -n '{1}p' | awk '{print \$1}' | xargs todoist close)+abort" \ + --header "Enter: show task | today | complete" | awk '{print $1}') + + if [[ -n "$selected_index" ]]; then + local task_id=$(echo "$inbox_tasks" | sed -n "${selected_index}p" | awk '{print $1}') + todoist --project-namespace show "$task_id" + fi + ;; + *) + todoist "$@" + ;; + esac +} -- cgit