impr: relaunch fzf only when necessary
This commit is contained in:
		
							
								
								
									
										120
									
								
								src/sh/cli.sh
									
									
									
									
									
								
							
							
						
						
									
										120
									
								
								src/sh/cli.sh
									
									
									
									
									
								
							@@ -1,34 +1,94 @@
 | 
			
		||||
# Git
 | 
			
		||||
if [ "${1:-}" = "--git-init" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    err "Git already enabled"
 | 
			
		||||
    return 1
 | 
			
		||||
  fi
 | 
			
		||||
  if ! command -v "git" >/dev/null; then
 | 
			
		||||
    err "Git not installed"
 | 
			
		||||
    return 1
 | 
			
		||||
  fi
 | 
			
		||||
  git -C "$ROOT" init
 | 
			
		||||
  git -C "$ROOT" add -A
 | 
			
		||||
  git -C "$ROOT" commit -m 'Initial commit: Start git tracking'
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ "${1:-}" = "--git" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  if [ -z "${GIT:-}" ]; then
 | 
			
		||||
    err "Git not supported, run \`$0 --git-init\` first"
 | 
			
		||||
    return 1
 | 
			
		||||
  fi
 | 
			
		||||
  $GIT "$@"
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Generate new entry
 | 
			
		||||
if [ "${1:-}" = "--new" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  collection=$(printf "%s" "$COLLECTION_LABELS" | tr ';' '\n' | $FZF --delimiter='=' --with-nth=2 --accept-nth=1)
 | 
			
		||||
  file=""
 | 
			
		||||
  while [ -f "$file" ] || [ -z "$file" ]; do
 | 
			
		||||
    uuid=$($UUIDGEN)
 | 
			
		||||
    file="$ROOT/$collection/$uuid.ics"
 | 
			
		||||
  done
 | 
			
		||||
  tmpmd=$(mktemp --suffix='.md')
 | 
			
		||||
  {
 | 
			
		||||
    echo "::: |> <!-- keep this line to associate the entry to _today_ -->"
 | 
			
		||||
    echo "::: <| <!-- specify the due date for to-dos, can be empty, a date string, or even \"next Sunday\" -->"
 | 
			
		||||
    echo "# <!-- write summary here -->"
 | 
			
		||||
    echo "> <!-- comma-separated list of categories -->"
 | 
			
		||||
    echo ""
 | 
			
		||||
  } >"$tmpmd"
 | 
			
		||||
  checksum=$(cksum "$tmpmd")
 | 
			
		||||
 | 
			
		||||
  # Open in editor
 | 
			
		||||
  $EDITOR "$tmpmd" >/dev/tty
 | 
			
		||||
 | 
			
		||||
  # Update if changes are detected
 | 
			
		||||
  if [ "$checksum" != "$(cksum "$tmpmd")" ]; then
 | 
			
		||||
    tmpfile="$tmpmd.ics"
 | 
			
		||||
    awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile"
 | 
			
		||||
    mv "$tmpfile" "$file"
 | 
			
		||||
    if [ -n "${GIT:-}" ]; then
 | 
			
		||||
      $GIT add "$file"
 | 
			
		||||
      $GIT commit -q -m "File added" -- "$file"
 | 
			
		||||
    fi
 | 
			
		||||
  fi
 | 
			
		||||
  rm "$tmpmd"
 | 
			
		||||
  __new
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Build query
 | 
			
		||||
while [ -n "${1:-}" ]; do
 | 
			
		||||
  case "${1:-}" in
 | 
			
		||||
  "--completed")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} ✅"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-completed")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !✅"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--open")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} 🔲"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-open")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !🔲"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--tasks")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} ✅ | 🔲"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-tasks")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !✅ !🔲"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--notes")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} 🗒️"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-notes")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !🗒️"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--journal")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} 📘"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-journal")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !📘"
 | 
			
		||||
    ;;
 | 
			
		||||
  "--filter")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} $1"
 | 
			
		||||
    shift
 | 
			
		||||
    ;;
 | 
			
		||||
  "--no-filter")
 | 
			
		||||
    shift
 | 
			
		||||
    cliquery="${cliquery:-} !$1"
 | 
			
		||||
    shift
 | 
			
		||||
    ;;
 | 
			
		||||
  *)
 | 
			
		||||
    err "Unknown option \"$1\""
 | 
			
		||||
    exit 1
 | 
			
		||||
    ;;
 | 
			
		||||
  esac
 | 
			
		||||
