#!/usr/bin/ruby
# Browser example — file-system directory browser
#
# Usage:
#   cd example && ruby browser
#   ruby browser /path/to/dir         Start in a specific directory
#   ruby browser --select             Start in pick mode (returns path)
#
# Navigates directories and displays files with size and type.
# Pass a directory path as an argument to start there.
# Pass --select to enter pick mode — selected file is printed to stdout on exit.
#
# Keys:
#   arrows / jk   Navigate up/down
#   enter          Open directory or pick file (in select mode)
#   /              Filter files by name
#   esc            Exit
#
# When run with --select, the chosen file path is printed
# to stdout after the TUI exits (piped to calling process).

require_relative '../lib/typr.rb'

class BrowserTest
  include Typr
  def initialize
    Typr.init 
    $conf=$*.map{|arg| [arg.start_with?(/\w/), arg] }.to_h 
    @browser = Browser.new( right: -1, bottom: -1,
      directory: $conf[false], colors: { hints: [:white, :black] } )
  end

  def run
    Typr.clear
    $file = @browser.pick $conf[true] 
  end
end

begin
  BrowserTest.new.run
ensure
  Typr.exit
  puts $file
end 

