summaryrefslogtreecommitdiff
path: root/lib/Patat/Images.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Patat/Images.hs')
-rw-r--r--lib/Patat/Images.hs61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/Patat/Images.hs b/lib/Patat/Images.hs
new file mode 100644
index 0000000..3ea7e0f
--- /dev/null
+++ b/lib/Patat/Images.hs
@@ -0,0 +1,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