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
|
module Main where
import qualified Brick.Main as M
import Control.Exception (throwIO)
import Control.Monad (void)
import qualified Daffm
import Daffm.Configuration (loadConfigFile)
import Data.Maybe (fromMaybe)
import qualified Data.Text as Text
import System.Directory (getCurrentDirectory)
import System.Environment (getArgs)
data Args = Args
{ argsCwd :: Maybe Text.Text,
argsConfigFile :: Maybe FilePath,
argsHelp :: Bool
}
deriving (Show)
main :: IO ()
main = do
args <- getArgs >>= parseArgs
evaluate args
evaluate :: Args -> IO ()
evaluate (Args {argsHelp = True}) =
putStrLn helpMenuContents
evaluate (Args {argsCwd, argsConfigFile}) = do
cwd <- getCurrentDirectory
config <- loadConfigFile argsConfigFile
let dir = fromMaybe (Text.pack cwd) argsCwd
initialState <- Daffm.loadDirToState dir $ Daffm.mkEmptyAppState config
void $ M.defaultMain Daffm.app initialState
parseArgs :: [String] -> IO Args
parseArgs rawArgs = case parsedArgs of
Left e -> throwIO $ userError e
Right v -> pure v
where
parsedArgs = parse rawArgs (Args {argsCwd = Nothing, argsConfigFile = Nothing, argsHelp = False})
parse :: [String] -> Args -> Either String Args
parse [] args = Right args
parse ("-h" : _) args = Right $ args {argsHelp = True}
parse ("--help" : _) args = Right $ args {argsHelp = True}
parse ("-c" : config : rest) args = parse rest $ args {argsConfigFile = Just config}
parse ["-c"] _ = Left "Missing value for -c arg"
parse ("--config" : config : rest) args = parse rest $ args {argsConfigFile = Just config}
parse ["--config"] _ = Left "Missing value for --config arg"
parse (flag@('-' : _) : _) _ = Left $ "Invalid flag " <> flag
parse (dir : rest) args = parse rest $ args {argsCwd = Just $ Text.pack dir}
helpMenuContents :: String
helpMenuContents =
unlines
[ "daffm - Dumb as-fuck file manager",
"",
"Usage: daffm [options] [dir]",
"",
"Arguments:",
" [dir]",
" Directory to load. Defaults to current working directory",
"",
"Options:",
" -c, --config <CONFIG-PATH>",
" Load toml config from file",
" If path is prefixed with @, will use alternate config",
" Ex: -c @foo will load $XDG_CONFIG_HOME/daffm/config.foo.toml",
" Default: $XDG_CONFIG_HOME/daffm/config.toml",
"",
" -h, --help",
" This help menu"
]
|