done
 | 
			
		||||
query=${cliquery:-!✅}
 | 
			
		||||
export query
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +0,0 @@
 | 
			
		||||
if [ "${1:-}" = "--git-init" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    err "Git already enabled"
 | 
			
		||||
    return 1
 | 
			
		||||
  fi
 | 
			
		||||
  if ! command -v "git" >/dev/null; then
 | 
			
		||||
    err "Git not installed"
 | 
			
		||||
    return 1
 | 
			
		||||
  fi
 | 
			
		||||
  git -C "$ROOT" init
 | 
			
		||||
  git -C "$ROOT" add -A
 | 
			
		||||
  git -C "$ROOT" commit -m 'Initial commit: Start git tracking'
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ "${1:-}" = "--git" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  if [ -z "${GIT:-}" ]; then
 | 
			
		||||
    err "Git not supported, run \`$0 --git-init\` first"
 | 
			
		||||
    return 1
 | 
			
		||||
  fi
 | 
			
		||||
  $GIT "$@"
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
# Generate preview of file from selection
 | 
			
		||||
if [ "${1:-}" = "--preview" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  name=$(echo "$1" | cut -d ' ' -f 3)
 | 
			
		||||
  name="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$name"
 | 
			
		||||
  awk -v field="DESCRIPTION" "$AWK_GET" "$file" |
 | 
			
		||||
@@ -11,83 +11,25 @@ if [ "${1:-}" = "--preview" ]; then
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Delete file from selection
 | 
			
		||||
if [ "${1:-}" = "--delete" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  name=$(echo "$1" | cut -d ' ' -f 3)
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$name"
 | 
			
		||||
  summary=$(awk -v field="SUMMARY" "$AWK_GET" "$file")
 | 
			
		||||
  while true; do
 | 
			
		||||
    printf "Do you want to delete the entry with the title \"%s\"? (yes/no): " "$summary" >/dev/tty
 | 
			
		||||
    read -r yn
 | 
			
		||||
    case $yn in
 | 
			
		||||
    "yes")
 | 
			
		||||
      rm -v "$file"
 | 
			
		||||
      if [ -n "${GIT:-}" ]; then
 | 
			
		||||
        $GIT add "$file"
 | 
			
		||||
        $GIT commit -q -m "File deleted" -- "$file"
 | 
			
		||||
      fi
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    "no")
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    *)
 | 
			
		||||
      echo "Please answer \"yes\" or \"no\"." >/dev/tty
 | 
			
		||||
      ;;
 | 
			
		||||
    esac
 | 
			
		||||
  done
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Toggle completed flag
 | 
			
		||||
