# Ansi colour output in drracket

**URL:** https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015
**Category:** Questions & Answers
**Created:** [November 19, 2025, 11:45am UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015 "2025-11-19T11:45:42Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![hendrikboom3](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)
#### Post date: [November 19, 2025, 11:45am UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/1 "2025-11-19T11:45:42Z")

</div>

I run the following in drracket:

```scheme
#lang racket/base
(require ansi-color)
(printf "plain text~n")
(with-colors 'red (lambda () (printf "red text~n")))
(with-colors 'green (lambda () (printf "green text~n")))

```

In the output window all the printf'd text appears in purple on a black background,, which are my usual default colours.  
None of it is red or green.  
The ansi colour escapes show up in purple too, introduced with a boxed hexadecimall character.

In case it is relevant, I use drracket in dark mode, and I have configured it to present parantheses in code in yellow -- so I can see these all-important charcters.

I use printf while debugging, and I'd like to use colour to emphasize milestones in printf trace output.

-- hendrik

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [November 20, 2025, 3:45pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/2 "2025-11-20T15:45:05Z")

</div>

Offhand, I don't think DrRacket supports ANSI escape sequences; I  
think `ansi-color` targets ANSI-compatible terminal devices.

---

<div class="post-metadata">

### Author: ![dominik.pantucek](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dominik.pantucek/32/144_2.png) [@dominik.pantucek](https://racket.discourse.group/u/dominik.pantucek)
#### Post date: [November 20, 2025, 5:00pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/3 "2025-11-20T17:00:33Z")

</div>

That is why I've implemented ANSI `pict` renderer for `uni-table`:

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/e/e92755aac5518ef2ca79259d696f3b565052dbcc.png)

