From cbe2b674b3a9297321dfb62cf294bb0f270ea731 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 3 Aug 2017 14:47:50 +0200 Subject: cleanup --- tprint/src/Main.hs | 42 +++++++++++++++++++++++------------------- tprint/src/Options.hs | 10 +++++----- tprint/tprint.cabal | 2 +- tprint/tprint.nix | 2 +- 4 files changed, 30 insertions(+), 26 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 tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do unless dryRun $ draftReplace draftId draftTitle p -tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} _ = do - d@(fromMaybe T.empty -> dTitle, dContent) <- draft draftId - let - content - | (JSON, _) <- output = return . LBS.toStrict $ encodePretty d - | otherwise = do - c <- either throwM return (cobbcode dContent) - return . T.encodeUtf8 $ dTitle <> T.pack "\n" <> c - tmpl - | (JSON, _) <- output = jsonTemplate - | otherwise = plainTemplate - parse - | (JSON, _) <- output = either (throwM . userError) return . eitherDecodeStrict' - | otherwise = (\(t:(T.unlines -> c)) -> (massage t, ) <$> either throwM return (bbcode c)) . T.lines . T.decodeUtf8 - massage (T.strip -> t) - | T.null t = Nothing - | otherwise = Just t - (dTitle', dContent') <- parse =<< runUserEditorDWIM tmpl =<< content - unless dryRun $ draftReplace draftId dTitle' dContent' +tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} out = do + (dTitle, dContent) <- editDraft (fst output) =<< maybe (return mempty) draft mDraftId + unless dryRun $ maybe (\t c -> hPutStrLn out . show =<< draftCreate t c) draftReplace mDraftId dTitle dContent tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId @@ -170,3 +154,23 @@ tprint TPrint{ operation = DraftPrint{..}, ..} client@Client{..} out = do tprint _ _ _ = undefined + + +editDraft :: Format -> (Maybe DraftTitle, Printout) -> IO (Maybe DraftTitle, Printout) +editDraft fmt d@(fromMaybe T.empty -> dTitle, dContent) = do + let + content + | JSON <- fmt = return . LBS.toStrict $ encodePretty d + | otherwise = do + c <- either throwM return (cobbcode dContent) + return . T.encodeUtf8 $ dTitle <> T.pack "\n--- ^ Title ---\n" <> c + tmpl + | JSON <- fmt = jsonTemplate + | otherwise = plainTemplate + parse + | JSON <- fmt = either (throwM . userError) return . eitherDecodeStrict' + | otherwise = (\(t:_:(T.unlines -> c)) -> (massage t, ) <$> either throwM return (bbcode c)) . T.lines . T.decodeUtf8 + massage (T.strip -> t) + | T.null t = Nothing + | otherwise = Just t + 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 , draftTitle :: Maybe DraftTitle , input :: Input } - | DraftEdit { draftId :: DraftId } + | DraftEdit { mDraftId :: Maybe DraftId } | Draft { draftId :: DraftId } | DraftDelete { draftId :: DraftId } | DraftPrint @@ -95,7 +95,7 @@ supportedInputs, supportedOutputs :: [Format] supportedInputs = [BBCode, JSON] supportedOutputs = [Human, BBCode, Internal, JSON] -cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation +cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraftEdit, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation cmdPrinters = info cmdPrinters' $ progDesc "List all available printers" where cmdPrinters' = pure Printers @@ -129,8 +129,8 @@ cmdDraftCreate = info cmdDraftCreate' $ progDesc "Create a new draft" cmdDraftReplace = info cmdDraftReplace' $ progDesc "Update the contents and title of a draft" where cmdDraftReplace' = DraftReplace <$> aDraft <*> optional pTitle <*> pInput -cmdDraftEdit = info cmdDraftEdit' $ progDesc "Edit an existing draft" - where cmdDraftEdit' = DraftEdit <$> aDraft +cmdDraftEdit = info cmdDraftEdit' $ progDesc "Edit or create a draft" + where cmdDraftEdit' = DraftEdit <$> optional aDraft cmdDraft = info cmdDraft' $ progDesc "Retrieve a drafts contents" where cmdDraft' = Draft <$> aDraft @@ -153,7 +153,7 @@ pOperation = hsubparser $ mconcat [ command "printers" cmdPrinters ) (progDesc "Interact with jobs") , command "draft" $ info ( hsubparser $ mconcat [ command "create" cmdDraftCreate , command "replace" cmdDraftReplace - , command "edit" cmdDraftEdit + , command "interact" cmdDraftEdit , command "content" cmdDraft , command "delete" cmdDraftDelete , command "print" cmdDraftPrint diff --git a/tprint/tprint.cabal b/tprint/tprint.cabal index bbc5207..bb0aad9 100644 --- a/tprint/tprint.cabal +++ b/tprint/tprint.cabal @@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: tprint -version: 3.1.0 +version: 3.2.0 synopsis: A CLI for thermoprint-client -- description: homepage: http://dirty-haskell.org/tags/thermoprint.html diff --git a/tprint/tprint.nix b/tprint/tprint.nix index 310fe03..57ba4f4 100644 --- a/tprint/tprint.nix +++ b/tprint/tprint.nix @@ -5,7 +5,7 @@ }: mkDerivation { pname = "tprint"; - version = "3.1.0"; + version = "3.2.0"; src = ./.; isLibrary = false; isExecutable = true; -- cgit v1.2.3