NB. dir.js   directory utilities
NB. 
NB.   dir          directory
NB.   dirs         directory browse

SCRIPTNAMES=. 'dir dirs'

NB. dir
dir=: 3 : 0
NB. directory
NB. 
NB. y. = file specification:
NB.
NB. x. is optional:
NB.    - if not given, defaults to 'n'
NB.    - if character, returns a formatted directory,
NB.        where x. is the sort option:
NB.          d=by date 
NB.          n=by name 
NB.          s=by size
NB.    - if numeric, there are 1 or 2 elements:
NB.          0{  0= list short names 
NB.              1= boxed list of full pathnames
NB.              2= full directory list
NB.          1{  0= filenames only (default)
NB.              1= include subdirectories
NB.
NB. subdirectories are shown first
NB.
NB. e.g.  dir ''   
NB.       1 dir ':main:'
'n' dir y.
:
y=. y.
if. 0=#dr=. 1!:0 y do. empty'' return. end.
fls=. 'd' ~: 4{"1>4{"1 dr
if. (1=#dr) *. 0={.fls do.
  x. dir y,':    ' return.
end.
if. fmt=. 2=3!:0 x. do. opt=. 2 1
else. opt=. 2{.x. end.
if. 0={:opt do. fls=. 1#~#dr=. fls#dr end.
if. 0=#dr do. empty'' return. end.
nms=. {."1 dr
nms=. nms tolower@,&.>fls{':';''
ndx=. /: (":,.fls),.>nms
if. 0=opt do. 
  list >ndx{nms 
elseif. 1=opt do. 
  path=. (+./\.y=':')#y
  path&,&.>ndx{nms 
elseif. fmt<2=opt do.
  ndx{nms,.}."1 dr
elseif. fmt do.
  ('nms';'ts';'size')=. |:3{."1 dr
  nms=. tolower L:0 nms
  ds=. ' <dir>   ' ((-.fls)#i.#fls) } 9":,.size
  mth=. _3[\'   JanFebMarAprMayJunJulAugSepOctNovDec'
  f=. > @ ([: _2&{. [: '0'&, ": )&.> 
  'ymdhns'=. f@,&> ,(0 1,:(#ts),1) <;. 3 [ 100|ts
  m=. (1{"1 ts){mth
  time=. d,.'-',.m,.'-',.y,.' ',.h,.':',.n,.':',.s
  dat=. (>nms),.ds,.' ',.time
  dat /: fls,. /:/: >(3|'dns'i.x.){ts;nms;size
elseif. 1 do. 
  'invalid left argument'
end.
)

NB. dirs
NB. browse files in directory
NB. e.g.  dirs ':main:'
dirs=: 3 : 0
if. -. y. -: _1 do.

  y.=. y.,(-. ':'=_1{.y.)#':'
  if. 0=#dr=. 1 dir y. do. 
    wdinfo 'dirs';'no files in directory: ',y. 
    return. 
  end.

  path=. (#~ +./\.@(':'&=))>{.dr
  snms=. ;,&LF@((#path)&}.)&.>dr

  wde 'pc dirs;phandler "dirs _1"'
  wde 'xywh 8 8 126 10;cc s0 static;cn *',,path
  wde 'xywh 8 18 72 87'
  wde 'cc dirsfile listbox ws_vscroll ws_border'
  wde 'cc dirspath edit;setshow dirspath 0;set dirspath *',path
  wde 'cc dirssnms edit;setshow dirssnms 0;set dirssnms *',snms
  wde 'xywh 92 17 40 12;cc view button;cn *&View'
  wde 'xywh 92 31 40 12;cc open button;cn *&Open'
  wde 'xywh 92 45 40 12;cc run button;cn *&Run'
  wde 'xywh 92 67 40 12;cc delete button;cn *&Delete'
  wde 'xywh 92 90 40 12;cc cancel button;cn *&Cancel'
  wde 'pas 7 5;pcenter;'
  wde 'setfocus dirsfile;set dirsfile *',snms
  wde 'setselect dirsfile 0;pshow'

else.

  wdq=. wd 'q'
  ({."1 wdq)=. {:"1 wdq
  
  if. ((<syschild) e. 'view';'dirsfile') +. systype-:'enter' do.
    dirsfile wdview 1!:1 <dirspath,dirsfile

  elseif. syschild-:'open' do.
    wd 'smsel *',dirspath,dirsfile
    wd 'smopen;smfocus'
    wd 'psel dirs;setfocus dirsfile'

  elseif. syschild-:'run' do.
    load dirspath,dirsfile

  elseif. syschild-:'delete' do.
    j=. '"OK to delete file:',LF,LF,dirspath,(dirsfile-.' '),'"'
    j=. 'mb dirs ',j,' mb_iconquestion mb_yesno mb_defbutton2'
    if. 'YES' -: 3{. wd j do. 
      snms=. <;._2 toJ dirssnms
      snms=. ;,&LF &.> snms -. <dirsfile
      wd 'set dirssnms *',snms
      wd 'set dirsfile *',snms
      1!:55 <dirspath,dirsfile
      if. 0 e. #snms do. wd 'pclose' end.
    end.

  elseif. 1 do. wd 'pclose'

  end.

end.
empty''
)


