John Smith
John Smith

Reputation: 897

How might I output formatted text to the screen without having to write each time the relevant escape sequences?

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

Upvotes: 0

Views: 38

Answers (1)

Shawn
Shawn

Reputation: 52409

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)

Upvotes: 1

Related Questions