Skip to content

Commit

Permalink
Switch to subcommands in CLI and add initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jun 16, 2024
1 parent 7466513 commit dac6b54
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ Finally, you can install the CLI with:
```
stack install
```

### Adding the prompt to your shell
In your `.zshrc` add
```sh
eval "$(prompt init)"
```
41 changes: 28 additions & 13 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ module Main where

import Control.Exception (SomeException)
import Control.Exception.Base (try)
import Control.Monad (when)
import GHC.IO.Exception (ExitCode (ExitSuccess))
import Options.Applicative
import System.Environment.XDG.BaseDir (getUserConfigDir, getUserDataDir)
import System.FilePath ((</>))
import System.Process (cwd, proc, readCreateProcessWithExitCode, readProcessWithExitCode)

data Command = Default | Init | Recompile

newtype Args = Args
{aRecompile :: Bool}
{aRecompile :: Command}

args :: Parser Args
args =
Args
<$> switch
( long "recompile"
<> short 'r'
<> help "Whether to recompile"
)
<$> ( subparser
( command "init" (info (pure Init) (progDesc "Initialize the shell prompt."))
<> command "recompile" (info (pure Recompile) (progDesc "Recompile the configuration."))
)
<|> pure Default
)

main :: IO ()
main = run =<< execParser opts
Expand All @@ -33,11 +35,12 @@ appName :: String
appName = "prompt"

run :: Args -> IO ()
run (Args True) = recompile
run (Args False) = prompt
run (Args Default) = runPrompt
run (Args Init) = runInit
run (Args Recompile) = runRecompile

prompt :: IO ()
prompt = do
runPrompt :: IO ()
runPrompt = do
dataDir <- getUserDataDir ""
let binPath = dataDir </> appName

Expand All @@ -53,8 +56,20 @@ prompt = do
then putStrLn stdout
else putStrLn stderr

recompile :: IO ()
recompile = do
runInit :: IO ()
runInit =
let s =
unlines
[ "#!/bin/zsh",
"setopt prompt_subst",
"function mkPrompt() { PROMPT=\"$(prompt)\" }",
"typeset -a precmd_functions",
"precmd_functions=(mkPrompt)"
]
in putStrLn s

runRecompile :: IO ()
runRecompile = do
dataDir <- getUserDataDir ""
configDir <- getUserConfigDir ""

Expand Down

0 comments on commit dac6b54

Please sign in to comment.