From 126ce037f82499d4acb055d764842d74f14ae62e Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sat, 6 Jun 2020 10:31:55 +0100 Subject: [PATCH] tmux-mate init command (#25) * Init command * Default to tmux-mate.dhall if nothing else passed * Start command * Update readme --- README.md | 9 +++++++++ app/CLICommands.hs | 7 +++++++ app/Main.hs | 35 ++++++++++++-------------------- app/Options.hs | 46 +++++++++++++++++++++++++++++++++++++++++++ src/TmuxMate.hs | 5 ++++- src/TmuxMate/Init.hs | 37 ++++++++++++++++++++++++++++++++++ src/TmuxMate/Types.hs | 2 +- 7 files changed, 117 insertions(+), 24 deletions(-) create mode 100644 app/CLICommands.hs create mode 100644 app/Options.hs create mode 100644 src/TmuxMate/Init.hs diff --git a/README.md b/README.md index 77ad21f..e46e670 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,15 @@ brew update && brew install danieljharvey/tools/tmux-mate Binaries available on the [releases](https://github.com/danieljharvey/tmux-mate/releases) page. +### Getting started + +```bash +# create a default tmux-mate.dhall +tmux-mate init +# Start running everything +tmux-mate start +``` + ### Tutorial Let's grab a couple of sample config files... diff --git a/app/CLICommands.hs b/app/CLICommands.hs new file mode 100644 index 0000000..b4ee134 --- /dev/null +++ b/app/CLICommands.hs @@ -0,0 +1,7 @@ +module CLICommands where + +import TmuxMate (CLIOptions (..)) + +data CLICommand + = CLIRun CLIOptions + | CLIInit diff --git a/app/Main.hs b/app/Main.hs index 53593fd..a5c5dce 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,29 +1,20 @@ module Main where -import Options.Applicative +import CLICommands +import Options (command) +import qualified Options.Applicative as Opt import System.Exit import TmuxMate main :: IO () main = do - options' <- execParser (info options fullDesc) - didItWork <- loadTestSession options' - case didItWork of - Yeah -> exitWith ExitSuccess - Nah i -> exitWith (ExitFailure i) - -configFilePathParser :: Parser ConfigFilePath -configFilePathParser = - ConfigFilePath - <$> argument str (metavar "") - -verbosityParser :: Parser Verbosity -verbosityParser = - flag' Chatty (short 'v' <> long "verbose") - <|> flag' DryRun (short 'd' <> long "dry-run") - <|> pure Silent - -options :: Parser CLIOptions -options = - CLIOptions - <$> configFilePathParser <*> verbosityParser + command' <- Opt.execParser (Opt.info command Opt.fullDesc) + case command' of + CLIInit -> do + createTmuxMateDhall + putStrLn "Initial tmux-mate.dhall created!" + CLIRun options' -> do + didItWork <- loadTestSession options' + case didItWork of + Yeah -> exitWith ExitSuccess + Nah i -> exitWith (ExitFailure i) diff --git a/app/Options.hs b/app/Options.hs new file mode 100644 index 0000000..5a04319 --- /dev/null +++ b/app/Options.hs @@ -0,0 +1,46 @@ +module Options (command) where + +import CLICommands +import Options.Applicative ((<|>)) +import qualified Options.Applicative as Opt +import TmuxMate + +command :: Opt.Parser CLICommand +command = + otherCommands + <|> (CLIRun <$> options) + +configFilePathParser :: Opt.Parser (Maybe ConfigFilePath) +configFilePathParser = + ( Just <$> ConfigFilePath + <$> Opt.argument Opt.str (Opt.metavar "") + ) + <|> pure Nothing + +verbosityParser :: Opt.Parser Verbosity +verbosityParser = + Opt.flag' Chatty (Opt.short 'v' <> Opt.long "verbose") + <|> Opt.flag' DryRun (Opt.short 'd' <> Opt.long "dry-run") + <|> pure Silent + +options :: Opt.Parser CLIOptions +options = + CLIOptions + <$> configFilePathParser <*> verbosityParser + +otherCommands :: Opt.Parser CLICommand +otherCommands = + Opt.subparser + ( Opt.command + "init" + ( Opt.info + (pure CLIInit) + (Opt.progDesc "Initialise a new tmux-mate.dhall file") + ) + <> Opt.command + "start" + ( Opt.info + (CLIRun <$> options) + (Opt.progDesc "Start running everything in the selected config file") + ) + ) diff --git a/src/TmuxMate.hs b/src/TmuxMate.hs index 9b6bdee..4c6a3eb 100644 --- a/src/TmuxMate.hs +++ b/src/TmuxMate.hs @@ -7,12 +7,15 @@ module TmuxMate CLIOptions (..), ConfigFilePath (..), Verbosity (..), + createTmuxMateDhall, ) where +import Data.Maybe (fromMaybe) import qualified Dhall as Dhall import System.Process import TmuxMate.Commands +import TmuxMate.Init import TmuxMate.Logger import TmuxMate.Running import TmuxMate.TmuxCommands @@ -36,7 +39,7 @@ data DidItWork loadTestSession :: CLIOptions -> IO DidItWork loadTestSession options = do let (decoder :: Dhall.Decoder Session) = Dhall.auto - let path = getConfigFilePath $ configFilePath options + let path = fromMaybe "tmux-mate.dhall" (getConfigFilePath <$> configFilePath options) myLog = logger (verbosity options) config <- Dhall.detailed (Dhall.inputFile decoder path) case parseSession config of diff --git a/src/TmuxMate/Init.hs b/src/TmuxMate/Init.hs new file mode 100644 index 0000000..08ae86e --- /dev/null +++ b/src/TmuxMate/Init.hs @@ -0,0 +1,37 @@ +module TmuxMate.Init (createTmuxMateDhall) where + +-- where we make a new empty session file + +import Data.Text.IO +import Dhall +import Dhall.Core (pretty) +import TmuxMate.Types + ( Pane (..), + PaneArrangement (..), + PaneCommand (..), + Session (..), + SessionName (..), + Window (..), + WindowName (..), + ) + +createTmuxMateDhall :: IO () +createTmuxMateDhall = do + let dhallVal = pretty (embed inject defaultSession) + Data.Text.IO.writeFile "./tmux-mate.dhall" dhallVal + +defaultSession :: Session +defaultSession = + Session + (SessionName "tmux-mate") + [ Window + (WindowName "first") + [ Pane + ( PaneCommand "watch echo \"hello from tmux-mate\"" + ), + Pane + ( PaneCommand "watch echo \"hello again from tmux-mate\"" + ) + ] + (PaneArrangement "tiled") + ] diff --git a/src/TmuxMate/Types.hs b/src/TmuxMate/Types.hs index d40eb2d..ba82068 100644 --- a/src/TmuxMate/Types.hs +++ b/src/TmuxMate/Types.hs @@ -166,7 +166,7 @@ newtype ConfigFilePath data CLIOptions = CLIOptions - { configFilePath :: ConfigFilePath, + { configFilePath :: Maybe ConfigFilePath, verbosity :: Verbosity } deriving (Eq, Ord, Show)