summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-22 06:42:02 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-22 06:42:02 +0100
commitd3de599ac01b133ce604aab7bd08fe60c484fac0 (patch)
tree41e88a34ab0e2417cc1a81054e8b08a67bff05f4
parent0b4c2d4fd9e060c8d0a307ca093b2b1b9ca7a865 (diff)
feat: add `view` command
-rw-r--r--app/Main.hs53
1 files 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