diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2017-02-26 19:59:17 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2017-02-26 19:59:17 -0700 |
commit | 5bb55ee72688f46ca7b92a8f59d121e2ec1622ba (patch) | |
tree | 2a857b7f32d5afe5b72285d9404422459ed504fe | |
parent | 4f6d803ad866dd2d201ac7098aa7cd51ad66c981 (diff) | |
download | sscan-5bb55ee72688f46ca7b92a8f59d121e2ec1622ba.tar.gz |
add ScanSess type & refactor
-rw-r--r-- | Main.hs | 55 | ||||
-rw-r--r-- | Types/State.hs | 37 |
2 files changed, 44 insertions, 48 deletions
@@ -40,9 +40,10 @@ drawUI st = [ui] , hBorderWithLabel (str "[ Actions ]") , vLimit 6 $ C.center $ actionsBox ] - status = str $ case st^.stPageCount of - Just n -> "Scanned " ++ show n ++ " pages" - Nothing -> "Ready to scan first page" + status = str $ maybe + "Ready to scan first page" + (\(ScanSess _ p) -> "Scanned " ++ show p ++ " pages") + (st^.stScanSess) settingsBox = defnList AlignRight Nothing [ ("run OCRmyPDF", if st^.stOCR then "yes" else "no") , ("colour data", show $ st^.stColour) @@ -101,29 +102,23 @@ beginScanSess :: St -> IO St beginScanSess st = do temp <- getTemporaryDirectory >>= \tmpdir -> createTempDirectory tmpdir "sscan" - return $ st - & stScanningSession .~ (Just temp) - & stPageCount .~ (Just 0) + return $ st & stScanSess .~ (Just $ ScanSess temp 0) abortScanSess :: St -> IO St abortScanSess st = do maybe (return ()) - removeDirectoryRecursive - (st^.stScanningSession) - return $ st - & stScanningSession .~ Nothing - & stPageCount .~ Nothing + (\(ScanSess d _) -> removeDirectoryRecursive d) + (st^.stScanSess) + return $ st & stScanSess .~ Nothing finishScanSess :: St -> IO St finishScanSess st = do void $ forkFinally (finishScanSess' st) $ \result -> case result of Right _ -> maybe (return ()) - removeDirectoryRecursive - (st^.stScanningSession) - return $ st - & stScanningSession .~ Nothing - & stPageCount .~ Nothing + (\(ScanSess d _) -> removeDirectoryRecursive d) + (st^.stScanSess) + return $ st & stScanSess .~ Nothing -- run OCRmyPDF, pdftk etc., and if any process existed non-zero, -- record to a log file in the outdir @@ -139,20 +134,17 @@ scanNextPage st = undefined handleHotKey :: St -> Char -> EventM () (Next St) handleHotKey st 'q' = handleQ st handleHotKey st ' ' = handleSPC st -handleHotKey st 'o' = updateStateOutsideSession st +handleHotKey st 'o' = continue $ updateSt st (\s -> s & stOCR .~ (not $ st^.stOCR)) -handleHotKey st 'c' = updateStateOutsideSession st +handleHotKey st 'c' = continue $ updateSt st (\s -> s & stColour .~ (cycleColour $ st^.stColour)) -handleHotKey st 'p' = updateStateOutsideSession st +handleHotKey st 'p' = continue $ updateSt st (\s -> s & stPaper .~ (cyclePaper $ st^.stPaper)) -handleHotKey st c = updateStateOutsideSession st $ +handleHotKey st c = continue $ updateSt st $ case lookupPreset c of Just (Preset _ _ f) -> f _ -> id -updateStateOutsideSession :: St -> (St -> St) -> EventM () (Next St) -updateStateOutsideSession st f = continue $ ifScanSess st st (f st) - appEvent :: St -> BrickEvent () e -> EventM () (Next St) appEvent st (VtyEvent e) = case e of @@ -176,14 +168,13 @@ main = do papersize <- init <$> readFile "/etc/papersize" let paper = if papersize == "letter" then Letter else A4 initialState = St - { _stScanningSession = Nothing - , _stPageCount = Nothing - , _stOCR = True - , _stColour = Greyscale - , _stPaper = paper - , _stDefaultPaper = paper - , _stDPI = 300 - , _stOutFormat = PDF - , _stOutdir = home </> "tmp" + { _stScanSess = Nothing + , _stOCR = True + , _stColour = Greyscale + , _stPaper = paper + , _stDefaultPaper = paper + , _stDPI = 300 + , _stOutFormat = PDF + , _stOutdir = home </> "tmp" } void $ defaultMain theApp initialState diff --git a/Types/State.hs b/Types/State.hs index e37d7a8..68ef188 100644 --- a/Types/State.hs +++ b/Types/State.hs @@ -33,26 +33,31 @@ type DPI = Int data OutputFormat = PDF | PNG deriving (Eq, Show) +-- | An active multi-page scanning session +data ScanSess = ScanSess + FilePath -- ^ session's tmpdir + Int -- ^ total pages scanner thus far + -- | Application state data St = - St { _stScanningSession :: Maybe FilePath -- ^ if a session is in - -- progress, accmulate - -- scans in this dir - , _stPageCount :: Maybe Int -- ^ if a session is in - -- progress, the number of - -- pages scanned thus far - , _stOCR :: Bool -- ^ whether to use OCRmyPDF - , _stColour :: Colour - , _stPaper :: Paper -- ^ currently selected paper size - , _stDefaultPaper :: Paper -- ^ locale's default paper size - , _stDPI :: DPI - , _stOutFormat :: OutputFormat - , _stOutdir :: FilePath -- ^ where to save final PDFs + St { _stScanSess :: Maybe ScanSess + , _stOCR :: Bool -- ^ whether to use OCRmyPDF + , _stColour :: Colour + , _stPaper :: Paper -- ^ currently selected paper size + , _stDefaultPaper :: Paper -- ^ locale's default paper size + , _stDPI :: DPI + , _stOutFormat :: OutputFormat + , _stOutdir :: FilePath -- ^ where to save final PDFs } - --- other device-specific scanimage options we could support: --swdespeck; --color-filter; --depth +-- other device-specific scanimage options the old script supported: --swdespeck; --color-filter; --depth makeLenses ''St ifScanSess :: St -> a -> a -> a -ifScanSess st a b = if isJust $ st^.stScanningSession then a else b +ifScanSess st a b = if isJust $ st^.stScanSess then a else b + +-- | Update a state when there is no scanning session in progress (the +-- state should not be changed when some pages have already been +-- scanned) +updateSt :: St -> (St -> St) -> St +updateSt st f = ifScanSess st st (f st) |