if [ "${1:-}" = "--toggle-completed" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  name=$(echo "$1" | cut -d ' ' -f 3)
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$name"
 | 
			
		||||
  tmpfile=$(mktemp)
 | 
			
		||||
  awk "$AWK_ALTERTODO" "$file" >"$tmpfile"
 | 
			
		||||
  mv "$tmpfile" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Completed toggle" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Increase priority
 | 
			
		||||
if [ "${1:-}" = "--increase-priority" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  name=$(echo "$1" | cut -d ' ' -f 3)
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$name"
 | 
			
		||||
  tmpfile=$(mktemp)
 | 
			
		||||
  awk -v delta="1" "$AWK_ALTERTODO" "$file" >"$tmpfile"
 | 
			
		||||
  mv "$tmpfile" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Priority increased" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Decrease priority
 | 
			
		||||
if [ "${1:-}" = "--decrease-priority" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  name=$(echo "$1" | cut -d ' ' -f 3)
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$name"
 | 
			
		||||
  tmpfile=$(mktemp)
 | 
			
		||||
  awk -v delta="-1" "$AWK_ALTERTODO" "$file" >"$tmpfile"
 | 
			
		||||
  mv "$tmpfile" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Priority decreased" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Reload view
 | 
			
		||||
if [ "${1:-}" = "--reload" ]; then
 | 
			
		||||
  shift
 | 
			
		||||
  case "${1:-}" in
 | 
			
		||||
  "--toggle-completed")
 | 
			
		||||
    shift
 | 
			
		||||
    fname="$1"
 | 
			
		||||
    shift
 | 
			
		||||
    __toggle_completed "$fname" >/dev/null
 | 
			
		||||
    ;;
 | 
			
		||||
  "--change-priority")
 | 
			
		||||
    shift
 | 
			
		||||
    delta=$1
 | 
			
		||||
    shift
 | 
			
		||||
    fname="$1"
 | 
			
		||||
    shift
 | 
			
		||||
    __change_priority "$delta" "$fname" >>/tmp/foo
 | 
			
		||||
    ;;
 | 
			
		||||
  esac
 | 
			
		||||
  __lines
 | 
			
		||||
  exit
 | 
			
		||||
fi
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										9
									
								
								src/sh/helper.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/sh/helper.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
# Print error message
 | 
			
		||||
err() {
 | 
			
		||||
  echo "❌ $1" >/dev/tty
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Strip whitespaces from argument
 | 
			
		||||
stripws() {
 | 
			
		||||
  echo "$@" | sed "s/^ *//" | sed "s/ *$//"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										128
									
								
								src/sh/icalendar.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								src/sh/icalendar.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,128 @@
 | 
			
		||||
# Interface to modify iCalendar files
 | 
			
		||||
 | 
			
		||||
# Toggle completed status of VTODO
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Relative path to iCalendar file
 | 
			
		||||
__toggle_completed() {
 | 
			
		||||
  fname="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  file="$ROOT/$fname"
 | 
			
		||||
  tmpfile=$(mktemp)
 | 
			
		||||
  awk "$AWK_ALTERTODO" "$file" >"$tmpfile"
 | 
			
		||||
  mv "$tmpfile" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Completed toggle" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Change priority of VTODO entry
 | 
			
		||||
#
 | 
			
		||||
# @input $1: Delta, can be any integer
 | 
			
		||||
# @input $2: Relative path to iCalendar file
 | 
			
		||||
__change_priority() {
 | 
			
		||||
  delta=$1
 | 
			
		||||
  shift
 | 
			
		||||
  fname="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  echo "call to __change_priority with delta=$delta and fname=$fname" >>/tmp/foo
 | 
			
		||||
  file="$ROOT/$fname"
 | 
			
		||||
  tmpfile=$(mktemp)
 | 
			
		||||
  echo "        tmpfile=$tmpfile" >>/tmp/foo
 | 
			
		||||
  awk -v delta="$delta" "$AWK_ALTERTODO" "$file" >"$tmpfile"
 | 
			
		||||
  echo "        lines=$(wc -l tmpfile)" >>/tmp/foo
 | 
			
		||||
  mv "$tmpfile" "$file"
 | 
			
		||||
  if [ -n "${GIT:-}" ]; then
 | 
			
		||||
    $GIT add "$file"
 | 
			
		||||
    $GIT commit -q -m "Priority changed by $delta" -- "$file"
 | 
			
		||||
  fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Edit file
 | 
			
		||||
#
 | 
			
		||||
# @input $1: File path
 | 
			
		||||
__edit() {
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  filetmp=$(mktemp --suffix='.md')
 | 
			
		||||
  awk "$AWK_EXPORT" "$file" >"$filetmp"
 | 
			
		||||
  checksum=$(cksum "$filetmp")
 | 
			
		||||
 | 
			
		||||
  # Open in editor
 | 
			
		||||
  $EDITOR "$filetmp" >/dev/tty
 | 
			
		||||
 | 
			
		||||
  # Update only if changes are detected
 | 
			
		||||
  if [ "$checksum" != "$(cksum "$filetmp")" ]; then
 | 
			
		||||
    file_new="$filetmp.ics"
 | 
			
		||||
    awk "$AWK_UPDATE" "$filetmp" "$file" >"$file_new"
 | 
			
		||||
    mv "$file_new" "$file"
 | 
			
		||||
    if [ -n "${GIT:-}" ]; then
 | 
			
		||||
      $GIT add "$file"
 | 
			
		||||
      $GIT commit -q -m "File modified" -- "$file"
 | 
			
		||||
    fi
 | 
			
		||||
  fi
 | 
			
		||||
  rm "$filetmp"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Delete file
 | 
			
		||||
#
 | 
			
		||||
# @input $1: File path
 | 
			
		||||
__delete() {
 | 
			
		||||
  file="$1"
 | 
			
		||||
  shift
 | 
			
		||||
  summary=$(awk -v field="SUMMARY" "$AWK_GET" "$file")
 | 
			
		||||
  while true; do
 | 
			
		||||
    printf "Do you want to delete the entry with the title \"%s\"? (yes/no): " "$summary" >/dev/tty
 | 
			
		||||
    read -r yn
 | 
			
		||||
    case $yn in
 | 
			
		||||
    "yes")
 | 
			
		||||
      rm -v "$file"
 | 
			
		||||
      if [ -n "${GIT:-}" ]; then
 | 
			
		||||
        $GIT add "$file"
 | 
			
		||||
        $GIT commit -q -m "File deleted" -- "$file"
 | 
			
		||||
      fi
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    "no")
 | 
			
		||||
      break
 | 
			
		||||
      ;;
 | 
			
		||||
    *)
 | 
			
		||||
      echo "Please answer \"yes\" or \"no\"." >/dev/tty
 | 
			
		||||
      ;;
 | 
			
		||||
    esac
 | 
			
		||||
  done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Add file
 | 
			
		||||
__new() {
 | 
			
		||||
  collection=$(printf "%s" "$COLLECTION_LABELS" | tr ';' '\n' | $FZF --delimiter='=' --with-nth=2 --accept-nth=1)
 | 
			
		||||
  file=""
 | 
			
		||||
  while [ -f "$file" ] || [ -z "$file" ]; do
 | 
			
		||||
    uuid=$($UUIDGEN)
 | 
			
		||||
    file="$ROOT/$collection/$uuid.ics"
 | 
			
		||||
  done
 | 
			
		||||
  tmpmd=$(mktemp --suffix='.md')
 | 
			
		||||
  {
 | 
			
		||||
    echo "::: |> <!-- keep this line to associate the entry to _today_ -->"
 | 
			
		||||
    echo "::: <| <!-- specify the due date for to-dos, can be empty, a date string, or even \"next Sunday\" -->"
 | 
			
		||||
    echo "# <!-- write summary here -->"
 | 
			
		||||
    echo "> <!-- comma-separated list of categories -->"
 | 
			
		||||
    echo ""
 | 
			
		||||
  } >"$tmpmd"
 | 
			
		||||
  checksum=$(cksum "$tmpmd")
 | 
			
		||||
 | 
			
		||||
  # Open in editor
 | 
			
		||||
  $EDITOR "$tmpmd" >/dev/tty
 | 
			
		||||
 | 
			
		||||
  # Update if changes are detected
 | 
			
		||||
  if [ "$checksum" != "$(cksum "$tmpmd")" ]; then
 | 
			
		||||
    tmpfile="$tmpmd.ics"
 | 
			
		||||
    awk -v uid="$uuid" "$AWK_NEW" "$tmpmd" >"$tmpfile"
 | 
			
		||||
    mv "$tmpfile" "$file"
 | 
			
		||||
    if [ -n "${GIT:-}" ]; then
 | 
			
		||||
      $GIT add "$file"
 | 
			
		||||
      $GIT commit -q -m "File added" -- "$file"
 | 
			
		||||
    fi
 | 
			
		||||
  fi
 | 
			
		||||
  rm "$tmpmd"
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user