diff options
Diffstat (limited to 'tprint/src/Main.hs')
-rw-r--r-- | tprint/src/Main.hs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/tprint/src/Main.hs b/tprint/src/Main.hs index bc5ee2f..f6008ac 100644 --- a/tprint/src/Main.hs +++ b/tprint/src/Main.hs | |||
@@ -1,5 +1,6 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | 1 | {-# LANGUAGE RecordWildCards #-} |
2 | {-# LANGUAGE ViewPatterns #-} | 2 | {-# LANGUAGE ViewPatterns #-} |
3 | {-# LANGUAGE TupleSections #-} | ||
3 | 4 | ||
4 | import Data.Map (Map) | 5 | import Data.Map (Map) |
5 | import qualified Data.Map as Map | 6 | import qualified Data.Map as Map |
@@ -8,7 +9,9 @@ import qualified Data.Sequence as Seq | |||
8 | import Data.Text (Text) | 9 | import Data.Text (Text) |
9 | import qualified Data.Text as T | 10 | import qualified Data.Text as T |
10 | import qualified Data.Text.IO as T | 11 | import qualified Data.Text.IO as T |
12 | import qualified Data.Text.Encoding as T | ||
11 | import qualified Data.ByteString.Lazy.Char8 as Lazy (ByteString) | 13 | import qualified Data.ByteString.Lazy.Char8 as Lazy (ByteString) |
14 | import qualified Data.ByteString.Lazy as LBS | ||
12 | import qualified Data.ByteString.Lazy.Char8 as LCBS | 15 | import qualified Data.ByteString.Lazy.Char8 as LCBS |
13 | import Data.Time | 16 | import Data.Time |
14 | 17 | ||
@@ -26,7 +29,9 @@ import Control.Concurrent (threadDelay) | |||
26 | 29 | ||
27 | import Text.Show.Pretty (dumpStr) | 30 | import Text.Show.Pretty (dumpStr) |
28 | import Data.Aeson.Encode.Pretty (encodePretty) | 31 | import Data.Aeson.Encode.Pretty (encodePretty) |
29 | import Data.Aeson (eitherDecode') | 32 | import Data.Aeson (eitherDecode', eitherDecodeStrict') |
33 | |||
34 | import Text.Editor (runUserEditorDWIM, jsonTemplate, plainTemplate) | ||
30 | 35 | ||
31 | import System.IO | 36 | import System.IO |
32 | 37 | ||
@@ -135,6 +140,26 @@ tprint TPrint{ operation = DraftCreate{..}, ..} Client{..} out = withPrintout in | |||
135 | tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do | 140 | tprint TPrint{ operation = DraftReplace{..}, ..} Client{..} _ = withPrintout input $ \p -> do |
136 | unless dryRun $ draftReplace draftId draftTitle p | 141 | unless dryRun $ draftReplace draftId draftTitle p |
137 | 142 | ||
143 | tprint TPrint{ operation = DraftEdit{..}, ..} Client{..} _ = do | ||
144 | d@(fromMaybe T.empty -> dTitle, dContent) <- draft draftId | ||
145 | let | ||
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 | |||
138 | tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId | 163 | tprint TPrint{ operation = DraftDelete{..}, ..} Client{..} _ = unless dryRun $ draftDelete draftId |
139 | 164 | ||
140 | tprint TPrint{ operation = DraftPrint{..}, ..} client@Client{..} out = do | 165 | tprint TPrint{ operation = DraftPrint{..}, ..} client@Client{..} out = do |