on master help sign in
Allow colour options from REPL

Add the command :colour or :color. Syntax: :colour on - enable colors :colour off - disable colors :colour TYPE OPTIONS - set a colour where TYPE is keyword, function, type, implicit, boundvar amd OPTIONS are ANSI colour names, underline, bold, vivid, dull

commitfe20bf197dfc1cec582a6a745f9b3090f7c45744 authorDavid Christiansen <david@davidchristiansen.dk> dateFri 13 Sep 2013, 11:34 (13 years ago) parent7c45129
6 files changed+91−2
src/Idris/AbsSyntax.hs+12 src/Idris/AbsSyntaxTree.hs+3 src/Idris/Colours.hs+14−2 src/Idris/Help.hs+1 src/Idris/REPL.hs+5 src/Idris/REPLParser.hs+56
src/Idris/AbsSyntax.hs+12
@@ -9,6 +9,7 @@ import Core.Elaborate hiding (Tactic(..))
99import Core.Typecheck
1010import Idris.AbsSyntaxTree
1111import Idris.IdeSlave
12+import Idris.Colours
1213import IRTS.CodegenCommon
1314import Util.DynamicLinker
1415
@@ -506,6 +507,17 @@ setImpShow t = do i <- getIState
506507 let opt' = opts { opt_showimp = t }
507508 putIState $ i { idris_options = opt' }
508509
510+setColour :: ColourType -> IdrisColour -> Idris ()
511+setColour ct c = do i <- getIState
512+ let newTheme = setColour' ct c (idris_colourTheme i)
513+ putIState $ i { idris_colourTheme = newTheme }
514+ where setColour' KeywordColour c t = t { keywordColour = c }
515+ setColour' BoundVarColour c t = t { boundVarColour = c }
516+ setColour' ImplicitColour c t = t { implicitColour = c }
517+ setColour' FunctionColour c t = t { functionColour = c }
518+ setColour' TypeColour c t = t { typeColour = c }
519+ setColour' DataColour c t = t { dataColour = c }
520+
509521logLvl :: Int -> String -> Idris ()
510522logLvl l str = do i <- getIState
511523 let lvl = opt_logLevel (idris_options i)
src/Idris/AbsSyntaxTree.hs+3
@@ -235,6 +235,9 @@ data Command = Quit
235235 | SetOpt Opt
236236 | UnsetOpt Opt
237237 | NOP
238+ | SetColour ColourType IdrisColour
239+ | ColourOn
240+ | ColourOff
238241
239242data Opt = Filename String
240243 | Ver
src/Idris/Colours.hs+14−2
@@ -3,7 +3,8 @@ module Idris.Colours (
33 ColourTheme(..),
44 defaultTheme,
55 colouriseKwd, colouriseBound, colouriseImplicit,
6− colouriseType, colouriseFun, colouriseData) where
6+ colouriseType, colouriseFun, colouriseData,
7+ ColourType(..)) where
78
89import System.Console.ANSI
910
@@ -12,6 +13,7 @@ data IdrisColour = IdrisColour { colour :: Color
1213 , underline :: Bool
1314 , bold :: Bool
1415 }
16+ deriving (Eq, Show)
1517
1618mkColour :: Color -> IdrisColour
1719mkColour c = IdrisColour c True False False
@@ -23,6 +25,7 @@ data ColourTheme = ColourTheme { keywordColour :: IdrisColour
2325 , typeColour :: IdrisColour
2426 , dataColour :: IdrisColour
2527 }
28+ deriving (Eq, Show)
2629
2730defaultTheme :: ColourTheme
2831defaultTheme = ColourTheme { keywordColour = IdrisColour Black True True True
@@ -56,4 +59,13 @@ colouriseType :: ColourTheme -> String -> String
5659colouriseType t = colourise (typeColour t)
5760
5861colouriseData :: ColourTheme -> String -> String
59−colouriseData t = colourise (dataColour t)
62+colouriseData t = colourise (dataColour t)
63+
64+
65+data ColourType = KeywordColour
66+ | BoundVarColour
67+ | ImplicitColour
68+ | FunctionColour
69+ | TypeColour
70+ | DataColour
71+ deriving (Eq, Show, Bounded, Enum)
src/Idris/Help.hs+1
@@ -46,5 +46,6 @@ help =
4646 ([":?",":h",":help"], NoArg, "Display this help text"),
4747 ([":set"], OptionArg, "Set an option (errorcontext, showimplicits)"),
4848 ([":unset"], OptionArg, "Unset an option"),
49+ ([":colour", ":color"], NoArg, "Turn REPL colours on or off; set a specific colour"),
4950 ([":q",":quit"], NoArg, "Exit the Idris system")
5051 ]
src/Idris/REPL.hs+5
@@ -541,6 +541,11 @@ process fn (UnsetOpt ShowImpl) = setImpShow False
541541
542542process fn (SetOpt _) = iFail "Not a valid option"
543543process fn (UnsetOpt _) = iFail "Not a valid option"
544+process fn (SetColour ty c) = setColour ty c
545+process fn ColourOn = do ist <- getIState
546+ putIState $ ist { idris_colourRepl = True }
547+process fn ColourOff = do ist <- getIState
548+ putIState $ ist { idris_colourRepl = False }
544549
545550
546551classInfo :: ClassInfo -> Idris ()
src/Idris/REPLParser.hs+56
@@ -1,7 +1,9 @@
11module Idris.REPLParser(parseCmd) where
22
33import System.FilePath ((</>))
4+import System.Console.ANSI (Color(..))
45
6+import Idris.Colours
57import Idris.Parser
68import Idris.AbsSyntax
79import Core.TT
@@ -14,6 +16,7 @@ import qualified Text.ParserCombinators.Parsec.Token as PTok
1416import Debug.Trace
1517import Data.List
1618import Data.List.Split(splitOn)
19+import Data.Char(toLower)
1720
1821parseCmd i = runParser pCmd i "(input)"
1922
@@ -57,6 +60,7 @@ pCmd = do spaces; try (do cmd ["q", "quit"]; eof; return Quit)
5760 <|> try (do cmd ["miss", "missing"]; n <- pfName; eof; return (Missing n))
5861 <|> try (do cmd ["dynamic"]; eof; return ListDynamic)
5962 <|> try (do cmd ["dynamic"]; l <- getInput; return (DynamicLink l))
63+ <|> try (do cmd ["color", "colour"]; pSetColourCmd)
6064 <|> try (do cmd ["set"]; o <-pOption; return (SetOpt o))
6165 <|> try (do cmd ["unset"]; o <-pOption; return (UnsetOpt o))
6266 <|> try (do cmd ["s", "search"]; whiteSpace; t <- pFullExpr defaultSyntax; return (Search t))
@@ -71,3 +75,55 @@ pOption :: IParser Opt
7175pOption = do discard (symbol "errorcontext"); return ErrContext
7276 <|> do discard (symbol "showimplicits"); return ShowImpl
7377
78+
79+colours :: [(String, Color)]
80+colours = [ ("black", Black)
81+ , ("red", Red)
82+ , ("green", Green)
83+ , ("yellow", Yellow)
84+ , ("blue", Blue)
85+ , ("magenta", Magenta)
86+ , ("cyan", Cyan)
87+ , ("white", White)
88+ ]
89+
90+pColour :: IParser Color
91+pColour = doColour colours
92+ where doColour [] = fail "Unknown colour"
93+ doColour ((s, c):cs) = (try (symbol s) >> return c) <|> doColour cs
94+
95+pColourMod :: IParser (IdrisColour -> IdrisColour)
96+pColourMod = (symbol "vivid" >> return doVivid)
97+ <|> (symbol "dull" >> return doDull)
98+ <|> (symbol "underline" >> return doUnderline)
99+ <|> (symbol "nounderline" >> return doNoUnderline)
100+ <|> (symbol "bold" >> return doBold)
101+ <|> (symbol "nobold" >> return doNoBold)
102+ <|> (pColour >>= return . doSetColour)
103+ where doVivid i = i { vivid = True }
104+ doDull i = i { vivid = False }
105+ doUnderline i = i { underline = True }
106+ doNoUnderline i = i { underline = False }
107+ doBold i = i { bold = True }
108+ doNoBold i = i { bold = False }
109+ doSetColour c i = i { colour = c }
110+
111+
112+colourTypes :: [(String, ColourType)]
113+colourTypes = map (\x -> ((map toLower . reverse . drop 6 . reverse . show) x, x)) $
114+ enumFromTo minBound maxBound
115+
116+pColourType :: IParser ColourType
117+pColourType = doColourType colourTypes
118+ where doColourType [] = fail $ "Unknown colour category. Options: " ++
119+ (concat . intersperse ", " . map fst) colourTypes
120+ doColourType ((s,ct):cts) = (try (symbol s) >> return ct) <|> doColourType cts
121+
122+pSetColourCmd :: IParser Command
123+pSetColourCmd = (do c <- pColourType
124+ let defaultColour = IdrisColour Black True False False
125+ opts <- sepBy pColourMod spaces
126+ let colour = foldr ($) defaultColour $ reverse opts
127+ return $ SetColour c colour)
128+ <|> try (symbol "on" >> return ColourOn)
129+ <|> try (symbol "off" >> return ColourOff)