Skip to content

Commit

Permalink
Add a SetReversed SGR variant
Browse files Browse the repository at this point in the history
  • Loading branch information
emdash committed Jan 24, 2025
1 parent f3cac49 commit 511e626
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/Text/ANSI/SGR.idr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@ import Data.List

public export
data Color
= Black | Red | Green | Yellow | Blue | Magenta | Cyan | White
| BrightBlack | BrightRed | BrightGreen | BrightYellow | BrightBlue | BrightMagenta | BrightCyan | BrightWhite
= Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
| BrightBlack
| BrightRed
| BrightGreen
| BrightYellow
| BrightBlue
| BrightMagenta
| BrightCyan
| BrightWhite

Cast Color String where
cast Black = "0"
Expand All @@ -29,9 +43,15 @@ Cast Color String where

public export
data Style
= Bold | Faint | NotBoldOrFaint | Italic
| SingleUnderline | DoubleUnderline | NoUnderline
| Striked | NotStriked
= Bold
| Faint
| NotBoldOrFaint
| Italic
| SingleUnderline
| DoubleUnderline
| NoUnderline
| Striked
| NotStriked

Cast Style String where
cast Bold = "1"
Expand Down Expand Up @@ -59,15 +79,21 @@ data SGR
| SetBackground Color
| SetStyle Style
| SetBlink Blink
| SetReversed Bool

||| Returns the ANSI escape code equivalent to the list of operations provided.
export
escapeSGR : List SGR -> String
escapeSGR xs = "\x1B[" ++ concat (intersperse ";" (toCode <$> xs)) ++ "m"
escapeSGR xs = "\x1B[\{params}m"
where
toCode : SGR -> String
toCode Reset = "0"
toCode (SetForeground c) = "38;5;" ++ cast c
toCode (SetBackground c) = "48;5;" ++ cast c
toCode (SetStyle s) = cast s
toCode (SetBlink b) = cast b
toCode (SetReversed True) = "7"
toCode (SetReversed False) = "27"

params : String
params = fastConcat $ intersperse ";" $ toCode <$> xs

0 comments on commit 511e626

Please sign in to comment.