Skip to content

Commit

Permalink
Auto-format all code with purs-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Feb 8, 2024
1 parent feea01b commit 768f1fe
Show file tree
Hide file tree
Showing 25 changed files with 1,659 additions and 1,456 deletions.
3 changes: 3 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"width": 80
}
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"big-integer": "^1.6.52",
"chrono-node": "^2.7.5",
"csvnorm": "^1.1.0",
"js-yaml": "^4.1.0"
"js-yaml": "^4.1.0",
"purs-tidy": "^0.10.1"
},
"optionalDependencies": {
"converter": "0.0.5",
Expand Down
112 changes: 60 additions & 52 deletions src/CliSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import Effect (Effect)
import Effect.Class.Console (log, error)
import Node.Process (argv, setExitCode)


-- TODO: Automatically disable colors if not supported
makeRed :: String -> String
makeRed str =
Expand All @@ -31,14 +30,12 @@ makeYellow :: String -> String
makeYellow str =
withGraphics (foreground Yellow) str


errorAndExit :: String -> Effect (Result String Unit)
errorAndExit message = do
error (makeRed message)
setExitCode 1
pure $ Error message


parseCliSpec :: String -> Result String CliSpec
parseCliSpec cliSpecJsonStr = do
let cliSpecRes = fromEither $ jsonParser cliSpecJsonStr
Expand All @@ -51,47 +48,50 @@ parseCliSpec cliSpecJsonStr = do
# (lmap printJsonDecodeError)
# fromEither


callCommand
:: CliSpec
-> String
-> Array CliArgument
-> (String -> String -> Array CliArgument -> Effect (Result String Unit))
-> Effect (Result String Unit)
-> Effect (Result String Unit)
callCommand (CliSpec cliSpec) usageString args executor = do
case args # head of
Nothing -> do
log "No arguments provided"
setExitCode 1
pure (Error "No arguments provided")

Just firstArg | firstArg == FlagShort 'h'
|| firstArg == FlagLong "help"
|| firstArg == CmdArg "help" -> do
log usageString
pure $ Ok unit
Just firstArg
| firstArg == FlagShort 'h'
|| firstArg == FlagLong "help"
|| firstArg == CmdArg "help" -> do
log usageString
pure $ Ok unit

Just firstArg | firstArg == FlagShort 'v'
|| firstArg == FlagLong "version"
|| firstArg == CmdArg "version" -> do
log usageString
pure $ Ok unit
Just firstArg
| firstArg == FlagShort 'v'
|| firstArg == FlagLong "version"
|| firstArg == CmdArg "version" -> do
log usageString
pure $ Ok unit

Just _mainCmd ->
case args # drop 1 # head of
Just arg | arg == (CmdArg "help")
|| arg == (FlagLong "help")
|| arg == (FlagShort 'h') -> do
-- TODO: Only show help for subcommand
log usageString
pure $ Ok unit

Just arg | arg == (CmdArg "version")
|| arg == (FlagLong "version")
|| arg == (FlagShort 'v') -> do
-- TODO: Only show version of subcommand (if available)
log (cliSpec.version # fromMaybe "0")
pure $ Ok unit
Just arg
| arg == (CmdArg "help")
|| arg == (FlagLong "help")
|| arg == (FlagShort 'h') -> do
-- TODO: Only show help for subcommand
log usageString
pure $ Ok unit

Just arg
| arg == (CmdArg "version")
|| arg == (FlagLong "version")
|| arg == (FlagShort 'v') -> do
-- TODO: Only show version of subcommand (if available)
log (cliSpec.version # fromMaybe "0")
pure $ Ok unit

Just (CmdArg cmdName) -> do
let
Expand All @@ -102,8 +102,9 @@ callCommand (CliSpec cliSpec) usageString args executor = do

case commandMb of
Nothing -> do
let errStr =
makeRed ("ERROR: Unknown command \"" <> cmdName <> "\"")
let
errStr =
makeRed ("ERROR: Unknown command \"" <> cmdName <> "\"")
<> "\n\n"
<> usageString
log errStr
Expand All @@ -114,8 +115,9 @@ callCommand (CliSpec cliSpec) usageString args executor = do
executor cmdName usageString providedArgs

Just arg -> do
let errMsg =
"ERROR: First argument must be a command and not \""
let
errMsg =
"ERROR: First argument must be a command and not \""
<> cliArgToString arg
<> "\"\n\n"
log $ makeRed $ errMsg <> usageString
Expand All @@ -127,13 +129,11 @@ callCommand (CliSpec cliSpec) usageString args executor = do
setExitCode 1
pure $ Error "No arguments provided"


-- | Function to repeat a string n times
repeatString :: String -> Int -> String
repeatString str n =
fold $ replicate n str


callCliApp
:: CliSpec
-> (String -> String -> Array CliArgument -> Effect (Result String Unit))
Expand All @@ -144,32 +144,40 @@ callCliApp cliSpec@(CliSpec cliSpecRaw) executor = do
lengthLongestCmd =
cliSpecRaw.commands
# fromMaybe []
# foldl (\acc (CliSpec cmd) ->
if acc > Str.length cmd.name
then acc
else Str.length cmd.name
) 0
# foldl
( \acc (CliSpec cmd) ->
if acc > Str.length cmd.name then acc
else Str.length cmd.name
)
0

usageString =
"USAGE: " <> cliSpecRaw.name <> " <command> [options]"
<> "\n\n"
<> cliSpecRaw.description
<> "\n\n"
<> "COMMANDS:"
<> "\n\n"
<> (cliSpecRaw.commands
# fromMaybe []
# foldMap (\(CliSpec cmd) ->
cmd.name
<> (repeatString " " (lengthLongestCmd - Str.length cmd.name))
<> " " <> cmd.description <> "\n"
)
<> "\n\n"
<> cliSpecRaw.description
<> "\n\n"
<> "COMMANDS:"
<> "\n\n"
<>
( cliSpecRaw.commands
# fromMaybe []
# foldMap
( \(CliSpec cmd) ->
cmd.name
<>
( repeatString " "
(lengthLongestCmd - Str.length cmd.name)
)
<> " "
<> cmd.description
<> "\n"
)
)

arguments <- argv

let
argsNoInterpreter = arguments # drop 1 -- Drop "node"
argsNoInterpreter = arguments # drop 1 -- Drop "node"
cliArgsMb =
tokensToCliArguments
cliSpec
Expand Down
Loading

0 comments on commit 768f1fe

Please sign in to comment.