Skip to content

Commit cc96b3b

Browse files
committed
CHANGE/FIX: rewritten list-dir to use recent dir-tree function.
The column file list is now removed as it was ugly and not much practical. Now it always display result as a directory tree. Just if there is not specified recursion (/r), the depth is limited to 1. This is backward compatibility breaking change, but easy to fix if needed. It greatly improve readability of the output (at least for my personal use). I can imagine, that it could be even more improved (like adding better filtering and or sorting). I've also added `dir` as an alias to `list-dir` (as it is in Red language).
1 parent dd2ec13 commit cc96b3b

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

src/mezz/mezz-files.r

+48-33
Original file line numberDiff line numberDiff line change
@@ -219,50 +219,65 @@ dir-tree: func [
219219
exit
220220
]
221221

222-
list-dir: func [
222+
list-dir: closure/with [
223223
"Print contents of a directory (ls)."
224224
'path [file! word! path! string! unset!] "Accepts %file, :variables, and just words (as dirs)"
225-
/l "Line of info format"
226225
/f "Files only"
227226
/d "Dirs only"
228-
; /t "Time order"
229227
/r "Recursive"
230-
/i indent
231-
/local files save-dir info
228+
/i indent [string! char!]
229+
/l "Limit recursive output to given maximal depth"
230+
max-depth [integer!]
232231
][
233-
save-dir: what-dir
234-
switch type?/word :path [
235-
unset! [] ; Stay here
236-
file! [change-dir path]
237-
string! [change-dir to-rebol-file path]
238-
word! path! [change-dir to-file path]
232+
if f [r: l: false]
233+
recursive?: any [r max-depth]
234+
files-only?: f
235+
apply :dir-tree [
236+
:path d i indent
237+
true either recursive? [:max-depth][1]
238+
true :on-value
239+
i indent
239240
]
240-
if r [l: true]
241-
unless l [l: make string! 62] ; approx width
242-
unless indent [indent: ""]
243-
files: attempt [read %./]
244-
if not files [print ["Not found:" :path] change-dir save-dir exit]
245-
foreach file files [
246-
if any [
247-
all [f dir? file]
248-
all [d not dir? file]
249-
][continue]
250-
either string? l [
251-
append l file
252-
append/dup l #" " 15 - remainder length? l 15
253-
if greater? length? l 60 [print l clear l]
241+
][
242+
recursive?: files-only?: none
243+
on-value: func[
244+
value depth
245+
/local info date time size
246+
][
247+
info: query/mode value [name size date]
248+
if depth = 0 [
249+
return ajoin ["^[[33;1mDIR: ^[[32;1m" to-local-file info/1 "^[[m"]
250+
]
251+
;@@ TODO: rewrite this date/time formating once it will be possible
252+
;@@ with some better method!
253+
date: info/3
254+
date/zone: 0
255+
time: date/time
256+
time: format/pad [2 #":" 2 ] reduce [time/hour time/minute] #"0"
257+
date: format/pad [-11] date/date #"0"
258+
date: ajoin ["^[[32m" date " " time "^[[m "]
259+
260+
size: any [info/2 0]
261+
if size >= 100'000'000 [size: join to integer! round (size / 1'000'000) "M"]
262+
263+
either dir? value [
264+
if files-only? [return none]
265+
ajoin [
266+
date "^[[32;1m"
267+
either recursive? [
268+
to-local-file info/1
269+
][ join " " dirize second split-path info/1]
270+
"^[[m"
271+
]
254272
][
255-
info: get query file
256-
change info second split-path info/1
257-
printf [indent 16 -8 #" " 24 #" " 6] info
258-
if all [r dir? file] [
259-
list-dir/l/r/i :file join indent " "
273+
format [date $33 -8 $0 #" "] reduce [
274+
size
275+
"^[[33;1m"
276+
second split-path info/1
277+
"^[[m"
260278
]
261279
]
262280
]
263-
if all [string? l not empty? l] [print l]
264-
change-dir save-dir
265-
exit
266281
]
267282

268283
undirize: func [

src/mezz/mezz-shell.r

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ REBOL [
1111
}
1212
]
1313

14-
ls: :list-dir
14+
ls:
15+
dir: :list-dir
1516
pwd: :what-dir
1617
rm: :delete
1718
mkdir: :make-dir

0 commit comments

Comments
 (0)