See [Unicode Tables](https://docs.racket-lang.org/uni-table/index.html) and [5&nbsp;Pict Renderer](https://docs.racket-lang.org/uni-table/Pict_Renderer.html) for more information.

The core of the algorithm used is in [private/sgr-pict.rkt · master · Dominik Joe Pantůček / uni-table · GitLab](https://gitlab.com/racketeer/uni-table/-/blob/master/private/sgr-pict.rkt?ref_type=heads) and the routines to convert ANSI sequences to `sgr-list` as provided by [1&nbsp;Text Output](https://docs.racket-lang.org/uni-table/Text_Output.html#%28def._%28%28lib._uni-table%2Fprivate%2Fecma-sgr..rkt%29._ecma-csi-sgr-parse-lines%29%29) are in [private/ecma-sgr.rkt · master · Dominik Joe Pantůček / uni-table · GitLab](https://gitlab.com/racketeer/uni-table/-/blob/master/private/ecma-sgr.rkt?ref_type=heads)

Not perfect, but it is a start for using ANSI sequences in DrRacket.

---

<div class="post-metadata">

### Author: ![hendrikboom3](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)
#### Post date: [November 21, 2025, 10:39pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/4 "2025-11-21T22:39:35Z")

</div>

I've now checked that ansi-color works fine outside DrRacket when the standard output goes to an uxterm terminal. But not to DrRacket's output panel.

Dominik's pict hack looks useful, but not for my purpose. I'm looking for a way to write out printf-style debugging output in a visually distinctive manner, mixed in with all the other standard-output a program produces.

There has to be some mechanism for colour in DrRacket's outut panel, because DrRacket uses colour in its welcome and error messages.

---

<div class="post-metadata">

### Author: ![hendrikboom3](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)
#### Post date: [December 9, 2025, 1:14pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/5 "2025-12-09T13:14:26Z")

</div>

The DrRacket interactions panel indeed does not appear to recognise the ANSI colour escape sequences. So I'll give up on that tack, unless that gets certified as a bug.

But DrRacket itself uses colour on the interactions panel -- so there has to be a mechanism for it.

In particular, error messages show up in red, my own printf output shows up in purple, and prompts by the REPL show up i white. (I'm still using dark mode, so white is a reasonable colour choice.)

So there _is_ a way to affect colour in the interactions window, and it's probably not ANSI escape sequences.  
I'm just trying to find out how to do it myself.

-- hendrik

---

<div class="post-metadata">

### Author: ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)
#### Post date: [December 9, 2025, 2:00pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/6 "2025-12-09T14:00:11Z")

</div>

> [@hendrikboom3](#):
>
> So there _is_ a way to affect colour in the interactions window, and it's probably not ANSI escape sequences.  
> I'm just trying to find out how to do it myself.

Unfortunately, it is not really a general facility that's available to the programs running in the IDE. If you print to stdout, it comes out in purple. If you print to stderr, it comes out in red. To get the blue, you need to print to a secret port that you don't have access to.

If you want colored output, I think the best approach is to use another library somehow; perhaps one that makes picts or one that opens a separate window for your fancier output. There's not really a middle ground currently.

---

<div class="post-metadata">

### Author: ![joeld](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/joeld/32/38_2.png) [@joeld](https://racket.discourse.group/u/joeld)
#### Post date: [December 9, 2025, 2:09pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/7 "2025-12-09T14:09:20Z")

</div>

Dr Racket just colors REPL output based purely on the port it's coming from. So you can choose between two colors that way, but it would be hacky. (There's also [`drracket:rep:current-value-port`](https://docs.racket-lang.org/tools/drracket_rep.html#%28def._%28%28lib._drracket%2Ftool-lib..rkt%29._drracket~3arep~3acurrent-value-port%29%29) but I think you’ll find it’s difficult to use without causing weird DrRacket behavior.)

 ![CleanShot 2025-12-09 at 08.00.00@2x](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/2/2d382eb53a77dae01043dd09348535bea3790d9a.png)

---

<div class="post-metadata">

### Author: ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)
#### Post date: [December 9, 2025, 3:14pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/8 "2025-12-09T15:14:10Z")

</div>

Oh, @mflatt suggested off-list that one could use [string snips](https://docs.racket-lang.org/gui/string-snip_.html) and [styles](https://docs.racket-lang.org/gui/style___.html). I'm not sure this is a good approach as there are limitations (like I'm not seeing how to get the right font) but here is some code that does a thing:

```scheme
#lang racket
(require racket/snip)

(define (display-lines/color str color)
  (define sd (make-object style-delta%))
  (send sd set-delta-foreground color)
  (for ([str (string-split str "\n")])
    (define snip (make-object string-snip% str))
    (send snip set-style (send the-style-list find-or-create-style
                               (send the-style-list basic-style)
                               sd))
    (write-special snip)
    (display "\n")))

(display-lines/color "ab\ncd" "blue")
(display-lines/color "ab\ncd" "orange")
(display-lines/color "ab\ncd" "purple")
(display-lines/color "ab\ncd" "green")

```

---

<div class="post-metadata">

### Author: ![robby](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/robby/32/7_2.png) [@robby](https://racket.discourse.group/u/robby)
#### Post date: [December 9, 2025, 3:37pm UTC](https://racket.discourse.group/t/ansi-colour-output-in-drracket/4015/9 "2025-12-09T15:37:06Z")

</div>

Aha, here's how to get the right font.

```scheme
#lang racket
(require racket/snip framework)

(define (display-lines/color str color)
  (define sd (make-object style-delta%))
  (send sd set-delta-foreground color)
  (define base
    (send the-style-list new-named-style
          "Standard"
          (send the-style-list basic-style)))
  (define colored
    (send the-style-list find-or-create-style
          base
          sd))
  (for ([str (string-split str "\n")])
    (define snip (make-object string-snip% str))
    (send snip set-style colored)
    (write-special snip)
    (display "\n")))

(display-lines/color "ab\ncd" "blue")
(display-lines/color "ab\ncd" "orange")
(display-lines/color "ab\ncd" "purple")
(display-lines/color "ab\ncd" "green")

```
