Skip to content

Commit

Permalink
feat: add stripAnsi to Terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed May 4, 2024
1 parent 9217f6b commit 85b074d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
3 changes: 3 additions & 0 deletions prelude/__internal__/IO.mad
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export red = Terminal.text.red
export green = Terminal.text.green
export yellow = Terminal.text.yellow
export grey = Terminal.text.white
export blue = Terminal.text.blue
export cyan = Terminal.text.cyan
export magenta = Terminal.text.magenta

#iftarget js

Expand Down
31 changes: 16 additions & 15 deletions prelude/__internal__/Terminal.mad
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import String from "String"



type HandlerId = HandlerId
export type HandlerId

Expand All @@ -24,7 +26,6 @@ export ansi = {
FGMagenta: "35",
FGCyan: "36",
FGWhite: "37",

FGBrightBlack: "90",
FGBrightRed: "91",
FGBrightGreen: "92",
Expand All @@ -33,7 +34,6 @@ export ansi = {
FGBrightMagenta: "95",
FGBrightCyan: "96",
FGBrightWhite: "97",

BGBlack: "40",
BGRed: "41",
BGGreen: "42",
Expand All @@ -42,7 +42,6 @@ export ansi = {
BGMagenta: "45",
BGCyan: "46",
BGWhite: "47",

BGBrightBlack: "100",
BGBrightRed: "101",
BGBrightGreen: "102",
Expand All @@ -51,12 +50,10 @@ export ansi = {
BGBrightMagenta: "105",
BGBrightCyan: "106",
BGBrightWhite: "107",

FormatUnderline: "4",
FormatNoUnderline: "24",
FormatBold: "1",
FormatNoBold: "21",

FormatInvert: "7",
}

Expand All @@ -69,9 +66,18 @@ export ansi = {
* ansiColor([ansi.FormatBold, ansi.FGBrightRed], "will be red and bold")
*/
ansiColor :: List String -> String -> String
export ansiColor = (parts, str) => colorize(
`\x1b[${String.join(";", parts)}m`,
str
export ansiColor = (parts, str) => colorize(`\x1b[${String.join(";", parts)}m`, str)

/**
* Removes ansi escape codes from a given string
* @since 0.23.13
* @example
* Terminal.stripAnsi(Terminal.text.red("hey")) == "hey"
*/
stripAnsi :: String -> String
export stripAnsi = String.replace(
`[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))`,
"",
)

export text = {
Expand Down Expand Up @@ -161,9 +167,7 @@ onWindowResizedFFI :: ({} -> {}) -> HandlerId
onWindowResizedFFI = extern "madlib__stdio__onWindowResized"

onWindowResized :: (#[Integer, Integer] -> {}) -> HandlerId
export onWindowResized = (cb) => onWindowResizedFFI(() => {
cb(getWindowSize())
})
export onWindowResized = (cb) => onWindowResizedFFI(() => { cb(getWindowSize()) })


clearWindowResizeHandler :: HandlerId -> {}
Expand Down Expand Up @@ -221,10 +225,7 @@ export clearKeyPressedHandler = (id) => #- {


getWindowSize :: {} -> #[Integer, Integer]
export getWindowSize = () => #[
#- process.stdout.columns -#,
#- process.stdout.rows -#,
]
export getWindowSize = () => #[#- process.stdout.columns -#, #- process.stdout.rows -#]


getTTYMode :: {} -> TTYMode
Expand Down
13 changes: 13 additions & 0 deletions prelude/__internal__/Terminal.spec.mad
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { assertEquals, test } from "Test"

import { stripAnsi, text } from "./Terminal"



test(
"stripAnsi",
() => do {
_ <- assertEquals(stripAnsi(text.red("colorful")), "colorful")
return assertEquals(stripAnsi("noChange"), "noChange")
},
)

0 comments on commit 85b074d

Please sign in to comment.