set previewer ~/.config/lf/preview
# Basic Settings
set preview true
set hidden true
set color256 true
set drawbox true
set icons true
set ignorecase true

# Custom Functions
cmd open ${{
    case $(file --mime-type "$f" -bL) in
        text/*|application/json) edit "$f";;
        video/*|image/*/application/pdf) xdg-open "$f";;
        *) xdg-open "$f" ;;
    esac
}}

cmd mkdir ${{
  printf "Directory Name: "
  read ans
  mkdir $ans
}}

cmd mkfile ${{
  printf "File Name: "
  read ans
  $EDITOR $ans
}}

cmd chmod ${{
  printf "Mode Bits: "
  read ans

  for file in "$fx"
  do
    chmod $ans $file
  done

  lf -remote 'send reload'
}}

cmd setwallpaper %cp "$f" ~/.config/wall.png && xwallpaper --zoom "$f"

cmd fzf_jump ${{
  res="$(find . -maxdepth 3 | fzf --reverse --header='Jump to location')"
  if [ -f "$res" ]; then
    cmd="select"
  elif [ -d "$res" ]; then
    cmd="cd"
  fi
  lf -remote "send $id $cmd \"$res\""
}}

cmd broot_jump ${{
  f=$(mktemp)
  res="$(broot --outcmd $f && cat $f | sed 's/cd //')"
  rm -f "$f"
  if [ -f "$res" ]; then
    cmd="select"
  elif [ -d "$res" ]; then
    cmd="cd"
  fi
  lf -remote "send $id $cmd \"$res\""
}}

cmd open_config  ${{
  $EDITOR $(bookmenu -b ~/.config/bookmenu/configs -f fzf -o)
}}

cmd dragon %dragon-drag-and-drop -a -x $fx
cmd dragon-stay %dragon-drag-and-drop -a $fx
cmd dragon-individual %dragon-drag-and-drop $fx
cmd dlfile %dlfile

# Archive bindings
cmd unarchive ${{
  case "$f" in
      *.zip) unzip "$f" ;;
      *.tar.gz) tar -xzvf "$f" ;;
      *.tar.bz2) tar -xjvf "$f" ;;
      *.tar) tar -xvf "$f" ;;
      *) echo "Unsupported format" ;;
  esac
}}

cmd zip %zip -r "$f" "$f"
cmd tar %tar cvf "$f.tar" "$f"
cmd targz %tar cvzf "$f.tar.gz" "$f"
cmd tarbz2 %tar cjvf "$f.tar.bz2" "$f"

# Trash cli bindings
cmd trash ${{
  files=$(printf "$fx" | tr '\n' ';')
  while [ "$files" ]; do
    # extract the substring from start of string up to delimiter.
    # this is the first "element" of the string.
    file=${files%%;*}

    trash-put "$(basename "$file")"
    # if there's only one element left, set `files` to an empty string.
    # this causes us to exit this `while` loop.
    # else, we delete the first "element" of the string from files, and move onto the next.
    if [ "$files" = "$file" ]; then
      files=''
    else
      files="${files#*;}"
    fi
  done
}}

cmd clear_trash %trash-empty

cmd restore_trash ${{
  trash-restore
}}

cmd stripspace %stripspace "$f"

# Bindings
# Remove some defaults
map m
map o
map n
map "'"
map '"'
map d
map c
map e
map f

# Not really image preview
map - $~/.config/lf/draw_img "$f"

cmd video_preview ${{
    CACHE=$(mktemp /tmp/thumb_cache.XXXXX)
    ffmpegthumbnailer -i "$f" -o $CACHE -s 0
    ~/.config/lf/draw_img $CACHE && rm $CACHE
}}
map + :video_preview

# File Openers
map ee $$EDITOR "$f"
map u $view "$f"

# Archive Mappings
map az zip
map at tar
map ag targz
map ab targz
map au unarchive

# Trash Mappings
map dd trash
map tc clear_trash
map tr restore_trash

# Broot Mapping
map f broot_jump

# Dragon Mapping
map dr dragon
map ds dragon-stay
map di dragon-individual

map ss stripspace

# Basic Functions
map . set hidden!
map DD delete
map p paste
map x cut
map y copy
map <enter> open
map mf mkfile
map mr sudomkfile
map md mkdir
map ch chmod
map bg setwallpaper
map o open_config
map br $vimv $fx
map r rename
map H top
map L bottom
map R reload
map C clear
map U unselect

# Movement
map gtr cd ~/.local/share/Trash/files

#map gd cd $DATA/4-incubator/downloads
map gc cd ~/.config
map gl cd ~/.local
map gE cd /etc
map gU. cd /usr
map gUs cd /usr/share
