{-# LANGUAGE TemplateHaskell #-}
module Patat.Images.Kitty
( backend
) where
import Control.Exception (throwIO)
import Control.Monad (unless, void, when)
import qualified Data.Aeson as A
import qualified Data.List as L
import Patat.Cleanup (Cleanup)
import qualified Patat.Images.Internal as Internal
import Data.Functor (($>))
import System.Environment (lookupEnv)
import System.Process (readProcess)
backend :: Internal.Backend
backend :: Backend
backend = forall a. FromJSON a => (Config a -> IO Handle) -> Backend
Internal.Backend Config Config -> IO Handle
new
data Config = Config deriving (Config -> Config -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Config -> Config -> Bool
$c/= :: Config -> Config -> Bool
== :: Config -> Config -> Bool
$c== :: Config -> Config -> Bool
Eq)
instance A.FromJSON Config where parseJSON :: Value -> Parser Config
parseJSON Value
_ = forall (m :: * -> *) a. Monad m => a -> m a
return Config
Config
new :: Internal.Config Config -> IO Internal.Handle
new :: Config Config -> IO Handle
new Config Config
config = do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Config Config
config forall a. Eq a => a -> a -> Bool
== forall a. Config a
Internal.Auto) forall a b. (a -> b) -> a -> b
$ do
Maybe String
term <- String -> IO (Maybe String)
lookupEnv String
"TERM"
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (String
"kitty" forall a. Eq a => [a] -> [a] -> Bool
`L.isInfixOf`) Maybe String
term) forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$
String -> BackendNotSupported
Internal.BackendNotSupported String
"TERM does not indicate kitty"
forall (m :: * -> *) a. Monad m => a -> m a
return Internal.Handle {hDrawImage :: String -> IO (IO ())
Internal.hDrawImage = String -> IO (IO ())
drawImage}
drawImage :: FilePath -> IO Cleanup
drawImage :: String -> IO (IO ())
drawImage String
path = [String] -> IO ()
icat [String
"--align=center", String
path] forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> [String] -> IO ()
icat [String
"--clear"]
where
icat :: [String] -> IO ()
icat [String]
args = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ String -> [String] -> String -> IO String
readProcess
String
"kitty" (String
"+kitten" forall a. a -> [a] -> [a]
: String
"icat" forall a. a -> [a] -> [a]
: String
"--transfer-mode=stream" forall a. a -> [a] -> [a]
: [String]
args) String
""