#!/usr/bin/env bosh

#
# Simple bosh file browser
#
# Keys:
#
# c - change to selected directory
# h - change to home directory
# m - view file
# e - edit file in $EDITOR (falls back to vi)

refresh=1
uservars=1

common{{
  function bof_ls {
    BOSHVAR1="$PWD"
    ( [ "$BOSHVAR1" = "/" ] || echo '..'; ls -p1 | grep '/' | tr -d '/') | sed 's/^/[/' | sed 's/$/]/'
    ls -p1 | grep -v '/'
    BOSHTITLE="$BOSHVAR1"
  }
}}

# Main command
command{{
  if [ -z "$BOSHVAR1" ]
  then
    if [ -n "$1" ]
    then
      if [ -d "$1" ]  
      then
        BOSHVAR1="$1"
      else
        BOSHERR="bof: directory '$1' does not exist"
        return 1
      fi
    else
      BOSHVAR1="."
    fi
  fi
  cd "$BOSHVAR1"
  bof_ls
}}

# Pre-action
preaction{{
  [ "$BOSHVAR1" = "/" ] && BOSHVAR1=""
  f="$BOSHVAR1/$(echo $BOSH | tr -d '[]')"
}}


# Actions:

# chdir
c[.]{{
  if [ -d "$f" ]
  then
    BOSHVAR1="$f"
  fi
  cd "$BOSHVAR1"
  bof_ls
}}

# home
h[.]{{
  cd
  bof_ls
}}

# more
m[>]{{
  cat "$f"
  BOSHTITLE="$f"
}}

# edit
e[!]{{
  [ -z "$EDITOR" ] && EDITOR=vi
  "$EDITOR" "$f"
}}
