Skip to content

Commit

Permalink
Allow to use commands with positional arguments
Browse files Browse the repository at this point in the history
This changes the `MailcapHandler` to support commands with positional
arguments. The patch removes the additional types and uses the
re-exports from `System.Process.Typed` instead. I did not see any reason
why we would need the additional overhead.

Instead of types, the handler is now a function which accepts the mail
as input to allow crafting whatever command is necessary.

Fixes #430
  • Loading branch information
romanofski committed May 16, 2021
1 parent c6c6d38 commit be6b798
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/Config/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import Control.Monad.Except (runExceptT)
import System.Environment (lookupEnv)
import System.Directory (getHomeDirectory)
import Data.Maybe (fromMaybe)
import Data.List.NonEmpty (fromList)
import System.Exit (ExitCode(..))

import Control.Lens (set)
Expand Down Expand Up @@ -290,9 +289,11 @@ defaultConfig =
, _mvToKeybindings = mailviewComposeToKeybindings
, _mvMailcap =
[ ( matchContentType "text" (Just "html")
, MailcapHandler (Shell (fromList "elinks -force-html")) CopiousOutput DiscardTempfile)
, MailcapHandler (\fp -> proc "elinks" ["-force-html", fp]) CopiousOutput DiscardTempfile)
, ( matchContentType "application" (Just "pdf")
, MailcapHandler (\fp -> proc "pdftotext" [fp, "-"]) CopiousOutput DiscardTempfile)
, ( const True
, MailcapHandler (Process (fromList "xdg-open") []) IgnoreOutput KeepTempfile)
, MailcapHandler (\fp -> proc "xdg-open" [fp]) IgnoreOutput KeepTempfile)
]
}
, _confIndexView = IndexViewSettings
Expand Down
6 changes: 0 additions & 6 deletions src/Purebred/System/Process.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ module Purebred.System.Process
, tmpfileResource
, draftFileResoure
, emptyResource
, toProcessConfigWithTempfile
, runEntityCommand
, createDraftFilePath
, createSentFilePath
Expand Down Expand Up @@ -54,7 +53,6 @@ import System.Directory (removeFile, createDirectoryIfMissing)
import qualified Data.ByteString.Lazy as LB
import qualified Data.ByteString as B
import Data.Char (isControl, isSpace)
import Data.Foldable (toList)
import Data.List (intercalate)
import Data.Time.Clock (getCurrentTime)
import Data.Time.Format (formatTime, defaultTimeLocale)
Expand Down Expand Up @@ -166,10 +164,6 @@ draftFileResoure maildir =
(tryIO . removeFile)
(\fp -> tryIO . B.writeFile fp)

toProcessConfigWithTempfile :: MakeProcess -> FilePath -> ProcessConfig () () ()
toProcessConfigWithTempfile (Shell cmd) fp = shell (toList cmd <> " " <> fp)
toProcessConfigWithTempfile (Process cmd args) fp = proc (toList cmd) (args <> [fp])

-- | Generates a Maildir filename
-- see https://cr.yp.to/proto/maildir.html
maildirMessageFileTemplate :: MonadIO m => m FilePath
Expand Down
10 changes: 5 additions & 5 deletions src/Storage/ParsedMail.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import Data.Text.Lens (packed)
import Control.Monad.Except (MonadError, throwError)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Catch (MonadMask)
import Data.Foldable (toList)
import qualified Data.ByteString as B
import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
Expand All @@ -67,7 +66,7 @@ import Purebred.System (tryIO)
import Purebred.Types.IFC (sanitiseText)
import Purebred.Parsing.Text (parseMailbody)
import Purebred.System.Process
(runEntityCommand, tmpfileResource, toProcessConfigWithTempfile,
(runEntityCommand, tmpfileResource,
tryReadProcessStdout, handleExitCodeThrow)

{- $synopsis
Expand Down Expand Up @@ -160,10 +159,11 @@ bodyToDisplay s textwidth charsets prefCT msg =
maybe
(pure $ parseMailbody textwidth "Internal Viewer" $ entityToText charsets entity)
(\handler ->
parseMailbody textwidth (showHandler handler) <$>
parseMailbody textwidth (showCommand handler) <$>
entityPiped handler entity)
(findAutoview s entity)
showHandler = view (mhMakeProcess . mpCommand . to (T.pack . toList))
showCommand :: MailcapHandler -> T.Text
showCommand h = T.pack $ show $ view mhMakeProcess h mempty
in (msg, ) <$> output


Expand Down Expand Up @@ -233,7 +233,7 @@ mkConfig cmd =
EntityCommand
handleExitCodeThrow
(tmpfileResource (view mhKeepTemp cmd))
(\_ fp -> toProcessConfigWithTempfile (view mhMakeProcess cmd) fp)
(\_ fp -> view mhMakeProcess cmd fp)
tryReadProcessStdout

quoteText :: T.Text -> T.Text
Expand Down
4 changes: 2 additions & 2 deletions src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,15 +1132,15 @@ data TempfileOnExit
deriving (Generic, NFData)

data MailcapHandler = MailcapHandler
{ _mhMakeProcess :: MakeProcess
{ _mhMakeProcess :: FilePath -> ProcessConfig () () ()
, _mhCopiousoutput :: CopiousOutput
-- ^ output should be paged or made scrollable
, _mhKeepTemp :: TempfileOnExit
-- ^ Keep the temporary file if application spawns child and parent
-- exits immediately (e.g. Firefox)
} deriving (Generic, NFData)

mhMakeProcess :: Lens' MailcapHandler MakeProcess
mhMakeProcess :: Lens' MailcapHandler (FilePath -> ProcessConfig () () ())
mhMakeProcess = lens _mhMakeProcess (\h x -> h { _mhMakeProcess = x })

mhCopiousoutput :: Lens' MailcapHandler CopiousOutput
Expand Down
6 changes: 3 additions & 3 deletions src/UI/Actions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,8 @@ openWithCommand =
cmd <- uses (asMailView . mvOpenCommand . E.editContentsL) (T.unpack . currentLine)
case cmd of
[] -> lift . Brick.continue . setUserMessage (makeWarning StatusBar "Empty command") =<< get
(x:xs) -> stateSuspendAndResume $
openCommand' (MailcapHandler (Process (x :| xs) []) IgnoreOutput KeepTempfile)
cmd' -> stateSuspendAndResume $
openCommand' (MailcapHandler (\_ -> proc cmd' []) IgnoreOutput KeepTempfile)
}

-- | Wrapper for 'Brick.suspendAndResume' that reads state from
Expand Down Expand Up @@ -1613,7 +1613,7 @@ openCommand' cmd = do
let con = EntityCommand
handleExitCodeThrow
(tmpfileResource (view mhKeepTemp cmd))
(\_ fp -> toProcessConfigWithTempfile (view mhMakeProcess cmd) fp)
(\_ fp -> view mhMakeProcess cmd fp)
tryReadProcessStderr
in fmap con . entityToBytes
selectedItemHelper (asMailView . mvAttachments) $ \ent ->
Expand Down

0 comments on commit be6b798

Please sign in to comment.