0

I am aware that I can display formatted text in Racket with display like so: (display "\33[3mSth in italic\33[m without any macro.\n") Much like I would do it in Bash:

ita='\e[3m'
end='\e[0m'

echo -e "${ita}This is in italics${end}."

or in Python:

italic = '\33[3m'
end = '\33[m'

print(f"{italic}This is in italic{end}")

Every time I create a new .sh or .py file the file gets prepended with the relevant shebang and the above boilerplate. This way I don't have to travel my poor memory the escape sequences as the process get automated away? How might I accomplish the same in Racket? I've tried with macros and format:

#!/usr/bin/env racket
#lang racket

(define-syntax ita
    (lambda (stx)
      (syntax "\33[3m")))
(define-syntax fi
    (lambda (stx)
      (syntax "\33[m")))
      
(format "In ~aitalic~a without macro." "\33[3m" "\33[m")
(format "In ~aitalic~a with macro." ita fi)

but I am unable to obtain the desired result. I would like to solve this within Racket. Let other solutions, e.g. modifying the file with sed, be outside of the scope of this question

1 Answer 1

1

Racket has a few libraries for colorizing text with ANSI escape sequences. colorize is a decently full featured one:

#lang racket
(require colorize)

(display "Plain")
(display (colorize "Italic" 'default #:style 'italic))
(display "Plain")
(newline)
Sign up to request clarification or add additional context in comments.

1 Comment

Note that the package has to be installed with the DrRacket package manger or from the command line with raco pkg install colorize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.