factor/basis/tools/files/files.factor

75 lines
2.2 KiB
Factor
Raw Normal View History

2008-11-14 01:25:00 -05:00
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays combinators io io.files io.files.info
io.directories kernel math.parser sequences system vocabs.loader
calendar math fry prettyprint ;
IN: tools.files
2008-11-14 01:25:00 -05:00
SYMBOLS: permissions file-name nlinks file-size date ;
2008-11-14 01:51:14 -05:00
<PRIVATE
2008-11-14 01:25:00 -05:00
: ls-time ( timestamp -- string )
[ hour>> ] [ minute>> ] bi
2008-12-08 00:51:13 -05:00
[ number>string 2 CHAR: 0 pad-left ] bi@ ":" glue ;
2008-11-14 01:25:00 -05:00
: 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
2008-11-14 01:44:01 -05:00
: execute>string ( ? -- string ) "x" "-" ? ; inline
2008-11-14 01:25:00 -05:00
HOOK: (directory.) os ( path -- lines )
2008-11-14 01:51:14 -05:00
PRIVATE>
2008-11-14 01:25:00 -05:00
: directory. ( path -- )
[ (directory.) ] with-directory-files [ print ] each ;
2008-12-08 00:51:13 -05:00
SYMBOLS: device-name mount-point type
available-space free-space used-space total-space
percent-used percent-free ;
: percent ( real -- integer ) 100 * >integer ; inline
: file-system-spec ( file-system-info obj -- str )
{
2008-12-09 22:30:11 -05:00
{ device-name [ device-name>> [ "" ] unless* ] }
{ mount-point [ mount-point>> [ "" ] unless* ] }
{ type [ type>> [ "" ] unless* ] }
2008-12-09 01:58:34 -05:00
{ available-space [ available-space>> [ 0 ] unless* ] }
{ free-space [ free-space>> [ 0 ] unless* ] }
{ used-space [ used-space>> [ 0 ] unless* ] }
{ total-space [ total-space>> [ 0 ] unless* ] }
2008-12-08 00:51:13 -05:00
{ percent-used [
2008-12-09 01:58:34 -05:00
[ used-space>> ] [ total-space>> ] bi
[ [ 0 ] unless* ] bi@ dup 0 =
2008-12-08 00:51:13 -05:00
[ 2drop 0 ] [ / percent ] if
] }
} case ;
: file-systems-info ( spec -- seq )
file-systems swap '[ _ [ file-system-spec ] with map ] map ;
: print-file-systems ( spec -- )
2008-12-08 00:51:13 -05:00
[ file-systems-info ]
[ [ unparse ] map ] bi prefix simple-table. ;
: file-systems. ( -- )
{ device-name available-space free-space used-space total-space percent-used mount-point }
print-file-systems ;
2008-11-14 01:25:00 -05:00
{
{ [ os unix? ] [ "tools.files.unix" ] }
{ [ os windows? ] [ "tools.files.windows" ] }
2008-11-14 01:25:00 -05:00
} cond require