chickadee » ansi-escape-sequences

ansi-escape-sequences

Description

Procedures to generate ANSI escape sequences.

Author

Mario Domenech Goulart. Erik Falor wrote the XTerm Enhanced Escape Sequences support.

Repository

https://github.com/mario-goulart/ansi-escape-sequences

Requirements

None

Procedures

cursor-position #!optional (line 0) (column 0)procedure

Move the cursor to the specified position (coordinates). If a position is not specified, the cursor is moved to the home position at the upper-left corner of the screen (line 0, column 0).

cursor-up linesprocedure

Move the cursor up by the specified number of lines without changing columns.

cursor-down linesprocedure

Move the cursor down by the specified number of lines without changing columns.

cursor-forward columnsprocedure

Move the cursor forward by the specified number of columns without changing lines.

cursor-backward columnsprocedure

Move the cursor back by the specified number of columns without changing lines.

save-cursor-positionprocedure

Save the current cursor position. You can move the cursor to the saved cursor position by using the restore-cursor-position procedure.

restore-cursor-positionprocedure

Return the cursor to the position stored by the save-cursor-position.

hide-cursorprocedure

Hide the cursor.

show-cursorprocedure

Show the cursor.

erase-displayprocedure

Clear the screen and move the cursor to the home position (line 0, column 0).

erase-lineprocedure

Clear all characters from the cursor position to the end of the line (including the character at the cursor position).

set-mode attribprocedure

Change the screen width or type to the mode specified by one of the following values (symbols):

  • 40x25-monochrome
  • 40x25-color
  • 80x25-monochrome
  • 80x25-color
  • 320x200-4-color
  • 320x200-monochrome
  • 640x200-monochrome
  • line-wrapping
  • 320x200-color
  • 640x200-color
  • 640x350-monochrome
  • 640x350-color
  • 640x480-monochrome
  • 640x480-color
  • 320x200-color
reset-mode attribprocedure

Reset the mode by using the same values as set-mode.

set-text attribs text #!optional (reset #t)procedure

Change the colors and attributes of text (such as bold and underline) displayed on the screen. The following attributes are available (symbols):

  • reset
  • bold
  • underscore
  • blink
  • reverse-video
  • concealed
  • fg-black
  • fg-red
  • fg-green
  • fg-yellow
  • fg-blue
  • fg-magenta
  • fg-cyan
  • fg-white
  • bg-black
  • bg-red
  • bg-green
  • bg-yellow
  • bg-blue
  • bg-magenta
  • bg-cyan
  • bg-white
  • fg-bright-black
  • fg-bright-red
  • fg-bright-green
  • fg-bright-yellow
  • fg-bright-blue
  • fg-bright-magenta
  • fg-bright-cyan
  • fg-bright-white
  • bg-bright-black
  • bg-bright-red
  • bg-bright-green
  • bg-bright-yellow
  • bg-bright-blue
  • bg-bright-magenta
  • bg-bright-cyan
  • bg-bright-white

bg- is for background. fg- is for foreground.

XTerm Enhanced Escape Sequences

The following procedures emit escape sequences first introduced by XTerm, but which have since been adopted by other terminals.

set-text256 attribs text #!optional (reset #t)procedure

Change the colors and attributes of text (such as bold and underline) displayed on the screen. The following attributes are available, the same as set-text (symbols):

  • reset
  • bold
  • underscore
  • blink
  • reverse-video
  • concealed

Colors are specified with a two-element list, the first element being one of the symbols foreground or background followed by a number in the range 0..255. Most terminals use the same color palette by default: colors 0-15 are the familiar 16 terminal colors. Colors 17-232 form a 6x6x6 color cube, with the remaining 24 colors defining a grayscale ramp from dark to light.

set-color256! index red green blueprocedure

Redefine the RGB value of color index. The values red, green, blue are integers in the range 0..255. Be aware that some terminals which will output 256 colors won't necessarily honor this escape sequence.

This egg's source is distributed with a CHICKEN port of the 256colors2.pl program used to test a terminal's ability to display colors beyond the standard 16.

set-title textprocedure

Sets the terminal's window title to text.

Examples

Here's a video (on YouTube) by Erik Falor showing an example of what you can do with the XTerm Enhanced Escape Sequences: https://www.youtube.com/watch?v=gTqHlKwziNU. The code for this example can be found here: http://paste.lisp.org/display/144140.

And here is a video showing the execution of the program below: OGV (Theora), AVI

(import ansi-escape-sequences (chicken port))

(set-buffering-mode! (current-output-port) #:none)

(display (save-cursor-position))
(for-each (lambda (letter)
            (display letter)
            (sleep 1)
            (cursor-forward 1))
          '("c" "h" "i" "c" "k" "e" "n"))

(display " ")
(for-each (lambda (letter)
            (display (set-text '(bg-black fg-yellow) letter))
            (sleep 1)
            (cursor-forward 1))
          '("r" "o" "c" "k" "s" "!"))

(display (restore-cursor-position))
(display (erase-line))

(for-each (lambda (letter)
            (display letter)
            (sleep 1)
            (cursor-forward 1))
          '("c" "h" "i" "c" "k" "e" "n"))

(display " ")
(for-each (lambda (letter)
            (display (set-text '(bg-red fg-white) letter))
            (sleep 1)
            (cursor-forward 1))
          '("r" "u" "l" "e" "s" "!"))

(print "")

License

 Copyright (c) 2010-2021, Mario Domenech Goulart
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. The name of the authors may not be used to endorse or promote products
    derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version history

Version 0.6 (2021-02-03)

Version 0.5

Version 0.4

Version 0.3

Version 0.2

Fixed escape sequences with more than one argument (reported by Felix)

Version 0.1

Initial release

Contents »