diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-10-05 18:49:36 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-10-05 18:49:36 +0530 |
| commit | 577a30cd5894aa16cf81606767d4d07c3e7b1603 (patch) | |
| tree | a3c50dade9399660ddba1008375f833862cf8839 /exe/Main.hs | |
| parent | 845b96c9a7f6d80f2042ef5d334fd6842d617f27 (diff) | |
| download | daffm-577a30cd5894aa16cf81606767d4d07c3e7b1603.tar.gz daffm-577a30cd5894aa16cf81606767d4d07c3e7b1603.zip | |
Add arg parsing, loading config from XDG_CONFIG_HOME and -c arg for loading config file
Diffstat (limited to 'exe/Main.hs')
| -rw-r--r-- | exe/Main.hs | 61 |
1 files changed, 59 insertions, 2 deletions
diff --git a/exe/Main.hs b/exe/Main.hs index 3ac3375..2bfe08b 100644 --- a/exe/Main.hs +++ b/exe/Main.hs @@ -1,15 +1,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 - initialState <- Daffm.loadDirToState (Text.pack cwd) $ Daffm.mkEmptyAppState config + 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" + ] |
