
set scrolloff 10      # space on top and bottom of screen
set icons true          # show icons next to files
set period 1          # interval to check for directory updates
set hidden true         # show hidden files by default
set incsearch true       # jump to first match after each keystroke in search
set dircounts true      # show number of items in directory instead of sie
set info "size"       # list directory information on the right
set sortby "natural"  # don't sort files in any special way
# set drawbox true        # draw a border around all panes
set globsearch false  # don't use glob patterns in search
set mouse false  # enable mouse support
set number true  # show line numbers

set preview true        # preview file on the right pane
# set previewer chafa --format sixel --work 9 --optimize 9 --stretch --zoom --size 140x80
set previewer ~/code/machineconfig/src/machineconfig/settings/lf/linux/exe/previewer.sh
set cleaner ~/code/machineconfig/src/machineconfig/settings/lf/linux/exe/cleaner.sh
# set previewer pistol

# set sixels true
# set SHELL bash
# set EDITOR code 
# set colors true
set ratios '1:2:3'  # ratio of pane widths
# set hiddenfiles ".*:*.aux:*.log:*.bbl:*.bcf:*.blg:*.run.xml"
# see more at https://git.lmburns.com/dotfiles-mac/file/.config/lf/lfrc.html


# set ignorecase true
# see:
# https://github.com/gokcehan/lf/wiki/Integrations#ripgrep
# https://github.com/gokcehan/lf/wiki/Integrations#fzf
# https://github.com/junegunn/fzf/blob/master/ADVANCED.md#ripgrep-integration


# Unix-like defaults1
#           $OPENER  open      # macos
#           $OPENER  xdg-open  # others
          # $EDITOR  code  # not vi
#           $PAGER   less
#           $SHELL   sh


# ============================ KEY BINDINGS ============================================
# avoid using confusing keys that are already bound to other functions by other programs, e.g. the console itself, like ctrl+c
# default reference: https://pkg.go.dev/github.com/gokcehan/lf


# test image viewer
map Q !~/code/machineconfig/src/machineconfig/settings/lf/linux/exe/previewer.sh "$f"

# key bindings
map vmap v  # default is invert
map V invert
map p
map x
map c
map v
# map D
map D $diskonaut
map K $kondo $f


map q quit  # default
# remove defaults
map k  # default up
map <c-u>  # half-up

# e edit
map e  # edit modal key.
map eh $hx $f
map eH !source ~/code/machineconfig/.venv/bin/activate;hx $f
map en $nano $f
map el $lvim $f
map ev $nvim $f
map ec $code $f
# map ep $pycharm
map o $start $f
map <enter> open


# w shells
map w
map ww bash
# map wp $powershell
map wr $~/scripts/croshell
# map wm $cmd
map i !~/code/machineconfig/src/machineconfig/settings/lf/linux/exe/previewer.sh $f
map I !cat $f | gum pager
map R $~/scripts/croshell -r $f
map O $~/scripts/croshell -rj $f


# o file/folder Operations
map c clear
map <delete> delete

map o
map od cut
map op paste
map oy copy
map oc $echo $f | xclip
map or rename
map of reload
map os mark-save
map ol mark-load
map oR mark-remove
map oD $ouch d $f
map oC $ouch c $f "$f.zip"
map oL $ln -s $f $HOME/tmp_results/tmp_links/$(basename "$f")

# j jump
map j
map jc cd ~/code
map jd cd ~/data
map jD cd ~/Downloads
map jx cd ~/dotfiles
# map jm cd ~/code/machineconfig/src/machineconfig
map jh cd ~
map jj $~/scripts/croshell -j --read $f
# map jj $lvim ~/code/machineconfig/src/machineconfig/settings/lf/linux/lfrc

# from https://github.com/gokcehan/lf/wiki/Integrations#zoxide
cmd zi ${{
    result="$(zoxide query -i -- "$@")"
    lf -remote "send ${id} cd '${result}'"
}}
map Z zi

cmd ter ${{
    result="$(tere query -i -- "$@")"
    lf -remote "send ${id} cd '${result}'"
}}
map T ter #!tere

# m make
map m
map md mkdir
map mf mkfile
map mF sudomkfile
map mp chmod


# =============== JUNK =================
map <c-f> :fzf_jump
map gs :fzf_search
map J broot_jump
map F fzf_jump


# from https://github.com/gokcehan/lf/wiki/Integrations#fzf
cmd fzf_jump ${{
    res="$(find . -maxdepth 100 | fzf --reverse --header='Jump to location' | sed 's/\\/\\\\/g;s/"/\\"/g')"
    if [ -d "$res" ] ; then
        cmd="cd"
    elif [ -f "$res" ] ; then
        cmd="select"
    else
        exit 0
    fi
    lf -remote "send $id $cmd \"$res\""
}}


cmd fzf2g $(~/scripts/fzf2g.sh $args[1])
# map F $nano fzf2g $args[1]


cmd fzf_search ${{
    res="$( \
        RG_PREFIX="rg --column --line-number --no-heading --color=always \
            --smart-case "
        FZF_DEFAULT_COMMAND="$RG_PREFIX ''" \
            fzf --bind "change:reload:$RG_PREFIX {q} || true" \
            --ansi --layout=reverse --header 'Search in files' \
            | cut -d':' -f1
    )"
    [ ! -z "$res" ] && lf -remote "send $id select \"$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 % echo "hi from bottom line of lf"  # this function acts in the bottom line, unlike this full-blown acting in its own screen:
cmd mkdir ${{
  printf "Directory Name: "
  read ans
  mkdir $ans
}}

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

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


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

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

  lf -remote 'send reload'
}}

