aboutsummaryrefslogtreecommitdiff
path: root/tprint/src
diff options
context:
space:
mode:
Diffstat (limited to 'tprint/src')
-rw-r--r--tprint/src/Main.hs42
-rw-r--r--tprint/src/Options.hs10
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
140tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do 140tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do
141 unless dryRun $ draftReplace draftId draftTitle p 141 unless dryRun $ draftReplace draftId draftTitle p
142 142
143tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} _ = do 143tprint 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
163tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId 147tprint 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
172tprint _ _ _ = undefined 156tprint _ _ _ = undefined
157
158
159editDraft :: Format -> (Maybe DraftTitle, Printout) -> IO (Maybe DraftTitle, Printout)
160editDraft 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]
95supportedInputs = [BBCode, JSON] 95supportedInputs = [BBCode, JSON]
96supportedOutputs = [Human, BBCode, Internal, JSON] 96supportedOutputs = [Human, BBCode, Internal, JSON]
97 97
98cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation 98cmdPrinters, cmdJobs, cmdJobCreate, cmdJob, cmdJobStatus, cmdJobDelete, cmdDrafts, cmdDraftCreate, cmdDraftReplace, cmdDraftEdit, cmdDraft, cmdDraftDelete, cmdDraftPrint :: ParserInfo Operation
99cmdPrinters = info cmdPrinters' $ progDesc "List all available printers" 99cmdPrinters = 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"
129cmdDraftReplace = info cmdDraftReplace' $ progDesc "Update the contents and title of a draft" 129cmdDraftReplace = 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
132cmdDraftEdit = info cmdDraftEdit' $ progDesc "Edit an existing draft" 132cmdDraftEdit = info cmdDraftEdit' $ progDesc "Edit or create a draft"
133 where cmdDraftEdit' = DraftEdit <$> aDraft 133 where cmdDraftEdit' = DraftEdit <$> optional aDraft
134 134
135cmdDraft = info cmdDraft' $ progDesc "Retrieve a drafts contents" 135cmdDraft = 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