diff --git a/extra/ls/authors.txt b/extra/ls/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/extra/ls/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/extra/ls/ls-tests.factor b/extra/ls/ls-tests.factor new file mode 100644 index 0000000000..b1c1f18472 --- /dev/null +++ b/extra/ls/ls-tests.factor @@ -0,0 +1,6 @@ +! Copyright (C) 2008 Your name. +! See http://factorcode.org/license.txt for BSD license. +USING: tools.test ls strings kernel ; +IN: ls.tests + +[ ] [ "" ls drop ] unit-test diff --git a/extra/ls/ls.factor b/extra/ls/ls.factor new file mode 100644 index 0000000000..3e21873fec --- /dev/null +++ b/extra/ls/ls.factor @@ -0,0 +1,63 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors arrays assocs combinators generalizations +io.files io.unix.files math.parser sequences.lib calendar math +kernel sequences unix.groups unix.users combinators.cleave +strings combinators.short-circuit unicode.case ; +IN: ls + +TUPLE: ls-info path user group size ; + +: ls-time ( timestamp -- string ) + [ hour>> ] [ minute>> ] bi + [ number>string 2 CHAR: 0 pad-left ] bi@ ":" splice ; + +: ls-timestamp ( timestamp -- string ) + [ month>> month-abbreviation ] + [ day>> number>string 2 CHAR: \s pad-left ] + [ + dup year>> dup now year>> = + [ drop ls-time ] [ nip number>string ] if + 5 CHAR: \s pad-left + ] tri 3array " " join ; + +: read>string ( ? -- string ) "r" "-" ? ; inline + +: write>string ( ? -- string ) "w" "-" ? ; inline + +: execute-string ( str bools -- str' ) + swap { + { { t t } [ >lower ] } + { { t f } [ >upper ] } + { { f t } [ drop "x" ] } + [ 2drop "-" ] + } case ; + +: permissions-string ( permissions -- str ) + { + [ type>> file-type>ch 1string ] + [ user-read? read>string ] + [ user-write? write>string ] + [ [ uid? ] [ user-execute? ] bi 2array "s" execute-string ] + [ group-read? read>string ] + [ group-write? write>string ] + [ [ gid? ] [ group-execute? ] bi 2array "s" execute-string ] + [ other-read? read>string ] + [ other-write? write>string ] + [ [ sticky? ] [ other-execute? ] bi 2array "t" execute-string ] + } concat ; + +: ls ( path -- lines ) + [ [ [ + "" directory-files [ + dup file-info + { + [ permissions-string ] + [ nlink>> number>string 3 CHAR: \s pad-left ] + ! [ uid>> ] + ! [ gid>> ] + [ size>> number>string 15 CHAR: \s pad-left ] + [ modified>> ls-timestamp ] + } swap suffix " " join + ] map + ] with-group-cache ] with-user-cache ] with-directory ; diff --git a/extra/ls/tags.txt b/extra/ls/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/ls/tags.txt @@ -0,0 +1 @@ +unportable