Skip to content

Commit

Permalink
Add color/no-color flag
Browse files Browse the repository at this point in the history
This resolves anttih#2. Happy Hacktoberfest!
  • Loading branch information
MichaelXavier committed Oct 15, 2016
1 parent 6394573 commit 6704f0b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/PscPane/Action.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type State =
, screen Screen
, box Box
, prevPaneState PaneState
, colorize Boolean
}

getState StateT State AffN State
Expand All @@ -71,9 +72,9 @@ appN (BuildProject f) = do
buf ← lift $ spawnAff buildCmd
liftEff $ f <$> toString UTF8 buf
appN (DrawPaneState state a) = do
{ screen, box, cwd } ← getState
{ screen, box, cwd, colorize } ← getState
height ← liftEff rows
liftEff (setContent box (formatState cwd height state))
liftEff (setContent box (formatState colorize cwd height state))
liftEff (render screen)
modify (_ { prevPaneState = state })
pure a
Expand Down
10 changes: 6 additions & 4 deletions src/PscPane/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Data.Maybe.First (First(..), runFirst)
import Data.String (split, trim)
import Node.Path (FilePath)
import Node.Process as P
import Node.Yargs.Applicative (yarg, runY)
import Node.Yargs.Applicative (flag, yarg, runY)
import Node.Yargs.Setup (usage, defaultHelp, defaultVersion)
import PscIde.Command (RebuildResult(RebuildResult))
import PscPane.Parser (PscResult(PscResult))
Expand Down Expand Up @@ -91,8 +91,8 @@ rebuildModule path = do
toPaneState _ (Just res) = PscError res
toPaneState path Nothing = ModuleOk path "building project..."

app String Array String EffN Unit
app buildCmd dirs = void do
app String Array String Boolean EffN Unit
app buildCmd dirs colorize = void do
cwd ← P.cwd
let
screen = mkScreen { smartCSR: true }
Expand All @@ -109,7 +109,7 @@ app buildCmd dirs = void do
runAff fail pure do
running ← startPscIdeServer cwd $ range 4242 4252
port ← maybe (throwError (error "Cannot start psc-ide-server")) pure running
stateRef ← liftEff $ newRef { screen, box, port, cwd, buildCmd, prevPaneState: InitialBuild }
stateRef ← liftEff $ newRef { screen, box, port, cwd, buildCmd, prevPaneState: InitialBuild, colorize }
let
runCmd A.Action Unit AffN Unit
runCmd program =
Expand Down Expand Up @@ -169,3 +169,5 @@ main = do
(Just "Directory to watch for changes (default: \"src\")")
(Left ["src"])
true
<*> flag "p" ["color"]
(Just "Colorize output. On by default. --no-color to turn off.")
28 changes: 20 additions & 8 deletions src/PscPane/Pretty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,29 @@ data PaneState
| ModuleOk FilePath Progress
| PscError PaneResult

formatState FilePath Height PaneState String
formatState _ _ InitialBuild = "Building project..."
formatState cwd height (PscError res) = pretty cwd height res
formatState _ _ (ModuleOk path progress) = green "Module OK" <> " " <> path <> " (" <> progress <> ")"
formatState _ _ BuildSuccess = green "Build successful"
formatState Boolean FilePath Height PaneState String
formatState _ _ _ InitialBuild = "Building project..."
formatState colorize cwd height (PscError res) = pretty colorize cwd height res
formatState colorize _ _ (ModuleOk path progress) = green' colorize "Module OK" <> " " <> path <> " (" <> progress <> ")"
formatState colorize _ _ BuildSuccess = green' colorize "Build successful"

data PaneResult = Warning RebuildError | Error RebuildError

pretty FilePath Height PaneResult String
pretty cwd h (Warning warn) = prettyError' (yellow "Warning") cwd h warn
pretty cwd h (Error err) = prettyError' (red "Error") cwd h err
pretty Boolean FilePath Height PaneResult String
pretty colorize cwd h (Warning warn) = prettyError' (yellow' colorize "Warning") cwd h warn
pretty colorize cwd h (Error err) = prettyError' (red' colorize "Error") cwd h err

yellow' Boolean String String
yellow' true = yellow
yellow' fale = id

green' Boolean String String
green' true = green
green' fale = id

red' Boolean String String
red' true = red
red' fale = id

prettyError' String FilePath Height RebuildError String
prettyError' t cwd h (RebuildError err@{ position }) =
Expand Down

0 comments on commit 6704f0b

Please sign in to comment.