forked from LaurentRDC/pandoc-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenFile.hs
37 lines (32 loc) · 1.17 KB
/
OpenFile.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- |
-- Module : $header$
-- Copyright : (c) Laurent P René de Cotret, 2019 - present
-- License : GNU GPL, version 2 or above
-- Maintainer : [email protected]
-- Stability : internal
-- Portability : portable
--
-- Open a file in its default program.
module OpenFile (openFile) where
import Data.List (isInfixOf)
import System.Info (os)
import System.Process.Typed (proc, runProcess_, shell)
openFile :: FilePath -> IO ()
openFile
-- Aliases taken from cabal's Distribution.System module
| os `elem` ["mingw32", "win32", "cygwin32"] = openFileWindows
| any (`isInfixOf` os) ["linux", "bsd"] = openFileLinux
| os `elem` ["darwin"] = openFileMacOS
| otherwise = error $ "Unsupported OS: " <> os
openFileWindows :: FilePath -> IO ()
openFileWindows fp =
-- Call looks like: cmd /c 'start "" "my_filepath.html"'
runProcess_ $ shell $ mconcat ["cmd /c start ", quoted mempty, " ", quoted fp]
where
quoted f = mconcat ["\"", f, "\""]
openFileLinux :: FilePath -> IO ()
openFileLinux fp =
runProcess_ (proc "sh" ["-c", "xdg-open \"$0\" 2>&1 > /dev/null", fp])
openFileMacOS :: FilePath -> IO ()
openFileMacOS fp =
runProcess_ (proc "open" [fp])