summaryrefslogtreecommitdiff
path: root/lib/Patat/Images.hs
blob: 3ea7e0f648ccf78c14ac06875462c5361a9c7d61 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
module Patat.Images
    ( Backend
    , Handle
    , new
    , drawImage
    ) where


--------------------------------------------------------------------------------
import           Control.Exception           (catch)
import qualified Data.Aeson                  as A
import qualified Data.Text                   as T
import           Patat.Cleanup
import           Patat.Images.Internal
import qualified Patat.Images.ITerm2         as ITerm2
import qualified Patat.Images.W3m            as W3m
import           Patat.Presentation.Internal


--------------------------------------------------------------------------------
new :: ImageSettings -> IO Handle
new is
    | isBackend is == "auto" = auto
    | Just (Backend b) <- lookup (isBackend is) backends =
        case A.fromJSON (A.Object $ isParams is) of
            A.Success c -> b (Explicit c)
            A.Error err -> fail $
                "Patat.Images.new: Error parsing config for " ++
                show (isBackend is) ++ " image backend: " ++ err
new is = fail $
    "Patat.Images.new: Could not find " ++ show (isBackend is) ++
    " image backend."


--------------------------------------------------------------------------------
auto :: IO Handle
auto = go [] backends
  where
    go names ((name, Backend b) : bs) = catch
        (b Auto)
        (\(BackendNotSupported _) -> go (name : names) bs)
    go names [] = fail $
        "Could not find a supported backend, tried: " ++
        T.unpack (T.intercalate ", " (reverse names))


--------------------------------------------------------------------------------
-- | All supported backends.  We can use CPP to include or exclude some
-- depending on platform availability.
backends :: [(T.Text, Backend)]
backends =
    [ ("iterm2", ITerm2.backend)
    , ("w3m",    W3m.backend)
    ]


--------------------------------------------------------------------------------
drawImage :: Handle -> FilePath -> IO Cleanup
drawImage = hDrawImage