diff options
-rw-r--r-- | app/Main.hs | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/app/Main.hs b/app/Main.hs index 05b066e..d0df4d1 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -54,7 +54,8 @@ data Cmd | List { filters :: [Filter], todo :: Bool, - view :: Bool + view :: Bool, + redo :: Bool } | Todo | View @@ -90,6 +91,7 @@ listCmd = <$> filtersArg <*> todoArg <*> viewArg + <*> redoArg todoCmd :: O.Parser Cmd todoCmd = @@ -144,6 +146,13 @@ todoArg = <> O.help "Run command `todo` on listed document(s)" ) +redoArg :: O.Parser Bool +redoArg = + O.switch + ( O.long "redo" + <> O.help "Redo OCR on listed document(s)" + ) + viewArg :: O.Parser Bool viewArg = O.switch @@ -166,7 +175,8 @@ main = do Args {cmd = Consume {keep, inputs}} -> mapM_ putStrLn =<< parMapM (consume1 keep) (map (cwd </>) inputs) - Args {cmd = List {filters, todo = False, view = False}} -> do + Args {cmd = List {filters, redo, todo = False, view = False}} -> do + doRedoIf filters redo mapM_ ( \(Document {iFileName, index}) -> do if hasTag (Tag "todo" Nothing) index @@ -175,7 +185,8 @@ main = do ) . applyFilters filters =<< getDocuments - Args {cmd = List {filters, todo = True}} -> do + Args {cmd = List {filters, redo, todo = True}} -> do + doRedoIf filters redo processDocuments . applyFilters filters =<< getDocuments @@ -183,7 +194,8 @@ main = do processDocuments . applyFilters [FilterByTag "todo"] =<< getDocuments - Args {cmd = List {filters, view = True}} -> do + Args {cmd = List {filters, redo, view = True}} -> do + doRedoIf filters redo viewDocuments . applyFilters filters =<< getDocuments @@ -191,6 +203,19 @@ main = do viewDocuments =<< mapM (readDocument . (<.> "json")) indexNames +doRedoIf :: [Filter] -> Bool -> IO () +doRedoIf filters redo = + when redo do + parMapM_ doRedo + . applyFilters filters + =<< getDocuments + where + doRedo doc = do + originalText <- ocr doc.oFilePath + withGit do + J.encodeFile doc.iFilePath doc.index {originalText = originalText} + commitAll [doc.iFilePath] (printf "redo %s" (takeBaseName doc.iFilePath)) + data Document = Document { iFileName :: String, index :: Index @@ -415,7 +440,7 @@ withGit = withLockFile def ".gitlock" commitAll :: [FilePath] -> String -> IO () commitAll fps m = do sh_ ("git add -- " ++ intercalate " " (map (printf "'%s'") fps)) - sh_ (printf "git commit -m '%s'" m) + sh_ (printf "git commit -m '%s' || :" m) data DecodeException = DecodeException FilePath String deriving (Show) @@ -597,6 +622,7 @@ parsePdfImages out' = sh :: String -> IO LB.ByteString sh cmd = do + -- printf "+ %s\n" cmd (exitCode, out, err) <- readProcess (fromString cmd) case exitCode of ExitSuccess -> return out |