diff options
author | Kierán Meinhardt <kieran.meinhardt@gmail.com> | 2020-09-30 16:56:28 +0200 |
---|---|---|
committer | Kierán Meinhardt <kieran.meinhardt@gmail.com> | 2020-09-30 16:56:28 +0200 |
commit | 2e7376208f7d0ca3be926a000fde517a9f329023 (patch) | |
tree | c203d45f8c334abb03bdbfdef9daf87c317d412b /config | |
parent | 63bf1907d3e6629ac21da87b9e45303bcec2bdf9 (diff) |
kmein config: adapt saveAttachment to ContentRaw
Diffstat (limited to 'config')
-rw-r--r-- | config/kmein.hs | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/config/kmein.hs b/config/kmein.hs index 6377056..5a121bb 100644 --- a/config/kmein.hs +++ b/config/kmein.hs @@ -26,6 +26,7 @@ import Text.Hyphenation import Text.LineBreak import qualified Data.Map as M import qualified Data.Text as T +import qualified Data.Text.IO as T import qualified Data.Tree as Tree import qualified Data.Tree.Zipper as Z @@ -77,27 +78,24 @@ main = saveAttachment :: State -> IO (Maybe FilePath, State) saveAttachment q | TVMessagePart message part <- Z.label (cursor q) = do - let query = Notmuch.unMessageID $ Notmuch.messageId message - defaultFilename = "much_part_" <> show (Notmuch.partID part) <> "_" <> formatTime defaultTimeLocale "%s" (Notmuch.messageTime message) - destination = - attachmentDestination </> - maybe defaultFilename T.unpack (Notmuch.partContentFilename part) - Notmuch.notmuchShowPartRaw query (Notmuch.partID part) >>= \case - Right byteString -> do - LBS8.writeFile destination byteString - return - ( Just destination - , q { flashMessage = Plain "Attachment saved to " <> SGR [1] (Plain destination) <> Plain "." } - ) - Left err -> return - ( Nothing - , q { flashMessage = SGR [38,5,9] $ Plain err } - ) + let messageId = Notmuch.unMessageID (Notmuch.messageId message) + Notmuch.notmuchShowPart messageId (Notmuch.partID part) >>= \case + Right part' -> + let + destination + | Just partFileName <- Notmuch.partContentFilename part = attachmentDestination </> T.unpack partFileName + | otherwise = concat ["much_", formatTime defaultTimeLocale "%s" (Notmuch.messageTime message), "_", show (Notmuch.partID part)] + q' = q { flashMessage = SGR [1] (Plain destination) <> Plain " saved." } + in case Notmuch.partContent part' of + Notmuch.ContentText text -> (Just destination, q') <$ T.writeFile destination text + Notmuch.ContentRaw raw _ -> (Just destination, q') <$ LBS8.writeFile destination raw + _ -> return (Nothing, q { flashMessage = SGR [38,5,9] $ Plain "This part cannot be saved." }) + Left err -> return (Nothing, q { flashMessage = SGR [38,5,9] $ Plain err }) | otherwise = return (Nothing, q { flashMessage = SGR [38,5,9] $ Plain "Cursor not on attachment." }) reply :: State -> IO State -reply q = q <$ runCommand "i3-sensible-terminal -e $EDITOR -c 'read !mail-reply'" +reply q = q <$ spawnCommand "i3-sensible-terminal -e $EDITOR -c 'read !mail-reply'" myKeymap :: String -> State -> IO State myKeymap "h" = closeFold |