module Chiasma.Data.WithPaneId where import qualified Data.Text as Text import Chiasma.Codec (TmuxCodec (decode, query), trim) import Chiasma.Codec.Decode (primDecode) import Chiasma.Data.DecodeError (DecodeError (DecodeError), DecodeFailure (TooFewFields)) import qualified Chiasma.Data.TmuxId as TmuxId import Chiasma.Data.TmuxId (HasPaneId, PaneId) import Chiasma.Data.TmuxQuery (TmuxQuery (TmuxQuery)) data WithPaneId a = WithPaneId { forall a. WithPaneId a -> PaneId paneId :: PaneId, forall a. WithPaneId a -> a pane :: a } deriving stock (WithPaneId a -> WithPaneId a -> Bool (WithPaneId a -> WithPaneId a -> Bool) -> (WithPaneId a -> WithPaneId a -> Bool) -> Eq (WithPaneId a) forall a. Eq a => WithPaneId a -> WithPaneId a -> Bool forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a $c== :: forall a. Eq a => WithPaneId a -> WithPaneId a -> Bool == :: WithPaneId a -> WithPaneId a -> Bool $c/= :: forall a. Eq a => WithPaneId a -> WithPaneId a -> Bool /= :: WithPaneId a -> WithPaneId a -> Bool Eq, Int -> WithPaneId a -> ShowS [WithPaneId a] -> ShowS WithPaneId a -> String (Int -> WithPaneId a -> ShowS) -> (WithPaneId a -> String) -> ([WithPaneId a] -> ShowS) -> Show (WithPaneId a) forall a. Show a => Int -> WithPaneId a -> ShowS forall a. Show a => [WithPaneId a] -> ShowS forall a. Show a => WithPaneId a -> String forall a. (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a $cshowsPrec :: forall a. Show a => Int -> WithPaneId a -> ShowS showsPrec :: Int -> WithPaneId a -> ShowS $cshow :: forall a. Show a => WithPaneId a -> String show :: WithPaneId a -> String $cshowList :: forall a. Show a => [WithPaneId a] -> ShowS showList :: [WithPaneId a] -> ShowS Show, (forall x. WithPaneId a -> Rep (WithPaneId a) x) -> (forall x. Rep (WithPaneId a) x -> WithPaneId a) -> Generic (WithPaneId a) forall x. Rep (WithPaneId a) x -> WithPaneId a forall x. WithPaneId a -> Rep (WithPaneId a) x forall a. (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a forall a x. Rep (WithPaneId a) x -> WithPaneId a forall a x. WithPaneId a -> Rep (WithPaneId a) x $cfrom :: forall a x. WithPaneId a -> Rep (WithPaneId a) x from :: forall x. WithPaneId a -> Rep (WithPaneId a) x $cto :: forall a x. Rep (WithPaneId a) x -> WithPaneId a to :: forall x. Rep (WithPaneId a) x -> WithPaneId a Generic) instance HasPaneId (WithPaneId a) where paneId :: WithPaneId a -> PaneId paneId = (.paneId) safeBreakOn :: Text -> Text -> Maybe (Text, Text) safeBreakOn :: Text -> Text -> Maybe (Text, Text) safeBreakOn Text n = \case Text "" -> Maybe (Text, Text) forall a. Maybe a Nothing Text t -> (Text, Text) -> Maybe (Text, Text) forall a. a -> Maybe a Just ((Text -> Text) -> (Text, Text) -> (Text, Text) forall b c a. (b -> c) -> (a, b) -> (a, c) forall (p :: * -> * -> *) b c a. Bifunctor p => (b -> c) -> p a b -> p a c second (Int -> Text -> Text Text.drop Int 1) (HasCallStack => Text -> Text -> (Text, Text) Text -> Text -> (Text, Text) Text.breakOn Text n Text t)) instance TmuxCodec a => TmuxCodec (WithPaneId a) where decode :: Text -> Either DecodeError (WithPaneId a) decode Text payload = case Text -> Text -> Maybe (Text, Text) safeBreakOn Text " " (Text -> Text trim Text payload) of Just (Text idField, Text rest) -> do a pane <- Text -> Either DecodeError a forall a. TmuxCodec a => Text -> Either DecodeError a decode Text rest PaneId paneId <- (DecodeFailure -> DecodeError) -> Either DecodeFailure PaneId -> Either DecodeError PaneId forall a b c. (a -> b) -> Either a c -> Either b c forall (p :: * -> * -> *) a b c. Bifunctor p => (a -> b) -> p a c -> p b c first ([Text] -> DecodeFailure -> DecodeError DecodeError [Text Item [Text] payload]) (Text -> Either DecodeFailure PaneId forall a. TmuxPrimDecode a => Text -> Either DecodeFailure a primDecode Text idField) pure WithPaneId {a PaneId $sel:paneId:WithPaneId :: PaneId $sel:pane:WithPaneId :: a pane :: a paneId :: PaneId ..} Maybe (Text, Text) Nothing -> DecodeError -> Either DecodeError (WithPaneId a) forall a b. a -> Either a b Left ([Text] -> DecodeFailure -> DecodeError DecodeError [Text Item [Text] payload] DecodeFailure TooFewFields) query :: TmuxQuery query = let TmuxQuery Text paneQuery = forall a. TmuxCodec a => TmuxQuery query @a in Text -> TmuxQuery TmuxQuery (Text "#{pane_id} " Text -> Text -> Text forall a. Semigroup a => a -> a -> a <> Text paneQuery)