From d3de599ac01b133ce604aab7bd08fe60c484fac0 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Fri, 22 Dec 2023 06:42:02 +0100 Subject: feat: add `view` command --- app/Main.hs | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index e9189d4..8ddf84e 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -56,6 +56,9 @@ data Cmd todo :: Bool } | Todo + | View + { indexNames :: [FilePath] + } args :: O.Parser Args args = @@ -69,7 +72,9 @@ cmd = O.command "list" . O.info listCmd $ O.progDesc "List document(s)", O.command "todo" . O.info todoCmd $ - O.progDesc "Interactively process new documents" + O.progDesc "Interactively process new documents", + O.command "view" . O.info viewCmd $ + O.progDesc "View document(s)" ] consumeCmd :: O.Parser Cmd @@ -88,9 +93,19 @@ todoCmd :: O.Parser Cmd todoCmd = pure Todo +viewCmd :: O.Parser Cmd +viewCmd = + View + <$> indexNamesArg + inputsArg :: O.Parser [FilePath] inputsArg = - O.many (O.strArgument (O.metavar "FILE" <> O.action "file")) + O.many + ( O.strArgument + ( O.metavar "FILE" + <> O.action "file" + ) + ) keepArg :: O.Parser Bool keepArg = @@ -112,6 +127,14 @@ filtersArg = parse ('@' : tagKey) = Just (FilterByTag (T.pack tagKey)) parse _ = Nothing +indexNamesArg :: O.Parser [FilePath] +indexNamesArg = + O.many $ + O.strArgument + ( O.metavar "ID" + <> O.action "file" + ) + todoArg :: O.Parser Bool todoArg = O.switch @@ -151,6 +174,9 @@ main = do processDocuments . applyFilters [FilterByTag "todo"] =<< getDocuments + Args {cmd = View {indexNames}} -> do + viewDocuments + =<< mapM (readDocument . (<.> "json")) indexNames data Document = Document { iFileName :: String, @@ -166,13 +192,14 @@ instance HasField "iFilePath" Document FilePath where getDocuments :: IO [Document] getDocuments = - parMapM - ( \iFileName -> - Document iFileName - <$> decodeFile @Index ("index" iFileName) - ) + parMapM readDocument =<< sort <$> listDirectory "index" +readDocument :: FilePath -> IO Document +readDocument iFileName = + Document iFileName + <$> decodeFile @Index ("index" iFileName) + applyFilters :: [Filter] -> [Document] -> [Document] applyFilters filters = filter (pred filters) `at` (.index) where @@ -220,9 +247,19 @@ processDocuments docs = "p" -> processDocument doc "s" -> pure () "v" -> do - sh_ (printf "zathura '%s'" doc.oFilePath) + viewDocuments [doc] processDocuments' n doc +viewDocuments :: [Document] -> IO () +viewDocuments docs = + sh_ + ( "zathura " + <> ( intercalate + " " + (map (\doc -> printf "'%s'" doc.oFilePath) docs) + ) + ) + processDocument :: Document -> IO () processDocument (Document {iFileName, index}) = do printf "%s\n" index.originalText -- cgit v1.2.3