-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.hs
executable file
·164 lines (133 loc) · 4.01 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- Main.hs
--
-- Adelie is a collection of scripts to querying portage packages.
module Main (main) where
import System.Environment (getArgs, getProgName)
import Adelie.Colour
import Adelie.Options
import Adelie.QChangelog
import Adelie.QCheck
import Adelie.QDepend
import Adelie.QHasUse
import Adelie.QList
import Adelie.QOwn
import Adelie.QSize
import Adelie.QUse
import Adelie.QWant
type CommandProc = [String] -> IO ()
data Command
= Short String String CommandProc
| Long String String String CommandProc
logCommands :: [Command]
logCommands = [
Long "c" "changes"
"list changes since the installed version"
qChangelog,
Short "cl"
"find the changelog of a package"
qLogFile
]
listCommands :: [Command]
listCommands = [
Long "f" "files"
"list the contents of a package"
(qList ListAll),
Short "fd"
"list the directories in a package"
(qList ListDirs),
Short "ff"
"list the files in a package"
(qList ListFiles),
Short "fl"
"list the links in a package"
(qList ListLinks)
]
ownCommands :: [Command]
ownCommands = [
Long "b" "belongs"
"find the package(s) owning a file"
qOwn,
Short "bp"
"find the package(s) owning a file with regexp"
qOwnRegex,
Long "s" "size"
"find the size of files in a package"
qSize,
Long "k" "check"
"check MD5sums and timestamps of a package"
qCheck
]
dependCommands :: [Command]
dependCommands = [
Long "d" "depends"
"list packages directly depending on this package"
qDepend,
Short "dd"
"list direct dependencies of a package"
qWant
]
useCommands :: [Command]
useCommands = [
Long "u" "uses"
"describe a package's USE flags"
qUse,
Long "h" "hasuse"
"list all packages with a USE flag"
qHasUse
]
allCommands :: [Command]
allCommands = logCommands ++ listCommands ++ ownCommands ++ dependCommands ++ useCommands
----------------------------------------------------------------
main :: IO ()
main = do
args0 <- getArgs
let (options, commands) = span isOption args0
mapM_ parseOptions options
case commands of
[] -> usage
cmd:cargs -> runCommand cmd allCommands cargs
isOption :: String -> Bool
isOption = ('-' ==) . head
----------------------------------------------------------------
parseOptions :: String -> IO ()
parseOptions [] = return ()
parseOptions "-C" = setColourEnabled False
parseOptions "--nocolor" = setColourEnabled False
parseOptions "--nocolour" = setColourEnabled False
parseOptions _ = return ()
----------------------------------------------------------------
runCommand :: String -> [Command] -> CommandProc
runCommand _ [] = \ _ -> usage
runCommand command (Short cmd _ f:cs)
| command == cmd = f
| otherwise = runCommand command cs
runCommand command (Long cmd0 cmd1 _ f:cs)
| command == cmd0 = f
| command == cmd1 = f
| otherwise = runCommand command cs
----------------------------------------------------------------
usage :: IO ()
usage = do
prog <- getProgName
putStrLn "fquery 0.2\n"
putStrLn $ "Usage: " ++ prog ++ " [options] <command> <arguments>\n"
cyan >> putStr "Options:" >> off2
inYellow (putStr " -C --nocolour") >> tab >> putStrLn "turn off colours"
nl
cyan >> putStr "Commands for Installed Packages:" >> off2
mapM_ putCommand logCommands; nl
mapM_ putCommand listCommands; nl
mapM_ putCommand ownCommands; nl
mapM_ putCommand dependCommands; nl
mapM_ putCommand useCommands; nl
putCommand :: Command -> IO ()
putCommand (Short cmd desc _) = f `withDesc` desc
where f = green >> putStr cmd >> off >> tab
putCommand (Long cmd0 cmd1 desc _) = f `withDesc` desc
where f = green >> putStr (cmd0 ++ " " ++ cmd1) >> off
withDesc :: IO () -> String -> IO ()
f `withDesc` desc = putStr " " >> f >> tab >> putStrLn desc
tab :: IO ()
tab = putChar '\t'
nl :: IO ()
nl = putChar '\n'