# blake's lf config

# --- folder specific ---

# timesheets show recent first
setlocal ~/documents/holocron/work/nhc/timesheets/2024/ reverse

# --- options ---

# file previews
set previewer ctpv
set cleaner ctpvclear
&ctpv -s $id
&ctpvquit $id

# mouse
set mouse true

# search
set ignorecase true

# --- mappings ---

# shortcuts
map gb cd /holocron
map gz cd %{{ [ -d /holocron ] && printf /holocron || printf /media/holocron }}
map gn cd ~/.nix
map gc cd ~/.config

# navigation
map [ half-up
map ] half-down

# files & archives
map ad mkdir
map af mkfile
map ab mkbak
map ae extract
map aa archive

# trash
map gt cd ~/.local/share/Trash/files
map <delete> trash
map <s-delete> $trash restore

# --- functions ---

# extract any archive
cmd extract %{{
    set -f
    case $f in
        *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf $f;;
        *.tar.gz|*.tgz) tar xzvf $f;;
        *.tar.xz|*.txz) tar xJvf $f;;
        *.zip) unzip $f;;
        *.rar) unrar x $f;;
        *.7z) 7z x $f;;
    esac
}}

cmd archive %{{
    echo "enter archive name: " ; read archive_name
    if [ -z "$archive_name" ]; then
        echo "No archive name provided."
        exit 1
    fi

    IFS="$(printf '\n\t')"
    file_list=()

    for x in $fx; do
        file_list+=("$x")
    done

    case "$archive_name" in
        *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar cjf "$archive_name" "${file_list[@]}" ;;
        *.tar.gz|*.tgz) tar czf "$archive_name" "${file_list[@]}" ;;
        *.tar.xz|*.txz) tar cJf "$archive_name" "${file_list[@]}" ;;
        *.zip) zip -r "$archive_name" "${file_list[@]}" ;;
        *.rar) rar a "$archive_name" "${file_list[@]}" ;;
        *.7z) 7z a "$archive_name" "${file_list[@]}" ;;
        *) echo "Unsupported archive format" ;;
    esac

}}

# move files to trash
cmd trash %IFS="$(printf '\n\t')"; trash put $fx

# make a directory
cmd mkdir %{{
    echo "enter directory name: " ; read dir
    if [ -n "$dir" ]; then
        mkdir -p "$dir" && echo "'$dir' created."
    else
        echo "no dir name provided"
    fi
}}

# make a file
cmd mkfile %{{
    echo "enter file name: " ; read file
    if [ -n "$file" ]; then
        touch "$file" && echo "'$file' created."
    else
        echo "no file name provided"
    fi
}}

# make backup
cmd mkbak %{{
  for x in $fx; do
    cp -r $x $x.bak
  done

}}

# git stuff
cmd git_branch ${{
    git branch | fzf | xargs git checkout
    pwd_shell="$(pwd | sed 's/\\/\\\\/g;s/"/\\"/g')"
    lf -remote "send $id updir; cd \"$pwd_shell\""
}}
map gb :git_branch
map gp $clear; git pull --rebase || true; echo "press ENTER"; read ENTER
map gs $clear; git status; echo "press ENTER"; read ENTER
map gl $clear; git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

set info custom

cmd on-load &{{
    cd "$(dirname "$1")" || exit 1
    [ "$(git rev-parse --is-inside-git-dir 2>/dev/null)" = false ] || exit 0

    cmds=""

    for file in "$@"; do
        case "$file" in
            */.git|*/.git/*) continue;;
        esac

        status=$(git status --porcelain --ignored -- "$file" | cut -c1-2 | head -n1)

        if [ -n "$status" ]; then
            cmds="${cmds}addcustominfo ${file} \"$status\"; "
        else
            cmds="${cmds}addcustominfo ${file} ''; "
        fi
    done

    lf -remote "send $id :$cmds"
}}

cmd on-cd &{{
    # display git repository status in your prompt
    source /usr/share/git/completion/git-prompt.sh
    GIT_PS1_SHOWDIRTYSTATE=auto
    GIT_PS1_SHOWSTASHSTATE=auto
    GIT_PS1_SHOWUNTRACKEDFILES=auto
    GIT_PS1_SHOWUPSTREAM=auto
    GIT_PS1_COMPRESSSPARSESTATE=auto
    git="$(__git_ps1 " [GIT BRANCH:> %s]")" || true
    fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[33;1m$git\033[0m"
    lf -remote "send $id set promptfmt \"$fmt\""
}}

cmd on-cd &{{
    # display repository status in your prompt
    if [ -d .git ] || [ -f .git ]; then
        branch="$(git branch --show-current 2>/dev/null)" || true
        remote="$(git config --get "branch.$branch.remote" 2>/dev/null)" || true
        url="$(git remote get-url "$remote" 2>/dev/null)" || true
        fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[33;1m [GIT BRANCH:> $branch >> $url]\033[0m"
    elif [ -d .hg ]; then
        hg="$(hg branch 2>/dev/null)" || true
        fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[33;1m [HG BRANCH:> $hg]\033[0m"
    elif [ -d .svn ]; then
        svn="$(svn info 2>/dev/null | awk '/^URL: /{print $2}')" || true
        fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[33;1m [SVN URL:> $svn]\033[0m"
    else
        fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"
    fi
    lf -remote "send $id set promptfmt \"$fmt\""
}}
