@@ -1,3 +1,4 @@
1+{-# LANGUAGE RecordWildCards #-}
12{-# LANGUAGE FlexibleInstances #-}
23{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE CPP #-}
@@ -14,7 +15,7 @@ module Graphics.Vty.Output.TerminfoBased
1415where
1516
1617import Control.Monad (when)
17−import Data.Bits (shiftL, (.&.))
18+import Data.Bits (shiftL)
1819import qualified Data.ByteString as BS
1920import Data.ByteString.Internal (toForeignPtr)
2021import Data.Terminfo.Parse
@@ -25,8 +26,9 @@ import Graphics.Vty.Image (DisplayRegion)
2526import Graphics.Vty.DisplayAttributes
2627import Graphics.Vty.Output.Interface
2728
28−import Blaze.ByteString.Builder (Write, writeToByteString, writeStorable, writeWord8)
29+import Blaze.ByteString.Builder (Write, writeToByteString, writeStorable)
2930
31+import Data.Bits ((.&.))
3032import Data.IORef
3133import Data.Maybe (isJust, isNothing, fromJust)
3234import Data.Word
@@ -49,6 +51,7 @@ data TerminfoCaps = TerminfoCaps
4951 , cup :: CapExpression
5052 , cnorm :: Maybe CapExpression
5153 , civis :: Maybe CapExpression
54+ , supportsNoColors :: Bool
5255 , useAltColorMap :: Bool
5356 , setForeColor :: CapExpression
5457 , setBackColor :: CapExpression
@@ -103,28 +106,28 @@ sendCapToTerminal t cap capParams = do
103106--
104107-- * Providing independent string capabilities for all display
105108-- attributes.
106−reserveTerminal :: String -> Fd -> ColorMode -> IO Output
107−reserveTerminal termName outFd colorMode = do
109+reserveTerminal :: String -> Fd -> IO Output
110+reserveTerminal termName outFd = do
108111 ti <- Terminfo.setupTerm termName
109112 -- assumes set foreground always implies set background exists.
110113 -- if set foreground is not set then all color changing style
111114 -- attributes are filtered.
112115 msetaf <- probeCap ti "setaf"
113116 msetf <- probeCap ti "setf"
114− let (useAlt, setForeCap)
117+ let (noColors, useAlt, setForeCap)
115118 = case msetaf of
116− Just setaf -> (False, setaf)
119+ Just setaf -> (False, False, setaf)
117120 Nothing -> case msetf of
118− Just setf -> (True, setf)
119− Nothing -> (True, error $ "no fore color support for terminal " ++ termName)
121+ Just setf -> (False, True, setf)
122+ Nothing -> (True, True, error $ "no fore color support for terminal " ++ termName)
120123 msetab <- probeCap ti "setab"
121124 msetb <- probeCap ti "setb"
122− let setBackCap
125+ let set_back_cap
123126 = case msetab of
124− Just setab -> setab
125127 Nothing -> case msetb of
126128 Just setb -> setb
127129 Nothing -> error $ "no back color support for terminal " ++ termName
130+ Just setab -> setab
128131
129132 hyperlinkModeStatus <- newIORef False
130133 newAssumedStateRef <- newIORef initialAssumedState
@@ -150,9 +153,10 @@ reserveTerminal termName outFd colorMode = do
150153 <*> requireCap ti "cup"
151154 <*> probeCap ti "cnorm"
152155 <*> probeCap ti "civis"
156+ <*> pure noColors
153157 <*> pure useAlt
154158 <*> pure setForeCap
155− <*> pure setBackCap
159+ <*> pure set_back_cap
156160 <*> requireCap ti "sgr0"
157161 <*> requireCap ti "clear"
158162 <*> requireCap ti "el"
@@ -192,12 +196,17 @@ reserveTerminal termName outFd colorMode = do
192196 when (toEnum len /= actualLen) $ fail $ "Graphics.Vty.Output: outputByteBuffer "
193197 ++ "length mismatch. " ++ show len ++ " /= " ++ show actualLen
194198 ++ " Please report this bug to vty project."
199+ , contextColorCount
200+ = case supportsNoColors terminfoCaps of
201+ False -> case Terminfo.getCapability ti (Terminfo.tiGetNum "colors" ) of
202+ Nothing -> 8
203+ Just v -> toEnum v
204+ True -> 1
195205 , supportsCursorVisibility = isJust $ civis terminfoCaps
196206 , supportsMode = terminfoModeSupported
197207 , setMode = terminfoSetMode
198208 , getModeStatus = terminfoModeStatus
199209 , assumedStateRef = newAssumedStateRef
200− , outputColorMode = colorMode
201210 -- I think fix would help assure tActual is the only
202211 -- reference. I was having issues tho.
203212 , mkDisplayContext = (`terminfoDisplayContext` terminfoCaps)
@@ -295,7 +304,7 @@ writeURLEscapes NoLinkChange =
295304-- | Portably setting the display attributes is a giant pain in the ass.
296305--
297306-- 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
307+-- state of each style directly ; and, for no good reason, resets the
299308-- colors to the default) this procedure is used:
300309--
301310-- 0. set the style attributes. This resets the fore and back color.
@@ -334,9 +343,9 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
334343 -- The only way to reset either color, portably, to the default
335344 -- is to use either the set state capability or the set default
336345 -- capability.
337− True -> do
346+ True -> do
338347 case reqDisplayCapSeqFor (displayAttrCaps terminfoCaps)
339− (fixedStyle attr)
348+ (fixedStyle attr )
340349 (styleToApplySeq $ fixedStyle attr) of
341350 -- only way to reset a color to the defaults
342351 EnterExitSeq caps -> writeDefaultAttr dc urlsEnabled
@@ -346,7 +355,8 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
346355 setColors
347356 -- implicitly resets the colors to the defaults
348357 SetState state -> writeCapExpr (fromJust $ setAttrStates
349− $ displayAttrCaps terminfoCaps
358+ $ displayAttrCaps
359+ $ terminfoCaps
350360 )
351361 (sgrArgsForState state)
352362 `mappend` setItalics
@@ -369,9 +379,9 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
369379 -- applied states.
370380 EnterExitSeq caps -> foldMap (\cap -> writeCapExpr cap []) caps
371381 `mappend`
372− writeColorDiff Foreground (foreColorDiff diffs)
382+ writeColorDiff setForeColor (foreColorDiff diffs)
373383 `mappend`
374− writeColorDiff Background (backColorDiff diffs)
384+ writeColorDiff setBackColor (backColorDiff diffs)
375385 -- implicitly resets the colors to the defaults
376386 SetState state -> writeCapExpr (fromJust $ setAttrStates
377387 $ displayAttrCaps terminfoCaps
@@ -403,65 +413,29 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
403413 | otherwise = mempty
404414 setColors =
405415 (case fixedForeColor attr of
406− Just c -> writeColor Foreground c
416+ Just c -> writeCapExpr (setForeColor terminfoCaps)
417+ [toEnum $ colorMap c]
407418 Nothing -> mempty)
408419 `mappend`
409420 (case fixedBackColor attr of
410− Just c -> writeColor Background c
421+ Just c -> writeCapExpr (setBackColor terminfoCaps)
422+ [toEnum $ colorMap c]
411423 Nothing -> mempty)
412− writeColorDiff _side NoColorChange
424+ writeColorDiff _f NoColorChange
413425 = mempty
414− writeColorDiff _side ColorToDefault
426+ writeColorDiff _f ColorToDefault
415427 = 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"
428+ writeColorDiff f (SetColor c)
429+ = writeCapExpr (f terminfoCaps) [toEnum $ colorMap c]
452430
453431-- | The color table used by a terminal is a 16 color set followed by a
454432-- 240 color set that might not be supported by the terminal.
455433--
456−-- This takes a Color which clearly identifies which palette to use and
457−-- computes the index into the full 256 color palette.
434+-- This takes a Color which clearly identifies which pallete to use and
435+-- computes the index into the full 256 color pallete.
458436ansiColorIndex :: Color -> Int
459437ansiColorIndex (ISOColor v) = fromEnum v
460438ansiColorIndex (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− ]
465439
466440-- | For terminals without setaf/setab
467441--
@@ -478,10 +452,6 @@ altColorIndex (ISOColor 6) = 3
478452altColorIndex (ISOColor 7) = 7
479453altColorIndex (ISOColor v) = fromEnum v
480454altColorIndex (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− ]
485455
486456{- | The sequence of terminfo caps to apply a given style are determined
487457 - according to these rules.