diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-08-03 14:47:50 +0200 | 
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-08-03 14:47:50 +0200 | 
| commit | cbe2b674b3a9297321dfb62cf294bb0f270ea731 (patch) | |
| tree | 4fb57a5f438a951eb1bdc6652ca75e79a18a9f94 /tprint/src | |
| parent | ef16292b588d312601af350254bae73c5278dc8a (diff) | |
| download | thermoprint-cbe2b674b3a9297321dfb62cf294bb0f270ea731.tar thermoprint-cbe2b674b3a9297321dfb62cf294bb0f270ea731.tar.gz thermoprint-cbe2b674b3a9297321dfb62cf294bb0f270ea731.tar.bz2 thermoprint-cbe2b674b3a9297321dfb62cf294bb0f270ea731.tar.xz thermoprint-cbe2b674b3a9297321dfb62cf294bb0f270ea731.zip | |
cleanup
Diffstat (limited to 'tprint/src')
| -rw-r--r-- | tprint/src/Main.hs | 42 | ||||
| -rw-r--r-- | tprint/src/Options.hs | 10 | 
2 files changed, 28 insertions, 24 deletions
| diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs index f6008ac..aed0388 100644 --- a/tprint/src/Main.hs +++ b/tprint/src/Main.hs | |||
| @@ -140,25 +140,9 @@ tprint TPrint{ operation = DraftCreate{..}, ..} Client{..} out = withPrintout in | |||
| 140 | tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do | 140 | tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do | 
| 141 | unless dryRun $ draftReplace draftId draftTitle p | 141 | unless dryRun $ draftReplace draftId draftTitle p | 
| 142 | 142 | ||
| 143 | tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} _ = do | 143 | tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} out = do | 
| 144 | d@(fromMaybe T.empty -> dTitle, dContent) <- draft draftId | 144 | (dTitle, dContent) <- editDraft (fst output) =<< maybe (return mempty) draft mDraftId | 
| 145 | let | 145 | unless dryRun $ maybe (\t c -> hPutStrLn out . show =<< draftCreate t c) draftReplace mDraftId dTitle dContent | 
| 146 | content | ||
| 147 | | (JSON, _) <- output = return . LBS.toStrict $ encodePretty d | ||
| 148 | | otherwise = do | ||
| 149 | c <- either throwM return (cobbcode dContent) | ||
| 150 | return . T.encodeUtf8 $ dTitle <> T.pack "\n" <> c | ||
| 151 | tmpl | ||
| 152 | | (JSON, _) <- output = jsonTemplate | ||
| 153 | | otherwise = plainTemplate | ||
| 154 | parse | ||
| 155 | | (JSON, _) <- output = either (throwM . userError) return . eitherDecodeStrict' | ||
| 156 | | otherwise = (\(t:(T.unlines -> c)) -> (massage t, ) <$> either throwM return (bbcode c)) . T.lines . T.decodeUtf8 | ||
| 157 | massage (T.strip -> t) | ||
| 158 | | T.null t = Nothing | ||
| 159 | | otherwise = Just t | ||
| 160 | (dTitle', dContent') <- parse =<< runUserEditorDWIM tmpl =<< content | ||
| 161 | unless dryRun $ draftReplace draftId dTitle' dContent' | ||
| 162 | 146 | ||
| 163 | tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId | 147 | tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId | 
| 164 | 148 | ||
| @@ -170,3 +154,23 @@ tprint TPrint{ operation = DraftPrint{..}, ..} client@Client{..} out = do | |||
| 170 | 154 | ||
| 171 | 155 | ||
| 172 | tprint _ _ _ = undefined | 156 | tprint _ _ _ = undefined | 
| 157 | |||
| 158 | |||
| 159 | editDraft :: Format -> (Maybe DraftTitle, Printout) -> IO (Maybe DraftTitle, Printout) | ||
| 160 | editDraft fmt d@(fromMaybe T.empty -> dTitle, dContent) = do | ||
| 161 | let | ||
| 162 | content | ||
| 163 | | JSON <- fmt = return . LBS.toStrict $ encodePretty d | ||
| 164 | | otherwise = do | ||
| 165 | c <- either throwM return (cobbcode dContent) | ||
| 166 | return . T.encodeUtf8 $ dTitle <> T.pack "\n--- ^ Title ---\n" <> c | ||
| 167 | tmpl | ||
| 168 | | JSON <- fmt = jsonTemplate | ||
| 169 | | otherwise = plainTemplate | ||
| 170 | parse | ||
| 171 | | JSON <- fmt = either (throwM . userError) return . eitherDecodeStrict' | ||
| 172 | | otherwise = (\(t:_:(T.unlines -> c)) -> (massage t, ) <$> either throwM return (bbcode c)) . T.lines . T.decodeUtf8 | ||
| 173 | massage (T.strip -> t) | ||
| 174 | | T.null t = Nothing | ||
| 175 | | otherwise = Just t | ||
| 176 | parse =<< runUserEditorDWIM tmpl =<< content | ||
| diff --git a/tprint/src/Options.hs b/tprint/src/Options.hs index f4c7ebd..046112f 100644 --- a/tprint/src/Options.hs +++ b/tprint/src/Options.hs | |||
| @@ -69,7 +69,7 @@ data Operation | |||
| 69 | , draftTitle :: Maybe DraftTitle | 69 | , draftTitle :: Maybe DraftTitle | 
| 70 | , input :: Input | 70 | , input :: Input | 
| 71 | } | 71 | } | 
| 72 | | DraftEdit { draftId :: DraftId } | 72 | | DraftEdit { mDraftId :: Maybe DraftId } | 
| 73 | | Draft { draftId :: DraftId } | 73 | | Draft { draftId :: DraftId } | 
| 74 | | DraftDelete { draftId :: DraftId } | 74 | | DraftDelete { draftId :: DraftId } | 
| 75 | | DraftPrint | 75 | | DraftPrint | 
| @@ -95,7 +95,7 @@ supportedInputs, supportedOutputs :: [Format] | |||
| 95 | supportedInputs = [BBCode, JSON] | 95 | supportedInputs = [BBCode, JSON] | 
| 96 | supportedOutputs = [Human, BBCode, Internal, JSON] | 96 | supportedOutputs = [Human, BBCode, Internal, JSON] | 
| 97 | 97 | ||
| 98 | cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation | 98 | cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraftEdit, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation | 
| 99 | cmdPrinters = info cmdPrinters' $ progDesc "List all available printers" | 99 | cmdPrinters = info cmdPrinters' $ progDesc "List all available printers" | 
| 100 | where cmdPrinters' = pure Printers | 100 | where cmdPrinters' = pure Printers | 
| 101 | 101 | ||
| @@ -129,8 +129,8 @@ cmdDraftCreate = info cmdDraftCreate' $ progDesc "Create a new draft" | |||
| 129 | cmdDraftReplace = info cmdDraftReplace' $ progDesc "Update the contents and title of a draft" | 129 | cmdDraftReplace = info cmdDraftReplace' $ progDesc "Update the contents and title of a draft" | 
| 130 | where cmdDraftReplace' = DraftReplace <$> aDraft <*> optional pTitle <*> pInput | 130 | where cmdDraftReplace' = DraftReplace <$> aDraft <*> optional pTitle <*> pInput | 
| 131 | 131 | ||
| 132 | cmdDraftEdit = info cmdDraftEdit' $ progDesc "Edit an existing draft" | 132 | cmdDraftEdit = info cmdDraftEdit' $ progDesc "Edit or create a draft" | 
| 133 | where cmdDraftEdit' = DraftEdit <$> aDraft | 133 | where cmdDraftEdit' = DraftEdit <$> optional aDraft | 
| 134 | 134 | ||
| 135 | cmdDraft = info cmdDraft' $ progDesc "Retrieve a drafts contents" | 135 | cmdDraft = info cmdDraft' $ progDesc "Retrieve a drafts contents" | 
| 136 | where cmdDraft' = Draft <$> aDraft | 136 | where cmdDraft' = Draft <$> aDraft | 
| @@ -153,7 +153,7 @@ pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters | |||
| 153 | ) (progDesc "Interact with jobs") | 153 | ) (progDesc "Interact with jobs") | 
| 154 | , command "draft" $ info ( hsubparser $ mconcat [ command "create" cmdDraftCreate | 154 | , command "draft" $ info ( hsubparser $ mconcat [ command "create" cmdDraftCreate | 
| 155 | , command "replace" cmdDraftReplace | 155 | , command "replace" cmdDraftReplace | 
| 156 | , command "edit" cmdDraftEdit | 156 | , command "interact" cmdDraftEdit | 
| 157 | , command "content" cmdDraft | 157 | , command "content" cmdDraft | 
| 158 | , command "delete" cmdDraftDelete | 158 | , command "delete" cmdDraftDelete | 
| 159 | , command "print" cmdDraftPrint | 159 | , command "print" cmdDraftPrint | 
