{-# options_haddock prune #-}
-- |CLI, Internal
module Helic.Cli where

import Options.Applicative (customExecParser, fullDesc, header, helper, info, prefs, showHelpOnEmpty, showHelpOnError)
import Polysemy.Chronos (ChronosTime, interpretTimeChronos)
import qualified Polysemy.Conc as Conc
import Polysemy.Conc (
  Critical,
  Interrupt,
  interpretAtomic,
  interpretCritical,
  interpretEventsChan,
  interpretInterrupt,
  interpretRace,
  )
import Polysemy.Error (errorToIOFinal)
import Polysemy.Http.Interpreter.Manager (interpretManager)
import qualified Polysemy.Log as Log
import Polysemy.Log (Log, Severity (Info), interpretLogStdoutLevelConc)
import Polysemy.Reader (runReader)
import Polysemy.Time (MilliSeconds (MilliSeconds))
import System.IO (hLookAhead)

import Helic.Cli.Options (Command (Listen, Yank), Conf (Conf), parser)
import Helic.Config.File (findFileConfig)
import Helic.Data.Config (Config (Config))
import Helic.Data.Event (Event)
import Helic.Data.XClipboardEvent (XClipboardEvent)
import Helic.Data.YankConfig (YankConfig (YankConfig))
import Helic.Interpreter.AgentNet (interpretAgentNet)
import Helic.Interpreter.AgentTmux (interpretAgentTmux)
import Helic.Interpreter.AgentX (interpretAgentX)
import Helic.Interpreter.InstanceName (interpretInstanceName)
import Helic.Interpreter.XClipboard (interpretXClipboardGtk, listenXClipboard)
import Helic.Listen (listen)
import Helic.Yank (yank)

logError ::
  Members [Log, Final IO] r =>
  Sem (Error Text : r) () ->
  Sem r ()
logError :: Sem (Error Text : r) () -> Sem r ()
logError Sem (Error Text : r) ()
sem =
  Sem (Error Text : r) () -> Sem r (Either Text ())
forall e (r :: EffectRow) a.
(Typeable e, Member (Final IO) r) =>
Sem (Error e : r) a -> Sem r (Either e a)
errorToIOFinal Sem (Error Text : r) ()
sem Sem r (Either Text ()) -> (Either Text () -> Sem r ()) -> Sem r ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Right () -> Sem r ()
forall (f :: * -> *). Applicative f => f ()
unit
    Left Text
err -> Text -> Sem r ()
forall (r :: EffectRow).
(HasCallStack, Member Log r) =>
Text -> Sem r ()
Log.error Text
err

type IOStack =
  [
    Error Text,
    Interrupt,
    Critical,
    Log,
    ChronosTime,
    Race,
    Async,
    Resource,
    Embed IO,
    Final IO
  ]

runIO ::
  Bool ->
  Sem IOStack () ->
  IO ()
runIO :: Bool -> Sem IOStack () -> IO ()
runIO Bool
verbose =
  Sem '[Final IO] () -> IO ()
forall (m :: * -> *) a. Monad m => Sem '[Final m] a -> m a
runFinal (Sem '[Final IO] () -> IO ())
-> (Sem IOStack () -> Sem '[Final IO] ())
-> Sem IOStack ()
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem '[Embed IO, Final IO] () -> Sem '[Final IO] ()
forall (m :: * -> *) (r :: EffectRow) a.
(Member (Final m) r, Functor m) =>
Sem (Embed m : r) a -> Sem r a
embedToFinal (Sem '[Embed IO, Final IO] () -> Sem '[Final IO] ())
-> (Sem IOStack () -> Sem '[Embed IO, Final IO] ())
-> Sem IOStack ()
-> Sem '[Final IO] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem '[Resource, Embed IO, Final IO] ()
-> Sem '[Embed IO, Final IO] ()
forall (r :: EffectRow) a.
Member (Final IO) r =>
Sem (Resource : r) a -> Sem r a
resourceToIOFinal (Sem '[Resource, Embed IO, Final IO] ()
 -> Sem '[Embed IO, Final IO] ())
-> (Sem IOStack () -> Sem '[Resource, Embed IO, Final IO] ())
-> Sem IOStack ()
-> Sem '[Embed IO, Final IO] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem '[Async, Resource, Embed IO, Final IO] ()
-> Sem '[Resource, Embed IO, Final IO] ()
forall (r :: EffectRow) a.
Member (Final IO) r =>
Sem (Async : r) a -> Sem r a
asyncToIOFinal (Sem '[Async, Resource, Embed IO, Final IO] ()
 -> Sem '[Resource, Embed IO, Final IO] ())
-> (Sem IOStack ()
    -> Sem '[Async, Resource, Embed IO, Final IO] ())
-> Sem IOStack ()
-> Sem '[Resource, Embed IO, Final IO] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem '[Race, Async, Resource, Embed IO, Final IO] ()
-> Sem '[Async, Resource, Embed IO, Final IO] ()
forall (r :: EffectRow).
Member (Final IO) r =>
InterpreterFor Race r
interpretRace (Sem '[Race, Async, Resource, Embed IO, Final IO] ()
 -> Sem '[Async, Resource, Embed IO, Final IO] ())
-> (Sem IOStack ()
    -> Sem '[Race, Async, Resource, Embed IO, Final IO] ())
-> Sem IOStack ()
-> Sem '[Async, Resource, Embed IO, Final IO] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem '[ChronosTime, Race, Async, Resource, Embed IO, Final IO] ()
-> Sem '[Race, Async, Resource, Embed IO, Final IO] ()
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor ChronosTime r
interpretTimeChronos (Sem '[ChronosTime, Race, Async, Resource, Embed IO, Final IO] ()
 -> Sem '[Race, Async, Resource, Embed IO, Final IO] ())
-> (Sem IOStack ()
    -> Sem
         '[ChronosTime, Race, Async, Resource, Embed IO, Final IO] ())
-> Sem IOStack ()
-> Sem '[Race, Async, Resource, Embed IO, Final IO] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Maybe Severity
-> InterpreterFor
     Log '[ChronosTime, Race, Async, Resource, Embed IO, Final IO]
forall (r :: EffectRow).
Members '[Resource, Async, Race, Embed IO] r =>
Maybe Severity -> InterpreterFor Log r
interpretLogStdoutLevelConc (if Bool
verbose then Maybe Severity
forall a. Maybe a
Nothing else Severity -> Maybe Severity
forall a. a -> Maybe a
Just Severity
Info) (Sem
   '[Log, ChronosTime, Race, Async, Resource, Embed IO, Final IO] ()
 -> Sem
      '[ChronosTime, Race, Async, Resource, Embed IO, Final IO] ())
-> (Sem IOStack ()
    -> Sem
         '[Log, ChronosTime, Race, Async, Resource, Embed IO, Final IO] ())
-> Sem IOStack ()
-> Sem '[ChronosTime, Race, Async, Resource, Embed IO, Final IO] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  '[Critical, Log, ChronosTime, Race, Async, Resource, Embed IO,
    Final IO]
  ()
-> Sem
     '[Log, ChronosTime, Race, Async, Resource, Embed IO, Final IO] ()
forall (r :: EffectRow).
Member (Final IO) r =>
InterpreterFor Critical r
interpretCritical (Sem
   '[Critical, Log, ChronosTime, Race, Async, Resource, Embed IO,
     Final IO]
   ()
 -> Sem
      '[Log, ChronosTime, Race, Async, Resource, Embed IO, Final IO] ())
-> (Sem IOStack ()
    -> Sem
         '[Critical, Log, ChronosTime, Race, Async, Resource, Embed IO,
           Final IO]
         ())
-> Sem IOStack ()
-> Sem
     '[Log, ChronosTime, Race, Async, Resource, Embed IO, Final IO] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem
  '[Interrupt, Critical, Log, ChronosTime, Race, Async, Resource,
    Embed IO, Final IO]
  ()
-> Sem
     '[Critical, Log, ChronosTime, Race, Async, Resource, Embed IO,
       Final IO]
     ()
forall (r :: EffectRow).
Members '[Critical, Race, Async, Embed IO] r =>
InterpreterFor Interrupt r
interpretInterrupt (Sem
   '[Interrupt, Critical, Log, ChronosTime, Race, Async, Resource,
     Embed IO, Final IO]
   ()
 -> Sem
      '[Critical, Log, ChronosTime, Race, Async, Resource, Embed IO,
        Final IO]
      ())
-> (Sem IOStack ()
    -> Sem
         '[Interrupt, Critical, Log, ChronosTime, Race, Async, Resource,
           Embed IO, Final IO]
         ())
-> Sem IOStack ()
-> Sem
     '[Critical, Log, ChronosTime, Race, Async, Resource, Embed IO,
       Final IO]
     ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Sem IOStack ()
-> Sem
     '[Interrupt, Critical, Log, ChronosTime, Race, Async, Resource,
       Embed IO, Final IO]
     ()
forall (r :: EffectRow).
Members '[Log, Final IO] r =>
Sem (Error Text : r) () -> Sem r ()
logError

listenApp ::
  Config ->
  Sem IOStack ()
listenApp :: Config -> Sem IOStack ()
listenApp (Config Maybe Text
name Maybe TmuxConfig
tmux Maybe NetConfig
net Maybe Int
maxHistory) =
  TmuxConfig
-> Sem (Reader TmuxConfig : IOStack) () -> Sem IOStack ()
forall i (r :: EffectRow) a. i -> Sem (Reader i : r) a -> Sem r a
runReader (TmuxConfig -> Maybe TmuxConfig -> TmuxConfig
forall a. a -> Maybe a -> a
fromMaybe TmuxConfig
forall a. Default a => a
def Maybe TmuxConfig
tmux) (Sem (Reader TmuxConfig : IOStack) () -> Sem IOStack ())
-> Sem (Reader TmuxConfig : IOStack) () -> Sem IOStack ()
forall a b. (a -> b) -> a -> b
$
  NetConfig
-> Sem (Reader NetConfig : Reader TmuxConfig : IOStack) ()
-> Sem (Reader TmuxConfig : IOStack) ()
forall i (r :: EffectRow) a. i -> Sem (Reader i : r) a -> Sem r a
runReader (NetConfig -> Maybe NetConfig -> NetConfig
forall a. a -> Maybe a -> a
fromMaybe NetConfig
forall a. Default a => a
def Maybe NetConfig
net) (Sem (Reader NetConfig : Reader TmuxConfig : IOStack) ()
 -> Sem (Reader TmuxConfig : IOStack) ())
-> Sem (Reader NetConfig : Reader TmuxConfig : IOStack) ()
-> Sem (Reader TmuxConfig : IOStack) ()
forall a b. (a -> b) -> a -> b
$
  forall (r :: EffectRow).
Members '[Resource, Race, Async, Embed IO] r =>
InterpretersFor
  '[Events (OutChan XClipboardEvent) XClipboardEvent,
    ChanConsumer XClipboardEvent]
  r
forall e (r :: EffectRow).
Members '[Resource, Race, Async, Embed IO] r =>
InterpretersFor '[Events (OutChan e) e, ChanConsumer e] r
interpretEventsChan @XClipboardEvent (Sem
   (Append
      '[Events (OutChan XClipboardEvent) XClipboardEvent,
        ChanConsumer XClipboardEvent]
      (Reader NetConfig : Reader TmuxConfig : IOStack))
   ()
 -> Sem (Reader NetConfig : Reader TmuxConfig : IOStack) ())
-> Sem
     (Append
        '[Events (OutChan XClipboardEvent) XClipboardEvent,
          ChanConsumer XClipboardEvent]
        (Reader NetConfig : Reader TmuxConfig : IOStack))
     ()
-> Sem (Reader NetConfig : Reader TmuxConfig : IOStack) ()
forall a b. (a -> b) -> a -> b
$
  forall (r :: EffectRow).
Members '[Resource, Race, Async, Embed IO] r =>
InterpretersFor
  '[Events (OutChan Event) Event, ChanConsumer Event] r
forall e (r :: EffectRow).
Members '[Resource, Race, Async, Embed IO] r =>
InterpretersFor '[Events (OutChan e) e, ChanConsumer e] r
interpretEventsChan @Event (Sem
   (Append
      '[Events (OutChan Event) Event, ChanConsumer Event]
      (Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack))
   ()
 -> Sem
      (Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (Append
        '[Events (OutChan Event) Event, ChanConsumer Event]
        (Events (OutChan XClipboardEvent) XClipboardEvent
           : ChanConsumer XClipboardEvent : Reader NetConfig
           : Reader TmuxConfig : IOStack))
     ()
-> Sem
     (Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Seq Event
-> InterpreterFor
     (AtomicState (Seq Event))
     (Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
forall a (r :: EffectRow).
Member (Embed IO) r =>
a -> InterpreterFor (AtomicState a) r
interpretAtomic Seq Event
forall a. Monoid a => a
mempty (Sem
   (AtomicState (Seq Event)
      : Events (OutChan Event) Event : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (Events (OutChan Event) Event
         : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Maybe Text
-> InterpreterFor
     (Reader InstanceName)
     (AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
forall (r :: EffectRow).
Members '[Error Text, Embed IO] r =>
Maybe Text -> InterpreterFor (Reader InstanceName) r
interpretInstanceName Maybe Text
name (Sem
   (Reader InstanceName
      : AtomicState (Seq Event) : Events (OutChan Event) Event
      : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (AtomicState (Seq Event)
         : Events (OutChan Event) Event : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Sem
  (Manager
     : Reader InstanceName : AtomicState (Seq Event)
     : Events (OutChan Event) Event : ChanConsumer Event
     : Events (OutChan XClipboardEvent) XClipboardEvent
     : ChanConsumer XClipboardEvent : Reader NetConfig
     : Reader TmuxConfig : IOStack)
  ()
-> Sem
     (Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor Manager r
interpretManager (Sem
   (Manager
      : Reader InstanceName : AtomicState (Seq Event)
      : Events (OutChan Event) Event : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (Reader InstanceName
         : AtomicState (Seq Event) : Events (OutChan Event) Event
         : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (Manager
        : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Sem
  (Reader GtkState
     : Manager : Reader InstanceName : AtomicState (Seq Event)
     : Events (OutChan Event) Event : ChanConsumer Event
     : Events (OutChan XClipboardEvent) XClipboardEvent
     : ChanConsumer XClipboardEvent : Reader NetConfig
     : Reader TmuxConfig : IOStack)
  ()
-> Sem
     (Manager
        : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall resource (r :: EffectRow).
Members
  '[Events resource XClipboardEvent, Error Text, Race, Resource,
    Async, Embed IO, Final IO]
  r =>
InterpreterFor (Reader GtkState) r
listenXClipboard (Sem
   (Reader GtkState
      : Manager : Reader InstanceName : AtomicState (Seq Event)
      : Events (OutChan Event) Event : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (Manager
         : Reader InstanceName : AtomicState (Seq Event)
         : Events (OutChan Event) Event : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (Reader GtkState
        : Manager : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (Manager
        : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Sem
  (XClipboard
     : Reader GtkState : Manager : Reader InstanceName
     : AtomicState (Seq Event) : Events (OutChan Event) Event
     : ChanConsumer Event
     : Events (OutChan XClipboardEvent) XClipboardEvent
     : ChanConsumer XClipboardEvent : Reader NetConfig
     : Reader TmuxConfig : IOStack)
  ()
-> Sem
     (Reader GtkState
        : Manager : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall (r :: EffectRow).
Members '[Reader GtkState, Embed IO] r =>
InterpreterFor XClipboard r
interpretXClipboardGtk (Sem
   (XClipboard
      : Reader GtkState : Manager : Reader InstanceName
      : AtomicState (Seq Event) : Events (OutChan Event) Event
      : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (Reader GtkState
         : Manager : Reader InstanceName : AtomicState (Seq Event)
         : Events (OutChan Event) Event : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (XClipboard
        : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (Reader GtkState
        : Manager : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Sem
  (Tagged AgentX Agent
     : XClipboard : Reader GtkState : Manager : Reader InstanceName
     : AtomicState (Seq Event) : Events (OutChan Event) Event
     : ChanConsumer Event
     : Events (OutChan XClipboardEvent) XClipboardEvent
     : ChanConsumer XClipboardEvent : Reader NetConfig
     : Reader TmuxConfig : IOStack)
  ()
-> Sem
     (XClipboard
        : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall xr er (r :: EffectRow).
(Members
   '[EventConsumer xr XClipboardEvent, Events er Event,
     Reader InstanceName, XClipboard, ChronosTime]
   r,
 Members
   '[Events xr XClipboardEvent, Error Text, Race, Resource, Async,
     Embed IO, Final IO]
   r) =>
InterpreterFor (Tagged AgentX Agent) r
interpretAgentX (Sem
   (Tagged AgentX Agent
      : XClipboard : Reader GtkState : Manager : Reader InstanceName
      : AtomicState (Seq Event) : Events (OutChan Event) Event
      : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (XClipboard
         : Reader GtkState : Manager : Reader InstanceName
         : AtomicState (Seq Event) : Events (OutChan Event) Event
         : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (Tagged AgentX Agent
        : XClipboard : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (XClipboard
        : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Sem
  (Tagged AgentNet Agent
     : Tagged AgentX Agent : XClipboard : Reader GtkState : Manager
     : Reader InstanceName : AtomicState (Seq Event)
     : Events (OutChan Event) Event : ChanConsumer Event
     : Events (OutChan XClipboardEvent) XClipboardEvent
     : ChanConsumer XClipboardEvent : Reader NetConfig
     : Reader TmuxConfig : IOStack)
  ()
-> Sem
     (Tagged AgentX Agent
        : XClipboard : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall resource (r :: EffectRow).
(Members
   '[Manager, Events resource Event, Reader InstanceName,
     Reader NetConfig]
   r,
 Members
   '[AtomicState (Seq Event), Log, Interrupt, Race, Resource, Async,
     Embed IO, Final IO]
   r) =>
InterpreterFor (Tagged AgentNet Agent) r
interpretAgentNet (Sem
   (Tagged AgentNet Agent
      : Tagged AgentX Agent : XClipboard : Reader GtkState : Manager
      : Reader InstanceName : AtomicState (Seq Event)
      : Events (OutChan Event) Event : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (Tagged AgentX Agent
         : XClipboard : Reader GtkState : Manager : Reader InstanceName
         : AtomicState (Seq Event) : Events (OutChan Event) Event
         : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (Tagged AgentNet Agent
        : Tagged AgentX Agent : XClipboard : Reader GtkState : Manager
        : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (Tagged AgentX Agent
        : XClipboard : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Sem
  (Tagged AgentTmux Agent
     : Tagged AgentNet Agent : Tagged AgentX Agent : XClipboard
     : Reader GtkState : Manager : Reader InstanceName
     : AtomicState (Seq Event) : Events (OutChan Event) Event
     : ChanConsumer Event
     : Events (OutChan XClipboardEvent) XClipboardEvent
     : ChanConsumer XClipboardEvent : Reader NetConfig
     : Reader TmuxConfig : IOStack)
  ()
-> Sem
     (Tagged AgentNet Agent
        : Tagged AgentX Agent : XClipboard : Reader GtkState : Manager
        : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall (r :: EffectRow).
Members
  '[Reader TmuxConfig, Log, Async, Race, Resource, Embed IO] r =>
InterpreterFor (Tagged AgentTmux Agent) r
interpretAgentTmux (Sem
   (Tagged AgentTmux Agent
      : Tagged AgentNet Agent : Tagged AgentX Agent : XClipboard
      : Reader GtkState : Manager : Reader InstanceName
      : AtomicState (Seq Event) : Events (OutChan Event) Event
      : ChanConsumer Event
      : Events (OutChan XClipboardEvent) XClipboardEvent
      : ChanConsumer XClipboardEvent : Reader NetConfig
      : Reader TmuxConfig : IOStack)
   ()
 -> Sem
      (Tagged AgentNet Agent
         : Tagged AgentX Agent : XClipboard : Reader GtkState : Manager
         : Reader InstanceName : AtomicState (Seq Event)
         : Events (OutChan Event) Event : ChanConsumer Event
         : Events (OutChan XClipboardEvent) XClipboardEvent
         : ChanConsumer XClipboardEvent : Reader NetConfig
         : Reader TmuxConfig : IOStack)
      ())
-> Sem
     (Tagged AgentTmux Agent
        : Tagged AgentNet Agent : Tagged AgentX Agent : XClipboard
        : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
-> Sem
     (Tagged AgentNet Agent
        : Tagged AgentX Agent : XClipboard : Reader GtkState : Manager
        : Reader InstanceName : AtomicState (Seq Event)
        : Events (OutChan Event) Event : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall a b. (a -> b) -> a -> b
$
  Maybe Int
-> Sem
     (Tagged AgentTmux Agent
        : Tagged AgentNet Agent : Tagged AgentX Agent : XClipboard
        : Reader GtkState : Manager : Reader InstanceName
        : AtomicState (Seq Event) : Events (OutChan Event) Event
        : ChanConsumer Event
        : Events (OutChan XClipboardEvent) XClipboardEvent
        : ChanConsumer XClipboardEvent : Reader NetConfig
        : Reader TmuxConfig : IOStack)
     ()
forall (r :: EffectRow) token.
(Members Agents r,
 Members
   '[EventConsumer token Event, AtomicState (Seq Event), ChronosTime,
     Log]
   r) =>
Maybe Int -> Sem r ()
listen Maybe Int
maxHistory

yankApp ::
  Config ->
  YankConfig ->
  Sem IOStack ()
yankApp :: Config -> YankConfig -> Sem IOStack ()
yankApp (Config Maybe Text
name Maybe TmuxConfig
_ Maybe NetConfig
net Maybe Int
_) YankConfig
yankConf =
  Sem (Manager : IOStack) () -> Sem IOStack ()
forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor Manager r
interpretManager (Sem (Manager : IOStack) () -> Sem IOStack ())
-> Sem (Manager : IOStack) () -> Sem IOStack ()
forall a b. (a -> b) -> a -> b
$
  Maybe Text
-> InterpreterFor (Reader InstanceName) (Manager : IOStack)
forall (r :: EffectRow).
Members '[Error Text, Embed IO] r =>
Maybe Text -> InterpreterFor (Reader InstanceName) r
interpretInstanceName Maybe Text
name (Sem (Reader InstanceName : Manager : IOStack) ()
 -> Sem (Manager : IOStack) ())
-> Sem (Reader InstanceName : Manager : IOStack) ()
-> Sem (Manager : IOStack) ()
forall a b. (a -> b) -> a -> b
$
  NetConfig
-> YankConfig -> Sem (Reader InstanceName : Manager : IOStack) ()
forall (r :: EffectRow).
Members
  '[Reader InstanceName, ChronosTime, Manager, Log, Race, Error Text,
    Embed IO]
  r =>
NetConfig -> YankConfig -> Sem r ()
yank (NetConfig -> Maybe NetConfig -> NetConfig
forall a. a -> Maybe a -> a
fromMaybe NetConfig
forall a. Default a => a
def Maybe NetConfig
net) YankConfig
yankConf

runCommand :: Config -> Command -> Sem IOStack ()
runCommand :: Config -> Command -> Sem IOStack ()
runCommand Config
config = \case
  Command
Listen ->
    Config -> Sem IOStack ()
listenApp Config
config
  Yank YankConfig
yankConf ->
    Config -> YankConfig -> Sem IOStack ()
yankApp Config
config YankConfig
yankConf

defaultCommand :: Sem IOStack Command
defaultCommand :: Sem IOStack Command
defaultCommand = do
  Sem IOStack (Maybe (Either Text Char))
-> MilliSeconds
-> Sem IOStack (Maybe (Either Text Char))
-> Sem IOStack (Maybe (Either Text Char))
forall u (r :: EffectRow) a.
(TimeUnit u, Member Race r) =>
Sem r a -> u -> Sem r a -> Sem r a
Conc.timeout_ (Maybe (Either Text Char) -> Sem IOStack (Maybe (Either Text Char))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Either Text Char)
forall a. Maybe a
Nothing) (Int64 -> MilliSeconds
MilliSeconds Int64
100) (Either Text Char -> Maybe (Either Text Char)
forall a. a -> Maybe a
Just (Either Text Char -> Maybe (Either Text Char))
-> Sem IOStack (Either Text Char)
-> Sem IOStack (Maybe (Either Text Char))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Char -> Sem IOStack (Either Text Char)
forall (r :: EffectRow) a.
Member (Embed IO) r =>
IO a -> Sem r (Either Text a)
tryAny (Handle -> IO Char
hLookAhead Handle
stdin)) Sem IOStack (Maybe (Either Text Char))
-> (Maybe (Either Text Char) -> Command) -> Sem IOStack Command
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
    Just (Right Char
_) -> YankConfig -> Command
Yank (Maybe Text -> YankConfig
YankConfig (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"cli"))
    Maybe (Either Text Char)
_ -> Command
Listen

withCliOptions :: Conf -> Maybe Command -> IO ()
withCliOptions :: Conf -> Maybe Command -> IO ()
withCliOptions (Conf Bool
verbose Maybe (Path Abs File)
file) Maybe Command
cmd =
  Bool -> Sem IOStack () -> IO ()
runIO Bool
verbose do
    Config
config <- Maybe (Path Abs File) -> Sem IOStack Config
forall (r :: EffectRow).
Members '[Log, Error Text, Embed IO] r =>
Maybe (Path Abs File) -> Sem r Config
findFileConfig Maybe (Path Abs File)
file
    Config -> Command -> Sem IOStack ()
runCommand Config
config (Command -> Sem IOStack ())
-> Sem IOStack Command -> Sem IOStack ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Sem IOStack Command
-> (Command -> Sem IOStack Command)
-> Maybe Command
-> Sem IOStack Command
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Sem IOStack Command
defaultCommand Command -> Sem IOStack Command
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Command
cmd

app :: IO ()
app :: IO ()
app = do
  (Conf
conf, Maybe Command
cmd) <- ParserPrefs
-> ParserInfo (Conf, Maybe Command) -> IO (Conf, Maybe Command)
forall a. ParserPrefs -> ParserInfo a -> IO a
customExecParser ParserPrefs
parserPrefs (Parser (Conf, Maybe Command)
-> InfoMod (Conf, Maybe Command)
-> ParserInfo (Conf, Maybe Command)
forall a. Parser a -> InfoMod a -> ParserInfo a
info (Parser (Conf, Maybe Command)
parser Parser (Conf, Maybe Command)
-> Parser ((Conf, Maybe Command) -> (Conf, Maybe Command))
-> Parser (Conf, Maybe Command)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> Parser ((Conf, Maybe Command) -> (Conf, Maybe Command))
forall a. Parser (a -> a)
helper) InfoMod (Conf, Maybe Command)
forall a. InfoMod a
desc)
  Conf -> Maybe Command -> IO ()
withCliOptions Conf
conf Maybe Command
cmd
  where
    parserPrefs :: ParserPrefs
parserPrefs =
      PrefsMod -> ParserPrefs
prefs (PrefsMod
showHelpOnEmpty PrefsMod -> PrefsMod -> PrefsMod
forall a. Semigroup a => a -> a -> a
<> PrefsMod
showHelpOnError)
    desc :: InfoMod a
desc =
      InfoMod a
forall a. InfoMod a
fullDesc InfoMod a -> InfoMod a -> InfoMod a
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod a
forall a. String -> InfoMod a
header String
"Helic is a clipboard synchronization tool."