@@ -1,3 +1,4 @@
1+{-# LANGUAGE RecordWildCards #-}
1{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE CPP #-}
4{-# LANGUAGE CPP #-}
@@ -14,7 +15,7 @@ module Graphics.Vty.Output.TerminfoBased
16import Control.Monad (when)
17import Control.Monad (when)
17−import Data.Bits (shiftL, (.&.))
18+import Data.Bits (shiftL)
18import qualified Data.ByteString as BS
19import qualified Data.ByteString as BS
19import Data.ByteString.Internal (toForeignPtr)
20import Data.ByteString.Internal (toForeignPtr)
20import Data.Terminfo.Parse
21import Data.Terminfo.Parse
@@ -25,8 +26,9 @@ import Graphics.Vty.Image (DisplayRegion)
25import Graphics.Vty.DisplayAttributes
26import Graphics.Vty.DisplayAttributes
26import Graphics.Vty.Output.Interface
27import Graphics.Vty.Output.Interface
28−import Blaze.ByteString.Builder (Write, writeToByteString, writeStorable, writeWord8)
29+import Blaze.ByteString.Builder (Write, writeToByteString, writeStorable)
31+import Data.Bits ((.&.))
30import Data.IORef
32import Data.IORef
31import Data.Maybe (isJust, isNothing, fromJust)
33import Data.Maybe (isJust, isNothing, fromJust)
32import Data.Word
34import Data.Word
@@ -49,6 +51,7 @@ data TerminfoCaps = TerminfoCaps
49 , cup :: CapExpression
51 , cup :: CapExpression
50 , cnorm :: Maybe CapExpression
52 , cnorm :: Maybe CapExpression
51 , civis :: Maybe CapExpression
53 , civis :: Maybe CapExpression
54+ , supportsNoColors :: Bool
52 , useAltColorMap :: Bool
55 , useAltColorMap :: Bool
53 , setForeColor :: CapExpression
56 , setForeColor :: CapExpression
54 , setBackColor :: CapExpression
57 , setBackColor :: CapExpression
@@ -103,28 +106,28 @@ sendCapToTerminal t cap capParams = do
104-- * Providing independent string capabilities for all display
107-- * Providing independent string capabilities for all display
105-- attributes.
108-- attributes.
106−reserveTerminal :: String -> Fd -> ColorMode -> IO Output
109+reserveTerminal :: String -> Fd -> IO Output
107−reserveTerminal termName outFd colorMode = do
110+reserveTerminal termName outFd = do
108 ti <- Terminfo.setupTerm termName
111 ti <- Terminfo.setupTerm termName
109 -- assumes set foreground always implies set background exists.
112 -- assumes set foreground always implies set background exists.
110 -- if set foreground is not set then all color changing style
113 -- if set foreground is not set then all color changing style
111 -- attributes are filtered.
114 -- attributes are filtered.
112 msetaf <- probeCap ti "setaf"
115 msetaf <- probeCap ti "setaf"
113 msetf <- probeCap ti "setf"
116 msetf <- probeCap ti "setf"
114− let (useAlt, setForeCap)
117+ let (noColors, useAlt, setForeCap)
115 = case msetaf of
118 = case msetaf of
116− Just setaf -> (False, setaf)
119+ Just setaf -> (False, False, setaf)
117 Nothing -> case msetf of
120 Nothing -> case msetf of
118− Just setf -> (True, setf)
121+ Just setf -> (False, True, setf)
119− Nothing -> (True, error $ "no fore color support for terminal " ++ termName)
122+ Nothing -> (True, True, error $ "no fore color support for terminal " ++ termName)
120 msetab <- probeCap ti "setab"
123 msetab <- probeCap ti "setab"
121 msetb <- probeCap ti "setb"
124 msetb <- probeCap ti "setb"
122− let setBackCap
125+ let set_back_cap
123 = case msetab of
126 = case msetab of
125 Nothing -> case msetb of
127 Nothing -> case msetb of
126 Just setb -> setb
128 Just setb -> setb
127 Nothing -> error $ "no back color support for terminal " ++ termName
129 Nothing -> error $ "no back color support for terminal " ++ termName
129 hyperlinkModeStatus <- newIORef False
132 hyperlinkModeStatus <- newIORef False
130 newAssumedStateRef <- newIORef initialAssumedState
133 newAssumedStateRef <- newIORef initialAssumedState
@@ -150,9 +153,10 @@ reserveTerminal termName outFd colorMode = do
150 <*> requireCap ti "cup"
153 <*> requireCap ti "cup"
151 <*> probeCap ti "cnorm"
154 <*> probeCap ti "cnorm"
152 <*> probeCap ti "civis"
155 <*> probeCap ti "civis"
153 <*> pure useAlt
157 <*> pure useAlt
154 <*> pure setForeCap
158 <*> pure setForeCap
155− <*> pure setBackCap
159+ <*> pure set_back_cap
156 <*> requireCap ti "sgr0"
160 <*> requireCap ti "sgr0"
157 <*> requireCap ti "clear"
161 <*> requireCap ti "clear"
158 <*> requireCap ti "el"
162 <*> requireCap ti "el"
@@ -164,10 +168,6 @@ reserveTerminal termName outFd colorMode = do
164 sendCap setDefaultAttr []
168 sendCap setDefaultAttr []
165 maybeSendCap cnorm []
169 maybeSendCap cnorm []
166 , supportsBell = return $ isJust $ ringBellAudio terminfoCaps
170 , supportsBell = return $ isJust $ ringBellAudio terminfoCaps
167− , supportsItalics = return $ (isJust $ enterItalic (displayAttrCaps terminfoCaps)) &&
168− (isJust $ exitItalic (displayAttrCaps terminfoCaps))
169− , supportsStrikethrough = return $ (isJust $ enterStrikethrough (displayAttrCaps terminfoCaps)) &&
170− (isJust $ exitStrikethrough (displayAttrCaps terminfoCaps))
171 , ringTerminalBell = maybeSendCap ringBellAudio []
171 , ringTerminalBell = maybeSendCap ringBellAudio []
172 , reserveDisplay = do
172 , reserveDisplay = do
173 -- If there is no support for smcup: Clear the screen
173 -- If there is no support for smcup: Clear the screen
@@ -192,15 +192,20 @@ reserveTerminal termName outFd colorMode = do
192 when (toEnum len /= actualLen) $ fail $ "Graphics.Vty.Output: outputByteBuffer "
192 when (toEnum len /= actualLen) $ fail $ "Graphics.Vty.Output: outputByteBuffer "
193 ++ "length mismatch. " ++ show len ++ " /= " ++ show actualLen
193 ++ "length mismatch. " ++ show len ++ " /= " ++ show actualLen
194 ++ " Please report this bug to vty project."
194 ++ " Please report this bug to vty project."
196+ = case supportsNoColors terminfoCaps of
197+ False -> case Terminfo.getCapability ti (Terminfo.tiGetNum "colors" ) of
195 , supportsCursorVisibility = isJust $ civis terminfoCaps
201 , supportsCursorVisibility = isJust $ civis terminfoCaps
196 , supportsMode = terminfoModeSupported
202 , supportsMode = terminfoModeSupported
197 , setMode = terminfoSetMode
203 , setMode = terminfoSetMode
198 , getModeStatus = terminfoModeStatus
204 , getModeStatus = terminfoModeStatus
199 , assumedStateRef = newAssumedStateRef
205 , assumedStateRef = newAssumedStateRef
200− , outputColorMode = colorMode
201 -- I think fix would help assure tActual is the only
206 -- I think fix would help assure tActual is the only
202 -- reference. I was having issues tho.
207 -- reference. I was having issues tho.
203− , mkDisplayContext = (`terminfoDisplayContext` terminfoCaps)
208+ , mkDisplayContext = \tActual -> terminfoDisplayContext tActual terminfoCaps
205 sendCap s = sendCapToTerminal t (s terminfoCaps)
210 sendCap s = sendCapToTerminal t (s terminfoCaps)
206 maybeSendCap s = when (isJust $ s terminfoCaps) . sendCap (fromJust . s)
211 maybeSendCap s = when (isJust $ s terminfoCaps) . sendCap (fromJust . s)
@@ -295,7 +300,7 @@ writeURLEscapes NoLinkChange =
295-- | Portably setting the display attributes is a giant pain in the ass.
300-- | Portably setting the display attributes is a giant pain in the ass.
297-- If the terminal supports the sgr capability (which sets the on/off
302-- 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
303+-- state of each style directly ; and, for no good reason, resets the
299-- colors to the default) this procedure is used:
304-- colors to the default) this procedure is used:
301-- 0. set the style attributes. This resets the fore and back color.
306-- 0. set the style attributes. This resets the fore and back color.
@@ -334,9 +339,9 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
334 -- The only way to reset either color, portably, to the default
339 -- The only way to reset either color, portably, to the default
335 -- is to use either the set state capability or the set default
340 -- is to use either the set state capability or the set default
336 -- capability.
341 -- capability.
337− True -> do
342+ True -> do
338 case reqDisplayCapSeqFor (displayAttrCaps terminfoCaps)
343 case reqDisplayCapSeqFor (displayAttrCaps terminfoCaps)
339− (fixedStyle attr)
344+ (fixedStyle attr )
340 (styleToApplySeq $ fixedStyle attr) of
345 (styleToApplySeq $ fixedStyle attr) of
341 -- only way to reset a color to the defaults
346 -- only way to reset a color to the defaults
342 EnterExitSeq caps -> writeDefaultAttr dc urlsEnabled
347 EnterExitSeq caps -> writeDefaultAttr dc urlsEnabled
@@ -346,7 +351,8 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
346 setColors
351 setColors
347 -- implicitly resets the colors to the defaults
352 -- implicitly resets the colors to the defaults
348 SetState state -> writeCapExpr (fromJust $ setAttrStates
353 SetState state -> writeCapExpr (fromJust $ setAttrStates
349− $ displayAttrCaps terminfoCaps
354+ $ displayAttrCaps
351 (sgrArgsForState state)
357 (sgrArgsForState state)
352 `mappend` setItalics
358 `mappend` setItalics
@@ -369,9 +375,9 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
369 -- applied states.
375 -- applied states.
370 EnterExitSeq caps -> foldMap (\cap -> writeCapExpr cap []) caps
376 EnterExitSeq caps -> foldMap (\cap -> writeCapExpr cap []) caps
371 `mappend`
377 `mappend`
372− writeColorDiff Foreground (foreColorDiff diffs)
378+ writeColorDiff setForeColor (foreColorDiff diffs)
373 `mappend`
379 `mappend`
374− writeColorDiff Background (backColorDiff diffs)
380+ writeColorDiff setBackColor (backColorDiff diffs)
375 -- implicitly resets the colors to the defaults
381 -- implicitly resets the colors to the defaults
376 SetState state -> writeCapExpr (fromJust $ setAttrStates
382 SetState state -> writeCapExpr (fromJust $ setAttrStates
377 $ displayAttrCaps terminfoCaps
383 $ displayAttrCaps terminfoCaps
@@ -403,65 +409,29 @@ terminfoWriteSetAttr dc terminfoCaps urlsEnabled prevAttr reqAttr diffs =
403 | otherwise = mempty
409 | otherwise = mempty
404 setColors =
410 setColors =
405 (case fixedForeColor attr of
411 (case fixedForeColor attr of
406− Just c -> writeColor Foreground c
412+ Just c -> writeCapExpr (setForeColor terminfoCaps)
413+ [toEnum $ colorMap c]
407 Nothing -> mempty)
414 Nothing -> mempty)
408 `mappend`
415 `mappend`
409 (case fixedBackColor attr of
416 (case fixedBackColor attr of
410− Just c -> writeColor Background c
417+ Just c -> writeCapExpr (setBackColor terminfoCaps)
418+ [toEnum $ colorMap c]
411 Nothing -> mempty)
419 Nothing -> mempty)
412− writeColorDiff _side NoColorChange
420+ writeColorDiff _f NoColorChange
414− writeColorDiff _side ColorToDefault
422+ writeColorDiff _f ColorToDefault
415 = error "ColorToDefault is not a possible case for applyColorDiffs"
423 = error "ColorToDefault is not a possible case for applyColorDiffs"
416− writeColorDiff side (SetColor c)
424+ writeColorDiff f (SetColor c)
419− writeColor side (RGBColor r g b) =
420− case outputColorMode (contextDevice dc) of
422− hardcodeColor side (r, g, b)
424− error "clampColor should remove rgb colors in standard mode"
426− writeCapExpr (setSideColor side terminfoCaps) [toEnum $ colorMap c]
425+ = writeCapExpr (f terminfoCaps) [toEnum $ colorMap c]
428−-- a color can either be in the foreground or the background
429−data ColorSide = Foreground | Background
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
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
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"
453-- | The color table used by a terminal is a 16 color set followed by a
427-- | The color table used by a terminal is a 16 color set followed by a
454-- 240 color set that might not be supported by the terminal.
428-- 240 color set that might not be supported by the terminal.
456−-- This takes a Color which clearly identifies which palette to use and
430+-- This takes a Color which clearly identifies which pallete to use and
457−-- computes the index into the full 256 color palette.
431+-- computes the index into the full 256 color pallete.
458ansiColorIndex :: Color -> Int
432ansiColorIndex :: Color -> Int
459ansiColorIndex (ISOColor v) = fromEnum v
433ansiColorIndex (ISOColor v) = fromEnum v
460ansiColorIndex (Color240 v) = 16 + fromEnum v
434ansiColorIndex (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"
466-- | For terminals without setaf/setab
436-- | For terminals without setaf/setab
@@ -478,10 +448,6 @@ altColorIndex (ISOColor 6) = 3
478altColorIndex (ISOColor 7) = 7
448altColorIndex (ISOColor 7) = 7
479altColorIndex (ISOColor v) = fromEnum v
449altColorIndex (ISOColor v) = fromEnum v
480altColorIndex (Color240 v) = 16 + fromEnum v
450altColorIndex (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"
486{- | The sequence of terminfo caps to apply a given style are determined
452{- | The sequence of terminfo caps to apply a given style are determined
487 - according to these rules.
453 - according to these rules.