#!/bin/bash

# Navigate using fzf
fzf_nav() {
[ -d "$1" ] && cd "$1"
opt=$(fzf --layout=reverse-list --cycle --preview 'p {}' --height=50% --border top --prompt='⯈ ')
[ -n "$opt" ] && o "$opt"
}

# EZ note taking
note() {
NOTES_DIR="$HOME/Nextcloud/Notes"
# Just edit today's note if no argument is given
[ -z "$1" ] && cd "$NOTES_DIR" && "$EDITOR" "note-$DATE.md"
[ -e "$NOTES_DIR/daily-notes/note-$DATE.md" ] &&
    printf "\n\033[1m\033[35mNote of the day:\033[0m \033[2mdaily-notes/\033[0mnote-$DATE.md\n"
# 'list' arg will list notes either with fzf or regular ls
if [ "$1" = "list" ] ; then
    cd "$NOTES_DIR"
    # If fzf is present, use it, ls otherwise
    [ -e '/usr/bin/fzf' ] || ls && fzf_nav
fi
}

# Copy output of a command
copy_history() {
HISTTIMEFORMAT="%F %T "
history |
    head -n -1 |
    fzf --cycle --height=50% --border top --tac --prompt='⯈ ' |
    sed -z 's/^.*:[0-9]*\s*//g;s/\s*$//g;s/\n//g' |
    xsel -ib
}