@@ -25,7 +25,7 @@ import Graphics.Vty.Image (DisplayRegion)
2525import Graphics.Vty.DisplayAttributes
2626import Graphics.Vty.Output.Interface
2727
28−import Blaze.ByteString.Builder (Write, writeToByteString, writeStorable, writeWord8)
28+import Blaze.ByteString.Builder (Write, writeToByteString, writeStorable)
2929
3030import Data.IORef
3131import Data.Maybe (isJust, isNothing, fromJust)
@@ -49,6 +49,7 @@ data TerminfoCaps = TerminfoCaps
4949 , cup :: CapExpression
5050 , cnorm :: Maybe CapExpression
5151 , civis :: Maybe CapExpression
52+ , supportsNoColors :: Bool
5253 , useAltColorMap :: Bool
5354 , setForeColor :: CapExpression
5455 , setBackColor :: CapExpression
@@ -103,28 +104,28 @@ sendCapToTerminal t cap capParams = do
103104--
104105-- * Providing independent string capabilities for all display
105106-- attributes.
106−reserveTerminal :: String -> Fd -> ColorMode -> IO Output
107−reserveTerminal termName outFd colorMode = do
107+reserveTerminal :: String -> Fd -> IO Output
108+reserveTerminal termName outFd = do
108109 ti <- Terminfo.setupTerm termName
109110 -- assumes set foreground always implies set background exists.
110111 -- if set foreground is not set then all color changing style
111112 -- attributes are filtered.
112113 msetaf <- probeCap ti "setaf"
113114 msetf <- probeCap ti "setf"
114− let (useAlt, setForeCap)
115+ let (noColors, useAlt, setForeCap)
115116 = case msetaf of
116− Just setaf -> (False, setaf)
117+ Just setaf -> (False, False, setaf)
117118 Nothing -> case msetf of
118− Just setf -> (True, setf)
119− Nothing -> (True, error $ "no fore color support for terminal " ++ termName)
119+ Just setf -> (False, True, setf)
120+ Nothing -> (True, True, error $ "no fore color support for terminal " ++ termName)
120121 msetab <- probeCap ti "setab"
121122 msetb <- probeCap ti "setb"
122− let setBackCap
123+ let set_back_cap
123124 = case msetab of
124− Just setab -> setab
125125 Nothing -> case msetb of
126126 Just setb -> setb
127127 Nothing -> error $ "no back color support for terminal " ++ termName
128+ Just setab -> setab
128129
129130 hyperlinkModeStatus <- newIORef False
130131 newAssumedStateRef <- newIORef initialAssumedState
@@ -150,9 +151,10 @@ reserveTerminal termName outFd colorMode = do
150151 <*> requireCap ti "cup"
151152 <*> probeCap ti "cnorm"
152153 <*> probeCap ti "civis"
154+ <*> pure noColors
153155 <*> pure useAlt
154156 <*> pure setForeCap
155− <*> pure setBackCap
157+ <*> pure set_back_cap
156158 <*> requireCap ti "sgr0"
157159 <*> requireCap ti "clear"
158160 <*> requireCap ti "el"
@@ -192,12 +194,17 @@ reserveTerminal termName outFd colorMode = do
192194 when (toEnum len /= actualLen) $ fail $ "Graphics.Vty.Output: outputByteBuffer "
193195 ++ "length mismatch. " ++ show len ++ " /= " ++ show actualLen
194196 ++ " Please report this bug to vty project."
197+ , contextColorCount
198+ = case supportsNoColors terminfoCaps of
199+ False -> case Terminfo.getCapability ti (Terminfo.tiGetNum "colors" ) of
200+ Nothing -> 8
201+ Just v -> toEnum v
202+ True -> 1
195203 , supportsCursorVisibility = isJust $ civis terminfoCaps
196204 , supportsMode = terminfoModeSupported
197205 , setMode = terminfoSetMode
198206 , getModeStatus = terminfoModeStatus
199207 , assumedStateRef = newAssumedStateRef
200− , outputColorMode = colorMode
201208 -- I think fix would help assure tActual is the only
202209 -- reference. I was having issues tho.
203210 , mkDisplayContext = (`terminfoDisplayContext` terminfoCaps)
@@ -295,7 +302,7 @@ writeURLEscapes NoLinkChange =
295302-- | Portably setting the display attributes is a giant pain in the ass.
296303--
297304-- If the terminal supports the sgr capability (which sets the on/off
298−-- state of each style directly; and, for no good reason, resets the
305+-- state of each style directly ; and, for no good reason, resets the
299306-- colors to the default) this procedure is used:
300307--
301308-- 0. set the style attributes. This resets the fore and back color.
@@ -334,9 +341,9 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
334341 -- The only way to reset either color, portably, to the default
335342 -- is to use either the set state capability or the set default
336343 -- capability.
337− True -> do
344+ True -> do
338345 case reqDisplayCapSeqFor (displayAttrCaps terminfoCaps)
339− (fixedStyle attr)
346+ (fixedStyle attr )
340347 (styleToApplySeq $ fixedStyle attr) of
341348 -- only way to reset a color to the defaults
342349 EnterExitSeq caps -> writeDefaultAttr dc urlsEnabled
@@ -369,9 +376,9 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
369376 -- applied states.
370377 EnterExitSeq caps -> foldMap (\cap -> writeCapExpr cap []) caps
371378 `mappend`
372− writeColorDiff Foreground (foreColorDiff diffs)
379+ writeColorDiff setForeColor (foreColorDiff diffs)
373380 `mappend`
374− writeColorDiff Background (backColorDiff diffs)
381+ writeColorDiff setBackColor (backColorDiff diffs)
375382 -- implicitly resets the colors to the defaults
376383 SetState state -> writeCapExpr (fromJust $ setAttrStates
377384 $ displayAttrCaps terminfoCaps
@@ -403,65 +410,29 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
403410 | otherwise = mempty
404411 setColors =
405412 (case fixedForeColor attr of
406− Just c -> writeColor Foreground c
413+ Just c -> writeCapExpr (setForeColor terminfoCaps)
414+ [toEnum $ colorMap c]
407415 Nothing -> mempty)
408416 `mappend`
409417 (case fixedBackColor attr of
410− Just c -> writeColor Background c
418+ Just c -> writeCapExpr (setBackColor terminfoCaps)
419+ [toEnum $ colorMap c]
411420 Nothing -> mempty)
412− writeColorDiff _side NoColorChange
421+ writeColorDiff _f NoColorChange
413422 = mempty
414− writeColorDiff _side ColorToDefault
423+ writeColorDiff _f ColorToDefault
415424 = error "ColorToDefault is not a possible case for applyColorDiffs"
416− writeColorDiff side (SetColor c)
417− = writeColor side c
418−
419− writeColor side (RGBColor r g b) =
420− case outputColorMode (contextDevice dc) of
421− FullColor ->
422− hardcodeColor side (r, g, b)
423− _ ->
424− error "clampColor should remove rgb colors in standard mode"
425− writeColor side c =
426− writeCapExpr (setSideColor side terminfoCaps) [toEnum $ colorMap c]
427−
428−-- a color can either be in the foreground or the background
429−data ColorSide = Foreground | Background
430−
431−-- get the capability for drawing a color on a specific side
432−setSideColor :: ColorSide -> TerminfoCaps -> CapExpression
433−setSideColor Foreground = setForeColor
434−setSideColor Background = setBackColor
435−
436−hardcodeColor :: ColorSide -> (Word8, Word8, Word8) -> Write
437−hardcodeColor side (r, g, b) =
438− -- hardcoded color codes are formatted as "\x1b[{side};2;{r};{g};{b}m"
439− mconcat [ writeStr "\x1b[", sideCode, delimiter, writeChar '2', delimiter
440− , writeColor r, delimiter, writeColor g, delimiter, writeColor b
441− , writeChar 'm']
442− where
443− writeChar = writeWord8 . fromIntegral . fromEnum
444− writeStr = mconcat . map writeChar
445− writeColor = writeStr . show
446− delimiter = writeChar ';'
447− -- 38/48 are used to set whether we should write to the
448− -- foreground/background. I really don't want to know why.
449− sideCode = case side of
450− Foreground -> writeStr "38"
451− Background -> writeStr "48"
425+ writeColorDiff f (SetColor c)
426+ = writeCapExpr (f terminfoCaps) [toEnum $ colorMap c]
452427
453428-- | The color table used by a terminal is a 16 color set followed by a
454429-- 240 color set that might not be supported by the terminal.
455430--
456−-- This takes a Color which clearly identifies which palette to use and
457−-- computes the index into the full 256 color palette.
431+-- This takes a Color which clearly identifies which pallete to use and
432+-- computes the index into the full 256 color pallete.
458433ansiColorIndex :: Color -> Int
459434ansiColorIndex (ISOColor v) = fromEnum v
460435ansiColorIndex (Color240 v) = 16 + fromEnum v
461−ansiColorIndex (RGBColor _ _ _) =
462− error $ unlines [ "Attempted to create color index from rgb color."
463− , "This is currently unsupported, and shouldn't ever happen"
464− ]
465436
466437-- | For terminals without setaf/setab
467438--
@@ -478,10 +449,6 @@ altColorIndex (ISOColor 6) = 3
478449altColorIndex (ISOColor 7) = 7
479450altColorIndex (ISOColor v) = fromEnum v
480451altColorIndex (Color240 v) = 16 + fromEnum v
481−altColorIndex (RGBColor _ _ _) =
482− error $ unlines [ "Attempted to create color index from rgb color."
483− , "This is currently unsupported, and shouldn't ever happen"
484− ]
485452
486453{- | The sequence of terminfo caps to apply a given style are determined
487454 - according to these rules.