From 0f4a73c745602fc2a3f837e7c6e829eff44de68a Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 28 Feb 2017 21:53:04 -0700 Subject: put command into scansess & rework --- Main.hs | 35 ++++++++++++++++++++++++++--------- Types/State.hs | 49 ++++++++++++++++++++++++++++++++++++++----------- UI.hs | 2 +- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/Main.hs b/Main.hs index 8838823..d7ac33a 100644 --- a/Main.hs +++ b/Main.hs @@ -23,18 +23,18 @@ along with sscan. If not, see . {-# LANGUAGE OverloadedStrings #-} -import Control.Concurrent (forkFinally) +import Control.Concurrent (forkIO) import Control.Monad (void) import Lens.Micro ((&), (.~), (^.)) import System.Directory (getHomeDirectory, removeDirectoryRecursive) import System.FilePath (()) -import System.IO.Temp (createTempDirectory) +import System.IO.Temp (withSystemTempDirectory) import Types.State import UI --- processScanSessDir :: ScanSess -> IO () --- processScanSessDir ss@(ScanSess d n) = +processScanSessDir :: St -> IO () +processScanSessDir = undefined -- -- rather than do any error handling here, we write a logfile to -- -- the outdir for user inspection -- void $ forkFinally process $ \_ -> nukeScanSessDir ss @@ -57,14 +57,31 @@ makeInitialState = do , _stDPI = 300 , _stOutFormat = PDF , _stOutdir = home "tmp" - , _stCommand = Quit } -uiLoop :: St -> IO () -uiLoop st = runTheApp st >>= \newSt -> do - undefined +scanPage :: FilePath -> IO () +scanPage dir = undefined -main = makeInitialState >>= uiLoop +processCommand :: St -> IO () +processCommand st = case st^.stScanSess of + Nothing -> return () -- quit sscan + Just (ScanSess command pages maybeDir) -> case maybeDir of + Nothing -> withSystemTempDirectory "sscan" $ \dir -> + processCommand (setScanSessDir dir st) + Just dir -> case command of + Abort -> newSession + NextPage -> scanPage dir >> presentUI (incrementPages st) + FinalPage -> scanPage dir + >> finaliseSession (incrementPages st) >> newSession + Finalise -> finaliseSession st >> newSession + where + newSession = presentUI $ resetScanSess st + finaliseSession = forkIO . processScanSessDir + +presentUI :: St -> IO () +presentUI st = runTheApp st >>= processCommand + +main = makeInitialState >>= presentUI -- TODO scanning should happen in main. We use withTempDir to setup a -- session, and then fire up brick again. add additional state diff --git a/Types/State.hs b/Types/State.hs index 1230e31..9d11019 100644 --- a/Types/State.hs +++ b/Types/State.hs @@ -56,22 +56,37 @@ type DPI = Int data OutputFormat = PDF | PNG deriving (Eq, Show) --- | An active multi-page scanning session +-- | An active scanning session data ScanSess = ScanSess - FilePath -- ^ session's tmpdir - Int -- ^ total pages scanner thus far + -- | An action requested by the user, that has not yet been + -- fulfilled + Command + -- | The number of pages scanned so far in this scanning session + Int + -- | Temporary directory containing pages scanned in this scanning + -- session, if any have been scanned, and the session is multi-page + (Maybe FilePath) -- | An sscan operation selected by the user in the UI data Command - = NextPage -- ^ scan first page or another page - | FinalPage -- ^ scan final page (might be the first page) - | Finalise -- ^ make the previous scanned page the final page of the document - | Abort -- ^ abandon the current scan session - | Quit -- ^ quit sscan + -- | Scan an additional page, or the first page, of a multi-page + -- scanning session. + = NextPage + -- | Scan the final page of a multi-page scanning session, or the + -- first page of a single-page scanning session + | FinalPage + -- | End a multi-page scanning session without scanning anymore + -- pages, i.e., consider the most recently scanned page the final + -- page + | Finalise + -- | Abandon the current scanning session. Delete its tmpdir. + | Abort -- | Application state data St = - St { _stScanSess :: Maybe ScanSess + -- + St { _stScanSess :: Maybe ScanSess -- ^ Nothing indicates user + -- wants to quit sscan , _stOCR :: Bool -- ^ whether to use OCRmyPDF , _stColour :: Colour , _stPaper :: Paper -- ^ currently selected paper size @@ -79,9 +94,9 @@ data St = , _stDPI :: DPI , _stOutFormat :: OutputFormat , _stOutdir :: FilePath -- ^ where to save final PDFs - , _stCommand :: Command } --- other device-specific scanimage options the old script supported: --swdespeck; --color-filter; --depth +-- other device-specific scanimage options the old script supported: +-- --swdespeck; --color-filter; --depth makeLenses ''St @@ -96,3 +111,15 @@ updateSt st f = ifScanSess st st (f st) resetScanSess :: St -> St resetScanSess st = st & stScanSess .~ Nothing + +setScanSessDir :: FilePath -> St -> St +setScanSessDir dir st = case st^.stScanSess of + Nothing -> st + Just (ScanSess c p _) -> + st & stScanSess .~ (Just $ ScanSess c p (Just dir)) + +incrementPages :: St -> St +incrementPages st = case st^.stScanSess of + Nothing -> st + Just (ScanSess c p d) -> + st & stScanSess .~ (Just $ ScanSess c (p+1) d) diff --git a/UI.hs b/UI.hs index be75757..c10e2cf 100644 --- a/UI.hs +++ b/UI.hs @@ -66,7 +66,7 @@ drawUI st = [ui] ] status = str $ maybe "Ready to scan first page" - (\(ScanSess _ p) -> "Scanned " ++ show p ++ " pages") + (\(ScanSess _ p _) -> "Scanned " ++ show p ++ " pages") (st^.stScanSess) settingsBox = defnList AlignRight Nothing [ ("run OCRmyPDF", if st^.stOCR then "yes" else "no") -- cgit v1.2.3