#!/usr/bin/env sh
#
# Managing notes with fzf (https://github.com/junegunn/fzf)
# - CTRL-L: List note files in descending order by their modified time
# - CTRL-F: Search file contents
#
# Configuration:
# - $CHT_DIR: Directory where note files are located
# - $CHT_EXT: Note file extension (default: txt)

CHT_DIR=${CHT_DIR:-$HOME/.dotfiles/cheats}
EDITOR=${EDITOR:-vim}
export CHT_EXT=${CHT_EXT:-md}
export NORENAME=1
cd "$CHT_DIR"

# delete() {
#   echo -en "\r\x1b[KDelete $1? (y/n) "
#   read yn
#   if [[ "$yn" =~ ^y ]]; then
#     mkdir -p "$TRASH_DIR"
#     mv "$CHT_DIR/${1}.$CHT_EXT" "$TRASH_DIR"
#   fi
# }

key=ctrl-l
query="$*"
opts='--reverse --no-hscroll --no-multi --ansi --print-query --tiebreak=index'
while true; do
  if [ "$key" = ctrl-l ]; then
    out=$(
      ruby --disable=gems -x "$0" "$CHT_DIR" list |
      fzf $opts --prompt="list> " --expect=ctrl-f,alt-n --query="$query" \
        --preview 'cat {1}.$CHT_EXT' \
        --no-clear --header=$'\nCTRL-F: find / ALT-N: new\n\n')
  else
    out=$(
      ruby --disable=gems -x "$0" "$CHT_DIR" find |
      fzf $opts --prompt="find> " --expect=ctrl-l,alt-n \
          --delimiter=: --nth=3.. --query="$query" \
          --preview 'bat {1}.$CHT_EXT --color=always --decorations=never --highlight-line={2}' --preview-window 'down,+{2}/2' \
          --no-clear --header=$'\nCTRL-L: list / ALT-N: new\n\n')
  fi

  # 2: Error / 130: Interrupt
  (( $? % 128 == 2 )) && exit 1

  lines=$(wc -l <<< "$out")
  [ $lines -lt 2 ] && continue

  if [ "$key" = ctrl-l ]; then
    file=$(tail -1 <<< "$out" | awk 'BEGIN { FS = "\t" } { gsub(/ +$/, "", $1); print $1 }')
  else
    file=$(tail -1 <<< "$out" | awk -F: '{print $1}')
  fi

  newkey=$(head -2 <<< "$out" | tail -1)
  case "$newkey" in
    ctrl-*) key=$newkey ;;
    alt-n)
      query=$(head -1 <<< "$out")
      [ -n "$query" ] && $EDITOR "$CHT_DIR/${query}.$CHT_EXT"
      ;;
    *)
      if [ "$key" = ctrl-l ]; then
        [ -n "$file" ] && $EDITOR "$CHT_DIR/${file}.$CHT_EXT"
      else
        # Assuming Vim
        cmd=$(tail -1 <<< "$out" |
              awk 'BEGIN { FS = ":" } { print "$EDITOR \"'$CHT_DIR'/" $1 ".'$CHT_EXT'\" +" $2 }')
        sh -c "$cmd"
      fi
      ;;
  esac
done


#!ruby
# encoding: utf-8

def list
  Dir[ARGV.first + '/*.' + ENV['CHT_EXT']]
# shellcheck disable=SC1036,SC1056,SC1072,SC1073,SC1083
    .map { |f| { time: File.mtime(f),
                 path: f,
                 name: File.basename(f).chomp('.' + ENV['CHT_EXT']) } }
    .sort_by { |h| [- h[:time].to_f, h[:name]] }
end

if ARGV.last == 'list'
  list.each do |h|
    puts "\x1b[1m#{h[:name].ljust(50)}\t\x1b[0;36m#{h[:time]}\x1b[m"
  end rescue exit
else
  list.each do |h|
    File.open(h[:path]).each_with_index do |line, no|
      next if line =~ /^\s*$/
      puts "\x1b[1m#{h[:name]}\x1b[m:\x1b[33m#{no + 1}\x1b[m: "
           .ljust(40) << line
    end
  end rescue exit
end
