]> gitweb.factorcode.org Git - factor.git/blob - extra/spotlight/spotlight.factor
functors: inline the parts of interpolate this needs
[factor.git] / extra / spotlight / spotlight.factor
1 ! Copyright (C) 2013 Charles Alston, John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays formatting help.stylesheet io io.encodings.utf8
4 io.launcher io.pathnames io.styles kernel locals memoize
5 namespaces sequences sequences.generalizations splitting
6 wrap.strings ;
7 IN: spotlight
8
9 ! -----
10 ! TO DO:
11 ! -need to test sudo-mdutil; intercept auth prompt
12 ! -handle case-sensitivity properly (OS X)
13 ! -test composing variant shell command constructions,
14 !  i.e., those which do or don't need spaces, parens,
15 !  single quotes, etc. (work through examples at end of file)
16 ! -access o.e.d & calculator through spotlight
17 ! -emit some sort of 'not-found' msg for unsuccessful search
18 ! -trap errors! ...
19 ! -----
20
21 : attr== ( item-name attr-name -- string )
22     swap "%s == '%s'" sprintf ;
23
24 : attr!= ( item-name attr-name -- string )
25     swap "%s != '%s'" sprintf ;
26
27 : attr> ( item-name attr-name -- string )
28     swap "%s > '%s'" sprintf ;
29
30 : attr>= ( item-name attr-name -- string )
31     swap "%s >= '%s'" sprintf ;
32
33 : attr< ( item-name attr-name -- string )
34     swap "%s < '%s'" sprintf ;
35
36 : attr<= ( item-name attr-name -- string )
37     swap "%s <= '%s'" sprintf ;
38
39 : attr&& ( attr1 attr2 -- string )
40     " && " glue ;
41
42 : attr|| ( attr1 attr2 -- string )
43     " || " glue ;
44
45 <PRIVATE
46
47 : run-process-output ( command -- seq )
48     utf8 [ read-lines ] with-process-reader ;
49
50 PRIVATE>
51
52 : mdfind ( query -- results )
53     "mdfind -onlyin . %s" sprintf run-process-output ;
54
55 : mdfind. ( query -- )
56     mdfind [ dup <pathname> write-object nl ] each ;
57
58 : mdls ( path -- )
59     absolute-path "mdls" swap 2array run-process-output
60     [ print ] each ;
61
62 : mdutil ( flags on|off volume -- seq )
63     [ "mdfind" swap "-" prepend "-i" ] 2dip 5 narray
64     run-process-output ;
65
66 : mdimport ( path -- seq )
67     absolute-path "mdimport " prepend run-process-output ;
68
69 : mdimport-with ( path options -- seq )
70     swap absolute-path "mdimport %s %s" sprintf run-process-output ;
71
72 MEMO: kMDItems ( -- seq )
73     "mdimport -A" run-process-output
74     [ "'kMDItem" head? ] filter
75     [ "\t" split harvest [ but-last rest ] map ] map ;
76
77 : kMDItems. ( -- )
78     kMDItems table-style get [
79         [
80             [
81                 [ [ 64 wrap-string write ] with-cell ] each
82             ] with-row
83         ] each
84     ] tabular-output nl ;