--------------------------------------------------------------------------------
{-# 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.Kitty          as Kitty
import qualified Patat.Images.W3m            as W3m
import           Patat.Presentation.Internal


--------------------------------------------------------------------------------
new :: ImageSettings -> IO Handle
new :: ImageSettings -> IO Handle
new ImageSettings
is
    | ImageSettings -> Text
isBackend ImageSettings
is forall a. Eq a => a -> a -> Bool
== Text
"auto" = IO Handle
auto
    | Just (Backend Config a -> IO Handle
b) <- forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (ImageSettings -> Text
isBackend ImageSettings
is) [(Text, Backend)]
backends =
        case forall a. FromJSON a => Value -> Result a
A.fromJSON (Object -> Value
A.Object forall a b. (a -> b) -> a -> b
$ ImageSettings -> Object
isParams ImageSettings
is) of
            A.Success a
c -> Config a -> IO Handle
b (forall a. a -> Config a
Explicit a
c)
            A.Error [Char]
err -> forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$
                [Char]
"Patat.Images.new: Error parsing config for " forall a. [a] -> [a] -> [a]
++
                forall a. Show a => a -> [Char]
show (ImageSettings -> Text
isBackend ImageSettings
is) forall a. [a] -> [a] -> [a]
++ [Char]
" image backend: " forall a. [a] -> [a] -> [a]
++ [Char]
err
new ImageSettings
is = forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall a b. (a -> b) -> a -> b
$
    [Char]
"Patat.Images.new: Could not find " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show (ImageSettings -> Text
isBackend ImageSettings
is) forall a. [a] -> [a] -> [a]
++
    [Char]
" image backend."


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


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


--------------------------------------------------------------------------------
drawImage :: Handle -> FilePath -> IO Cleanup
drawImage :: Handle -> [Char] -> IO Cleanup
drawImage = Handle -> [Char] -> IO Cleanup
hDrawImage