From 405242c916fad3050b73ef19bcb2240ff04a2b1f Mon Sep 17 00:00:00 2001 From: Nils Steinger Date: Sat, 27 Apr 2019 16:00:42 +0200 Subject: [PATCH 1/3] Start external viewer non-blockingly This prevents paperboy's UI from becoming unresponsive while the external viewer is running. This also makes it possible to have multiple viewer instances running at the same time. --- src/Lib.hs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Lib.hs b/src/Lib.hs index ed8830b..e69fd1b 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -29,6 +29,7 @@ import qualified Path import qualified Path.IO as Path import qualified System.FilePath as F import qualified System.Process as P +import System.Directory (findExecutable) import qualified Text.PDF.Info as PDFI data FileInfo = FileInfo @@ -173,15 +174,20 @@ fileFile conf newFileName file = do openFile :: Path Abs File -> IO () openFile file = do - linuxOpen <- tryOpenWith file "xdg-open" - - if Either.isLeft linuxOpen + xdgOpenPresent <- findExecutable "xdg-open" + if Maybe.isJust xdgOpenPresent then do - _ <- tryOpenWith file "open" + tryOpenWith file "xdg-open" pure () - else pure () + else do + openPresent <- findExecutable "open" + if Maybe.isJust openPresent + then do + tryOpenWith file "open" + pure () + else pure () -tryOpenWith :: Path Abs File -> FilePath -> IO (Either SomeException String) +tryOpenWith :: Path Abs File -> FilePath -> IO (P.ProcessHandle) tryOpenWith file cmd = - E.try (P.readProcess cmd [Path.fromAbsFile file] "") + P.runCommand (cmd ++ " " ++ Path.fromAbsFile file) From f6710b41d0f7bb62fd0b94f682c551c9d9a3824a Mon Sep 17 00:00:00 2001 From: Nils Steinger Date: Sat, 27 Apr 2019 16:18:03 +0200 Subject: [PATCH 2/3] Blackhole any terminal output from external viewer --- src/Lib.hs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Lib.hs b/src/Lib.hs index e69fd1b..7518bd6 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -29,6 +29,7 @@ import qualified Path import qualified Path.IO as Path import qualified System.FilePath as F import qualified System.Process as P +import qualified GHC.IO.Handle.Types as IOHT import System.Directory (findExecutable) import qualified Text.PDF.Info as PDFI @@ -188,6 +189,7 @@ openFile file = do else pure () -tryOpenWith :: Path Abs File -> FilePath -> IO (P.ProcessHandle) -tryOpenWith file cmd = - P.runCommand (cmd ++ " " ++ Path.fromAbsFile file) +tryOpenWith :: Path Abs File -> FilePath -> IO () +tryOpenWith file cmd = do + _ <- P.createProcess (P.proc cmd [Path.fromAbsFile file]){ P.std_out = P.NoStream, P.std_err = P.NoStream } + return () From efa34ed23a2acb70627ed28ad32292256798fa7f Mon Sep 17 00:00:00 2001 From: Nils Steinger Date: Sat, 27 Apr 2019 16:37:38 +0200 Subject: [PATCH 3/3] Specify possible viewers as array Easier extensibility than with specialised code for each one. --- src/Lib.hs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Lib.hs b/src/Lib.hs index 7518bd6..d152f11 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -175,18 +175,18 @@ fileFile conf newFileName file = do openFile :: Path Abs File -> IO () openFile file = do - xdgOpenPresent <- findExecutable "xdg-open" - if Maybe.isJust xdgOpenPresent - then do - tryOpenWith file "xdg-open" - pure () + tryViewers ["xdg-open", "open"] file + + +tryViewers :: [[Char]] -> Path Abs File -> IO () +tryViewers [] file = do return () +tryViewers (e:es) file = do + v <- findExecutable e + if Maybe.isNothing v + then tryViewers es file else do - openPresent <- findExecutable "open" - if Maybe.isJust openPresent - then do - tryOpenWith file "open" - pure () - else pure () + tryOpenWith file e + return () tryOpenWith :: Path Abs File -> FilePath -> IO ()