# interpreter for shell commands
set shell bash

# set '-eu' options for shell commands
# These options are used to have safer shell commands. Option '-e' is used to
# exit on error and option '-u' is used to give error for unset variables.
# Option '-f' disables pathname expansion which can be useful when $f, $fs, and
# $fx variables contain names with '*' or '?' characters. However, this option
# is used selectively within individual commands as it can be limiting at
# times.
set shellopts '-eu'

# set internal field separator (IFS) to "\n" for shell commands
# This is useful to automatically split file names in $fs and $fx properly
# since default file separator used in these variables (i.e. 'filesep' option)
# is newline. You need to consider the values of these options and create your
# commands accordingly.
set ifs "\n"

# leave some space at the top and the bottom of the screen
set scrolloff 10

# Use the `dim` attribute instead of underline for the cursor in the preview pane
set cursorpreviewfmt "\033[7;2m"

# Use icons
set icons

# Preview files
set previewer ~/.config/lf/lf_kitty_preview
set cleaner ~/.config/lf/lf_kitty_clean

# define a custom 'open' command
# This command is called when current file is not a directory. You may want to
# use either file extensions and/or mime types here. Below uses an editor for
# text files and a file opener for the rest.
cmd open &{{
    case $(file --mime-type -Lb $f) in
        text/*) lf -remote "send $id \$$EDITOR \$fx";;
        application/json) lf -remote "send $id \$$EDITOR \$fx";;
        *) for f in $fx; do $OPENER $f > /dev/null 2> /dev/null & done;;
    esac
}}

# extract the current file with the right command
# (xkcd link: https://xkcd.com/1168/)
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;;
        *.tar.zst) tar --zstd -xvf $f;;
        *.zip) unzip $f;;
        *.rar) unrar x $f;;
        *.7z) 7z x $f;;
    esac
}}

# custom commands
#
# files and dirs
cmd mkdir ${{
    printf "Directory name: "
    read ans
    mkdir $ans
}}

cmd mkfile ${{
    printf "File name: "
    read ans
    touch $ans
}}

# compress current file or selected files with tar and gunzip
cmd tar ${{
    printf "Archive name: "
    read ans
    mkdir $ans
    cp -r $fx $ans
    tar czf $ans.tar.gz $ans
    rm -rf $ans
}}

# compress current file or selected files with zip
cmd zip ${{
    printf "Archive name: "
    read ans
    mkdir $ans
    cp -r $fx $ans
    zip -r $ans.zip $ans
    rm -rf $ans
}}

# trash management
cmd trash ${{
    files=$(printf "$fx" | tr '\n' ';')

    while [ "$files" ]; do
        file=${files%%;*}

        trash-put "$(basename  "$file")"

        if [ "$files" = "$file" ]; then
            files=""
        else
            files="${files#*;}"
        fi
    done
}}

cmd trash-clear %trash-empty

cmd trash-restore ${{
    trash-restore
}}

cmd on-select &{{
    lf -remote "send $id set statfmt \"$(eza -ld --color=always "$f")\""
}}

# keybindings
#
# remove some defaults
map m
map d
map t

# use enter for shell commands
map <enter> shell

# show the result of execution of previous commands
map ` !true

# execute current file (must be executable)
map x $$f
map X !$f

# custom keybindings
#
# goto directory
map gc cd ~/.config
map gd cd ~/Development
map gv cd ~/Videos
map gm cd ~/Music
map gp cd ~/Pictures
map ga cd /run/user/1000/gvfs
map gw cd ~/Downloads
map go cd ~/Documents
map gr cd /

# archives
map ax extract
map at tar
map az zip

# files and dirs
map md mkdir
map mf mkfile
map dc cut

# trash
map dd trash
map tc trash-clear
map tr trash-restore