diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Daffm.hs | 26 | ||||
| -rw-r--r-- | lib/Daffm/Args.hs | 44 | ||||
| -rw-r--r-- | lib/Daffm/Types.hs | 7 |
3 files changed, 67 insertions, 10 deletions
diff --git a/lib/Daffm.hs b/lib/Daffm.hs index afdba11..a4ced9f 100644 --- a/lib/Daffm.hs +++ b/lib/Daffm.hs @@ -1,18 +1,24 @@ -module Daffm (app, loadDirToState, mkEmptyAppState) where +module Daffm (initApp, loadDirToState, mkEmptyAppState) where -import qualified Brick.Main as M +import qualified Brick.Main as B +import Control.Monad (void) import Daffm.Attrs (appAttrMap) import Daffm.Event (appEvent) import Daffm.State (loadDirToState, mkEmptyAppState) -import Daffm.Types (AppState (..), FocusTarget) +import Daffm.Types (AppState (..), Configuration, FilePathText, FocusTarget) import Daffm.View (appView) -app :: M.App AppState e FocusTarget +app :: B.App AppState e FocusTarget app = - M.App - { M.appDraw = appView, - M.appChooseCursor = M.showFirstCursor, - M.appHandleEvent = appEvent, - M.appStartEvent = pure (), - M.appAttrMap = const appAttrMap + B.App + { B.appDraw = appView, + B.appChooseCursor = B.showFirstCursor, + B.appHandleEvent = appEvent, + B.appStartEvent = pure (), + B.appAttrMap = const appAttrMap } + +initApp :: FilePathText -> Configuration -> IO () +initApp dir config = do + initialState <- loadDirToState dir $ mkEmptyAppState config + void $ B.defaultMain app initialState diff --git a/lib/Daffm/Args.hs b/lib/Daffm/Args.hs new file mode 100644 index 0000000..aeaead7 --- /dev/null +++ b/lib/Daffm/Args.hs @@ -0,0 +1,44 @@ +module Daffm.Args where + +import Control.Exception (throwIO) +import Daffm.Types +import qualified Data.Text as Text + +parseArgs :: [String] -> IO Args +parseArgs rawArgs = case parsedArgs of + Left e -> throwIO $ userError e + Right v -> pure v + where + parsedArgs = parse rawArgs (Args {argsDirOrFile = 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"] _ = Left "Missing value for -c arg" + parse ("-c" : config : rest) args = parse rest $ args {argsConfigFile = Just config} + parse ["--config"] _ = Left "Missing value for --config arg" + parse ("--config" : config : rest) args = parse rest $ args {argsConfigFile = Just config} + parse (flag@('-' : _) : _) _ = Left $ "Invalid flag " <> flag + parse (dir : rest) args = parse rest $ args {argsDirOrFile = Just $ Text.pack dir} + +helpMenuContents :: String +helpMenuContents = + unlines + [ "daffm - Dumb as-fuck file manager", + "", + "Usage: daffm [options] [dir]", + "", + "Arguments:", + " [dir]", + " Directory or file path 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" + ] diff --git a/lib/Daffm/Types.hs b/lib/Daffm/Types.hs index b916c79..3fd7086 100644 --- a/lib/Daffm/Types.hs +++ b/lib/Daffm/Types.hs @@ -103,6 +103,13 @@ instance Semigroup Configuration where configTheme = configTheme a <> configTheme b } +data Args = Args + { argsDirOrFile :: Maybe Text.Text, + argsConfigFile :: Maybe FilePath, + argsHelp :: Bool + } + deriving (Show) + defaultConfiguration :: Configuration defaultConfiguration = Configuration |
