implements all control functions of ECMA-48

This commit is contained in:
Frank Zechert 2023-03-31 22:57:18 +02:00
commit 2791de95c9
10 changed files with 3546 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/Cargo.lock

16
Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "ansi"
description = "This library contains all ANSI Escape Codes that are defined in the ISO 6429 Standard"
version = "0.1.0"
edition = "2021"
authors = ["Frank Zechert <rust.frank@zechert.net>"]
repository = ""
license = "MIT"
# crates.io
publish = false
keywords = ["ansi", "escape codes", "ISO 6429", "ECMA 48", "ANSI X3.64"]
categories = ["command-line-interface"]
[dependencies]

21
LICENSE.txt Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Frank Zechert
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

330
src/c0.rs Normal file
View File

@ -0,0 +1,330 @@
//! Elements of the C0 set.
//!
//! These control functions are represented in 7-bit codes by bit combinations from `00/00` to `01/15`.
//!
//! The 3-character escape sequence designating and invoking this C0 set is `ESC 02/01 04/00`,
//! see [`ANNOUNCER_SEQUENCE`].
//!
//! It is assumed that even with no invoked C0 set, the control character `ESCAPE` (`ESC`) is always available, and is
//! represented by bit combination `01/00`.
//!
//! ## Overview of the C0 Set
//!
//! | Row Number | Column `00` | Column `01` |
//! | ---------: | :---------: | :---------: |
//! | `00` | [`NUL`] | [`DLE`] |
//! | `01` | [`SOH`] | [`DC1`] |
//! | `02` | [`STX`] | [`DC2`] |
//! | `03` | [`ETX`] | [`DC3`] |
//! | `04` | [`EOT`] | [`DC4`] |
//! | `05` | [`ENQ`] | [`NAK`] |
//! | `06` | [`ACK`] | [`SYN`] |
//! | `07` | [`BEL`] | [`ETB`] |
//! | `08` | [`BS`] | [`CAN`] |
//! | `09` | [`HT`] | [`EM`] |
//! | `10` | [`LF`] | [`SUB`] |
//! | `11` | [`VT`] | [`ESC`] |
//! | `12` | [`FF`] | [`IS4`] |
//! | `13` | [`CR`] | [`IS3`] |
//! | `14` | [`SO`] | [`IS2`] |
//! | `15` | [`SI`] | [`IS1`] |
use crate::ControlFunction;
macro_rules! c0 {
($xx:literal/$yy:literal) => {
ControlFunction::new_c0(ascii!($xx / $yy))
};
}
/// Announcer Sequence for C0.
///
/// Designate the C0 set of control functions as the active set of control functions.
///
/// ## Note 1
///
/// The use of this escape sequence implies that all control function of this C0 set must be implemented.
///
/// ## Note 2
///
/// It is assumed that even with no invoked C0 set, the control character `ESCAPE` (`ESC`) is available, and is
/// represented by the bit combination `01/11`.
pub const ANNOUNCER_SEQUENCE: &'static str = ascii!(01 / 11, 02 / 01, 04 / 00);
/// Acknowledge.
///
/// `ACK` is transmitted by a receiver as an affirmative response to the sender.
///
/// The use of `ACK` is defined in ISO 1745.
pub const ACK: ControlFunction = c0!(00 / 06);
/// Bell.
///
/// `BEL` is used when there is a need to call for attention; it may control alarm or attention devices.
pub const BEL: ControlFunction = c0!(00 / 07);
/// Backspace.
///
/// `BS` causes the active data position to be moved one character position in the data component in the direction
/// opposite to that of the implicit movement.
///
/// The direction of the implicit movement depends on the parameter value of Select Implicit Movement Direction
/// (`SIMD`).
pub const BS: ControlFunction = c0!(00 / 08);
/// Cancel.
///
/// `CAN` is used to indicate that the data preceding it in the data stream is in error. As a result, this data shall be
/// ignored. The specific meaning of this control function shall be defined for each application and/or between sender
/// and recipient.
pub const CAN: ControlFunction = c0!(01 / 08);
/// Carriage Return.
///
/// The effect of `CR` depends on the setting of the DEVICE COMPONENT SELECT MODE (`DCSM`) and on the parameter value of
/// SELECT IMPLICIT MOVEMENT DIRECTION (`SIMD`).
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to PRESENTATION and with the parameter value of `SIMD` equal to
/// `0`, `CR` causes the active presentation position to be moved to the line home position of the same line in the
/// presentation component. The line home position is established by the parameter value of SET LINE HOME (`SLH`).
///
/// With a parameter value of `SIMD` equal to `1`, `CR` causes the active presentation position to be moved to the line
/// limit position of the same line in the presentation component. The line limit position is established by the
/// parameter value of SET LINE LIMIT (`SSL`).
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to DATA and with a parameter of `SIMD` equal to `0`, `CR` causes
/// the active data position to be moved to the line home position of the same line in the data component. The line home
/// position is established by the parameter value of SET LINE HOME (`SLH`).
///
/// With a parameter value of `SIMD` equal to `1`, `CR` causes the active data position to be moved to the line limit
/// position of the same line in the data component. The line limit position is established by the parameter value of
/// SET LINE LIMIT (`SSL`).
pub const CR: ControlFunction = c0!(00 / 13);
/// Device Control One.
///
/// `DC1` is primarily intended for tuning on or starting an ancillary device. If it is not required for this purpose,
/// it may be used to restore a device to the basic mode of operation (see also [`DC2`] and [`DC3`]), or any other
/// device control function not provided by other DCs.
///
/// ## Note
///
/// When used for data flow control, `DC1` is sometimes called `X-ON`.
pub const DC1: ControlFunction = c0!(01 / 01);
/// Device Control Two.
///
/// `DC2` is primarily intended for tuning on or starting an ancillary device. If it is not required for this purpose,
/// it may be used to set a device to a special mode of operation (in which case [`DC1`] is used to restore the device
/// to the basic mode), or for any other device control function not provided by other DCs.
pub const DC2: ControlFunction = c0!(01 / 02);
/// Device Control Three.
///
/// `DC3` is primarily intended for turning off or stopping an ancillary device. This function may be a secondary level
/// stop, for example wait, pause, stand-by or halt (in which case [`DC1`] is used to restore normal operation). If it
/// is not required for this purpose, it may be used for any other device control function not provided by other DCs.
pub const DC3: ControlFunction = c0!(01 / 03);
/// Device Control Four.
///
/// `DC4` is primarily intended for turning off, stopping or interrupting an ancillary device. If it is not required for
/// this purpose, it may be used for any other device control function not provided by other DCs.
pub const DC4: ControlFunction = c0!(01 / 04);
/// Data Link Escape.
///
/// `DLE` is used exclusively to provide supplementary transmission control functions.
///
/// The use of `DLE` is defined in ISO 1745.
pub const DLE: ControlFunction = c0!(01 / 00);
/// End Of Medium.
///
/// `EM` is used to identify the physical end of a medium, or the end of the used portion of a medium, or the end of the
/// wanted portion of data recorded on a medium.
pub const EM: ControlFunction = c0!(01 / 09);
/// Enquiry.
///
/// `ENQ` is transmitted by a sender as a request for a response from a receiver.
///
/// The use of `EOT` is defined in ISO 1745.
pub const ENQ: ControlFunction = c0!(00 / 05);
/// End Of Transmission.
///
/// `EOT` is used to indicate the conclusion of the transmission of one or more texts.
///
/// The use of `EOT` is defined in ISO 1745.
pub const EOT: ControlFunction = c0!(00 / 04);
/// Escape.
///
/// `ESC` is used for code extension purposes. It causes the meanings of a limited number of bit combinations following
/// it in the data stream to be changed.
///
/// The use of `ESC` is defined in Standard ECMA-35.
pub const ESC: ControlFunction = c0!(01 / 11);
/// End Of Transmission Block.
///
/// `ETB` is used to indicate the end of a block of data where the data are divided into such blocks for transmission
/// purposes.
///
/// The use of `ETB` is defined in ISO 1745.
pub const ETB: ControlFunction = c0!(01 / 07);
/// End Of Text.
///
/// `ETX` is used to indicate the end of a text.
///
/// The use of `ETX` is defined in ISO 1745.
pub const ETX: ControlFunction = c0!(00 / 03);
/// Form Feed.
///
/// `FF` causes the active presentation position to be moved to the corresponding character position of the line at the
/// page home position of the next form or page in the presentation component. The page home position is established by
/// the parameter value of SET PAGE HOME (`SPH`).
pub const FF: ControlFunction = c0!(00 / 12);
/// Character Tabulation.
///
/// `HT` causes the active presentation position to be moved to the following character tabulation stop in the
/// presentation component.
///
/// In addition, if that following character tabulation stop has been set by TABULATION ALIGN CENTRE (`TAC`), TABULATION
/// ALIGN LEADING EDGE (`TALE`), TABULATION ALIGN TRAILING EDGE (`TATE`) or TABULATION CENTERED ON CHARACTER (`TCC`),
/// `HT` indicates the beginning of a string of text which is to be positioned within a line according to the properties
/// of that tabulation stop. The end of the string is indicated by the next occurrence of `HT` or CARRIAGE RETURN
/// ([`CR`]) or NEXT LINE ([`NEL`][crate::c1::NEL]) in the data stream.
pub const HT: ControlFunction = c0!(00 / 09);
/// Information Separator One (US - Unit Separator).
///
/// `IS1` is used to separate and qualify data logically; its specific meaning has to be defined for each application.
/// If this control function is used in hierarchical order, it may delimit a data item called a unit.
pub const IS1: ControlFunction = c0!(01 / 15);
/// Information Separator Two (RS - Record Separator).
///
/// `IS2` is used to separate and qualify data logically; its specific meaning has to be defined for each application.
/// If this control function is used in hierarchical order, it may delimit a data item called a record.
pub const IS2: ControlFunction = c0!(01 / 14);
/// Information Separator Three (GS - Group Separator).
///
/// `IS3` is used to separate and qualify data logically; its specific meaning has to be defined for each application.
/// If this control function is used in hierarchical order, it may delimit a data item called a group.
pub const IS3: ControlFunction = c0!(01 / 13);
/// Information Separator Four (FS - File Separator).
///
/// `IS4` is used to separate and qualify data logically; its specific meaning has to be defined for each application.
/// If this control function is used in hierarchical order, it may delimit a data item called a file.
pub const IS4: ControlFunction = c0!(01 / 12);
/// Line Feed.
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to PRESENTATION, `LF` causes the active presentation position
/// to be moved to the corresponding character position of the following line in the presentation component.
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to DATA, `LF` causes the active data position to be moved to the
/// corresponding character position of the following line in the data component.
pub const LF: ControlFunction = c0!(00 / 10);
/// Locking-Shift Zero.
///
/// `LS0` is used for code extension purposes. It causes the meanings of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `LS0` is defined in Standard ECMA-35.
///
/// ## Note
///
/// `LS0` is used in 8-bit environments only; in 7-bit environments SHIFT-IN ([`SI`]) is used instead.
pub const LS0: ControlFunction = c0!(00 / 15);
/// Locking-Shift One.
///
/// `LS1` is used for code extension purposes. It causes the meanings of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `LS1` is defined in Standard ECMA-35.
///
/// ## Note
///
/// `LS1` is used in 8-bit environments only; in 7-bit environments SHIFT-OUT ([`SO`]) is used instead.
pub const LS1: ControlFunction = c0!(00 / 14);
/// Negative Acknowledge.
///
/// `NAK` is transmitted by a receiver as a negative response to the sender.
///
/// The use of `NAK` is defined in ISO 1745.
pub const NAK: ControlFunction = c0!(01 / 05);
/// Null.
///
/// `NUL` is used for media-fill or time-fill. `NUL` characters may be inserted into, or removed from, a data stream
/// without affecting information content of that stream, but such action may affect the information layout and/or the
/// control of equipment.
pub const NUL: ControlFunction = c0!(00 / 00);
/// Shift-In.
///
/// `SI` is used for code extension purposes. It causes the meanings of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `SI` is defined in Standard ECMA-35.
///
/// ## Note
///
/// `SI` is used in 7-bit environments only; in 8-bit environments LOCKING-SHIFT ZERO (`LS0`) is used instead.
pub const SI: ControlFunction = c0!(00 / 15);
/// Shift-Out.
///
/// `SO` is used for code extension purposes. It causes the meanings of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `SI` is defined in Standard ECMA-35.
///
/// ## Note
///
/// `SO` is used in 7-bit environments only; in 8-bit environments LOCKING-SHIFT ONE (`LS1`) is used instead.
pub const SO: ControlFunction = c0!(00 / 14);
/// Start of Heading.
///
/// `SOH` is used to indicate the beginning of a heading.
///
/// The use of `SOH` is defined in ISO 1745.
pub const SOH: ControlFunction = c0!(00 / 01);
/// Start of Text.
///
/// `STX` is used to indicate the beginning of a text and the ned of a heading.
///
/// The use of `STX` is defined in ISO 1745.
pub const STX: ControlFunction = c0!(00 / 02);
/// Substitute.
///
/// `SUB` is used in the place of a character that has been found to be invalid or in error. `SUB` is intended to be
/// introduced by automatic means.
pub const SUB: ControlFunction = c0!(01 / 10);
/// Synchronous Idle.
///
/// `SYN` is used by a synchronous transmission system in the absence of any other character (idle condition) to provide
/// a signal from which synchronism may be achieved or retained between data terminal equipment.
///
/// The use of `SYN` is defined in ISO 1745.
pub const SYN: ControlFunction = c0!(01 / 06);
/// Line Tabulation.
///
/// `VT` causes the active presentation position to be moved in the presentation component to the corresponding
/// character position on the line at which the following line tabulation stop is set.
pub const VT: ControlFunction = c0!(00 / 11);

319
src/c1.rs Normal file
View File

@ -0,0 +1,319 @@
//! Elements of the C1 set.
//!
//! These control functions are represented in 7-bit codes by escape sequences of the form `ESC Fe`, where `ESC` is
//! represented by bit combination `01/11` and `Fe` is represented by a bit combination from `04/00` to `05/15`.
//!
//! The unallocated bit combinations are reserved for future standardization and shall not be used.
//!
//! The 3-character escape sequence designating and invoking this c1 set is `ESC 02/06 04/00` and `ESC 02/02 F`.
//! see [`ANNOUNCER_SEQUENCE`], and [`ALTERNATIVE_ANNOUNCER_SEQUENCE`].
//!
//! ## Overview of the C1 Set
//!
//! | Row Number | Column `04` | Column `05` |
//! | ---------: | :---------: | :---------: |
//! | `00` | -- | [`DCS`] |
//! | `01` | -- | [`PU1`] |
//! | `02` | [`BPH`] | [`PU2`] |
//! | `03` | [`NBH`] | [`STS`] |
//! | `04` | -- | [`CCH`] |
//! | `05` | [`NEL`] | [`MW`] |
//! | `06` | [`SSA`] | [`SPA`] |
//! | `07` | [`ESA`] | [`EPA`] |
//! | `08` | [`HTS`] | [`SOS`] |
//! | `09` | [`HTJ`] | -- |
//! | `10` | [`VTS`] | [`SCI`] |
//! | `11` | [`PLD`] | [`CSI`] |
//! | `12` | [`PLU`] | [`ST`] |
//! | `13` | [`RI`] | [`OSC`] |
//! | `14` | [`SS2`] | [`PM`] |
//! | `15` | [`SS3`] | [`APC`] |
use crate::ControlFunction;
macro_rules! c1 {
($xx:literal/$yy:literal) => {
ControlFunction::new_c1(ascii!($xx / $yy))
};
}
/// Announcer Sequence for C1.
///
/// Designate the C1 set of control functions as the active set of control functions.
///
/// ## Note
///
/// The use of this escape sequence implies that all control function of this c1 set must be implemented.
pub const ANNOUNCER_SEQUENCE: &'static str = ascii!(01 / 11, 02 / 06, 04 / 00);
/// Alternative Announcer Sequence for C1.
///
/// Designate the C1 set of control functions as the active set of control functions.
///
/// ## Note
///
/// The use of this escape sequence implies that all control function of this c1 set must be implemented.
pub const ALTERNATIVE_ANNOUNCER_SEQUENCE: &'static str = ascii!(01 / 11, 02 / 02, 04 / 06);
/// Application Program Command.
///
/// `APC` is used as the opening delimiter of a control string for application program use. The command string following
/// may consist of bit combinations in the range `00/08` to `00/13` and `02/00` to `07/14`. The control string is closed
/// by the terminating delimiter String Terminator (`ST`). The interpretation of the command string depends on the
/// relevant application program.
pub const APC: ControlFunction = c1!(05 / 15);
/// Break Permitted Here.
///
/// `BPH` is used to indicate a point where a line break may occur when text is formatted. `BPH` may occur between two
/// graphic characters, either or both of which may be `SPACE`.
pub const BPH: ControlFunction = c1!(04 / 02);
/// Cancel Character.
///
/// `CCH` is used to indicate that both the preceding graphic character in the data stream (represented by one or more
/// bit combinations), including `SPACE`, and the control function `CCH` itself are to be ignored for further
/// interpretation of the data stream.
///
/// If the character preceding `CCH` in the data stream is a control function (represented by one or more bit
/// combinations), the effect of `CCH` is not defined.
pub const CCH: ControlFunction = c1!(05 / 04);
/// Control Sequence Introducer.
///
/// `CSI` is used as the first character of a control sequence. See [control_sequences][crate::control_sequences].
pub const CSI: ControlFunction = c1!(05 / 11);
/// Device Control String.
///
/// `DCS` is used as the opening delimiter of a control string for device control use. The command string following may
/// consist of bit combinations in the range `00/08` to `00/13` and `02/00` to `07/14`. The control string is closed by
/// the terminating delimiter STRING TERMINATOR (`ST`).
///
/// The command string represents either one or more commands for the receiving device, or one or more status reports
/// from the sending device. The purpose and the format of the command string are specified by the most recent
/// occurrence of IDENTIFY DEVICE CONTROL STRING (`IDCS`), if any, or depend on the sending and/or the receiving device.
pub const DCS: ControlFunction = c1!(05 / 00);
/// End Of Guarded Area.
///
/// `EPA` is used to indicate that the active presentation position is the last of a string of character positions in
/// the presentation component, the contents of which are protected against manual alteration, are guarded against
/// transmission or transfer, depending on the setting of GUARDED AREA TRANSFER MODE (`GATM`), and may be protected
/// against erasure, depending on the setting of the ERASURE MODE (`ERM`). The beginning of this string is indicated
/// by START OF GUARDED AREA ([`SPA`]).
///
/// ## Note
///
/// The control functions for area definition ([`DAQ`][crate::control_sequences::DAQ], [`EPA`], [`ESA`], [`SPA`],
/// [`SSA`]) should not be used within an `SRS` string or an `SDS` string.
pub const EPA: ControlFunction = c1!(05 / 07);
/// End Of Selected Area.
///
/// `ESA` is used to indicate that the active presentation position is the last of a string of character positions in
/// the presentation component, the contents of which are eligible to be transmitted in the form of a data stream or
/// transferred to an auxiliary input/output device. The beginning of the string is indicated by START OF SELECTED
/// AREA ([`SSA`])
///
/// ## Note
///
/// The control functions for area definition ([`DAQ`][crate::control_sequences::DAQ], [`EPA`], [`ESA`], [`SPA`],
/// [`SSA`]) should not be used within an `SRS` string or an `SDS` string.
pub const ESA: ControlFunction = c1!(04 / 07);
/// Character Tabulation With Justification.
///
/// `HTJ` causes the contents of the active field (the field in the presentation component that contains the active
/// presentation position) to be shifted forward so that it ends at the character position preceding the following
/// character tabulation stop. The active presentation position is moved to that following character tabulation stop.
/// The character positions which precede the beginning of the shifted string are put into the erased state.
pub const HTJ: ControlFunction = c1!(04 / 09);
/// Character Tabulation Set.
///
/// `HTS` causes a character tabulation stop to be set at the active presentation position in the presentation
/// component.
///
/// The number of liens affected depends on the setting of the TABULATION STOP MODE (`TSM`).
pub const HTS: ControlFunction = c1!(04 / 08);
/// Message Waiting.
///
/// `MW` is used to set a message waiting indicator in the receiving device. An appropriate acknowledgement to the
/// receipt of `MW` may be given by using DEVICE STATUS REPORT (`DSR`).
pub const MW: ControlFunction = c1!(05 / 05);
/// No Break Here.
///
/// `NBH` is used to indicate a point where a line break shall not occur when text is formatted. `NBH` may occur between
/// two graphic characters either or both of which may be SPACE.
pub const NBH: ControlFunction = c1!(04 / 03);
/// Next Line.
///
/// The effect of `NEL` depends on the setting of the DEVICE COMPONENT SELECT MODE (`DCSM`) and on the parameter value
/// of SELECT IMPLICIT MOVEMENT DIRECTION (`SIMD`).
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to PRESENTATION and with a parameter value of `SIMD` equal to
/// `0`, `NEL` causes the active presentation position to be moved to the line home position of the following line in
/// the presentation component. The line home position is established by the parameter value of SET LINE HOME (`SLH`).
///
/// With a parameter value of `SIMD` equal to `1`, `NEL` causes the active presentation position to be moved to the line
/// limit position of the following line in the presentation component. The line limit position is established by the
/// parameter value of SET LINE LIMIT (`SLL`).
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to DATA and with a parameter value of `SIMD` equal to `0`, `NEL`
/// causes the active data position to be moved to the line home position of the following line in the data component.
/// The line home position is established by the parameter value of SET LINE HOME (`SLH`).
///
/// With a parameter value of `SIMD` equal to `1`, `NEL` causes the active data position to be moved to the line limit
/// position of the following line in the data component. The line limit position is established by the parameter value
/// of SET LINE LIMIT (`SLL`).
pub const NEL: ControlFunction = c1!(04 / 05);
/// Operating System Command
///
/// `OSC` is used as the opening delimiter of a control string for operating system use. The command string following
/// may consist of a sequence of bit combinations in the range `00/08` to `00/13` and `02/00` to `07/14`. The control
/// string is closed by the terminating delimiter STRING TERMINATOR (`ST`). The interpretation of the command string
/// depends on the relevant operating system.
pub const OSC: ControlFunction = c1!(05 / 13);
/// Partial Line Forward.
///
/// `PLD` causes the active presentation position to be moved in the presentation component to the corresponding
/// position of an imaginary line with a partial offset in the direction of the line progression. This offset should be
/// sufficient either to image following characters as subscripts until the first following occurrence of PARTIAL LINE
/// BACKWARD (`PLU`) in the data stream, or, if preceding characters were imaged as superscripts, to restore imaging
/// of following characters to the active line (the line that contains the active presentation position).
///
/// Any interactions between `PLD` and format effectors other than `PLU` are not defined.
pub const PLD: ControlFunction = c1!(04 / 11);
/// Partial Line Backwards.
///
/// `PLU` causes the active presentation position to be moved in the presentation component to the corresponding
/// position of an imaginary line with a partial offset in the direction opposite to that of the line progression. This
/// offset should be sufficient either to image following characters as superscripts until the first following
/// occurrence of PARTIAL LINE FORWARD (`PLD`) in the data stream, or, if preceding characters were imaged as
/// subscripts, to restore imaging of following characters to the active line (the line that contains the active
/// presentation position).
///
/// Any interactions between `PLU` and format effectors other than `PLD` are not defined.
pub const PLU: ControlFunction = c1!(04 / 12);
/// Privacy Message.
///
/// `PM` is used as the opening delimiter of a control string for privacy message use. The command string following may
/// consist of a sequence of bit combinations in the range `00/08` to `00/13` and `02/00` to `07/14`. The control string
/// is closed by the terminating delimiter STRING TERMINATOR (`ST`). The interpretation of the command string depends
/// on the relevant privacy discipline.
pub const PM: ControlFunction = c1!(05 / 14);
/// Private Use One.
///
/// `PU1` is reserved for a function without standardized meaning for private use as required, subject to the prior
/// agreement between the sender and the recipient of the data.
pub const PU1: ControlFunction = c1!(05 / 01);
/// Private Use Two.
///
/// `PU2` is reserved for a function without standardized meaning for private use as required, subject to the prior
/// agreement between the sender and the recipient of the data.
pub const PU2: ControlFunction = c1!(05 / 02);
/// Reverse Line Feed.
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to PRESENTATION, `RI` causes the active presentation position to
/// be moved in the presentation component to the corresponding character position of the preceding line feed.
///
/// If the DEVICE COMPONENT SELECT MODE (`DCSM`) is set to DATA, `RI` causes the active data position to be moved in the
/// data component to the corresponding character position of the preceding line.
pub const RI: ControlFunction = c1!(04 / 13);
/// Single Character Introducer.
///
/// `SCI` and the bit combination following it are used to represent a control function or a graphic character. The bit
/// combination following `SCI` must be from `00/08` to `00/13` or `02/00` to `07/14`. The use of `SCI` is reserved
/// for future standardization.
pub const SCI: ControlFunction = c1!(05 / 10);
/// Start of String.
///
/// `SOS` is used as the opening delimiter of a control string. The character string following may consist of any bit
/// combination, except those representing `SOS` or STRING TERMINATOR (`ST`). The control string is closed by the
/// terminating delimiter STRING TERMINATOR (`ST`). The interpretation of the character string depends on the
/// application.
pub const SOS: ControlFunction = c1!(05 / 08);
/// Start of Guarded Area.
///
/// `SPA` is used to indicate that the active presentation position is the first of a string of character positions in
/// the presentation component, the contents of which are protected against manual alteration, are guarded against
/// transmission or transfer, depending on the setting of the GUARDED AREA TRANSFER MODE (`GATM`) and may be protected
/// against erasure, depending on the setting of the ERASURE MODE (`ERM`). The end of this string is indicated by
/// END OF GUARDED AREA (`EPA`).
///
/// ## Note
///
/// The control functions for area definition (`DAQ`, `EPA`, `ESA`, `SPA`, `SSA`) should not be used within an `SRS`
/// string or an `SDS` string.
pub const SPA: ControlFunction = c1!(05 / 06);
/// Start of Selected Area.
///
/// `SSA` is used to indicate that the active presentation position is the first of a string of character positions in
/// the presentation component, the contents of which are eligible to be transmitted in the form of a data stream or
/// transferred to an auxilliary input/output device.
///
/// The end of this string is indicated by END OF SELECTED AREA ([`ESA`]). The string of characters actually transmitted
/// or transferred depends on the setting of the GUARDED AREA TRANSFER MODE ([`GATM`][crate::modes::GATM]) and on any
/// guarded areas established by DEFINE AREA QUALIFICATION ([`DAQ`][crate::control_sequences::DAQ]), or by START OF
/// GUARDED AREA ([`SPA`]) and END OF GUARDED AREA ([`EPA`]).
///
/// ## Note
///
/// The control functions for area definition (`DAQ`, `EPA`, `ESA`, `SPA`, `SSA`) should not be used within an `SRS`
/// string or an `SDS` string.
pub const SSA: ControlFunction = c1!(04 / 06);
/// Single-Shift Two.
///
/// `SS2` is used for code extension purposes. It causes the meanings of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `SS2` is defined in Standard ECMA-35.
pub const SS2: ControlFunction = c1!(04 / 14);
/// Single-Shift Three.
///
/// `SS3` is used for code extension purposes. It causes the meanings of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `SS3` is defined in Standard ECMA-35.
pub const SS3: ControlFunction = c1!(04 / 15);
/// String Terminator.
///
/// `ST` is used as the closing delimiter of a control string opened by APPLICATION PROGRAM COMMAND (`APC`), DEVICE
/// CONTROL STRING (`DCS`), OPERATING SYSTEM COMMAND (`OSC`), PRIVACY MESSAGE (`PM`), or START OF STRING (`SOS`).
pub const ST: ControlFunction = c1!(05 / 12);
/// Set Transmit State.
///
/// `STS` is used to establish the transmit state in the receiving device. In this state the transmission of data from
/// the device is possible.
///
/// The actual initiation of transmission of data is performed by a data communication or input/output interface control
/// procedure which is outside the scope of this Standard.
///
/// The transmit state is established either by `STS` appearing in the received data stream or by the operation of an
/// appropriate key on a keyboard.
pub const STS: ControlFunction = c1!(05 / 03);
/// Line Tabulation Set.
///
/// `VTS` causes a line tabulation stop to be set at the active line (the line that contains the active presentation
/// position).
pub const VTS: ControlFunction = c1!(04 / 10);

2446
src/control_sequences.rs Normal file

File diff suppressed because it is too large Load Diff

10
src/control_strings.rs Normal file
View File

@ -0,0 +1,10 @@
//! Control Strings.
//!
//! A control string is a string of bit combinations which may occur in the data stream as a logical entity for
//! control purposes. A control string consists of an opening delimiter, a command string or a character string,
//! and a terminating delimiter, the String Terminator (`ST`).
//!
//! A command string is a sequence of bit combinations in the range `00/08` to `00/13` and `02/00` to `07/14`.
//!
//! A character string is a sequence of any bit combination, except those representing Start Of String (`SOS`) or String
//! Terminator (`ST`).

View File

@ -0,0 +1,113 @@
//! Independent Control Functions.
//!
//! These control functions are represented in 7-bit codes by 2-character escape sequences of the form `ESC Fs`, where
//! `ESC` is represented by bit combination `01/11` and `Fs` is represented by a bit combination from `06/00` to
//! `07/14`.
//!
//! ## Overview of the Independent Control Functions
//!
//! | Row Number | Column `06` | Column `07` |
//! | ---------: | :---------: | :---------: |
//! | `00` | [`DMI`] | -- |
//! | `01` | [`INT`] | -- |
//! | `02` | [`EMI`] | -- |
//! | `03` | [`RIS`] | -- |
//! | `04` | [`CMD`] | -- |
//! | `05` | -- | -- |
//! | `06` | -- | -- |
//! | `07` | -- | -- |
//! | `08` | -- | -- |
//! | `09` | -- | -- |
//! | `10` | -- | -- |
//! | `11` | -- | -- |
//! | `12` | -- | [`LS3R`] |
//! | `13` | -- | [`LS2R`] |
//! | `14` | [`LS2`] | [`LS1R`] |
//! | `15` | [`LS3`] | -- |
//!
//! ## Note
//!
//! `ESC Fs` sequences are registered in the ISO International Register of Coded Character Sets to be Used with Escape
//! Sequences, which is maintained by the Registration Authority for ISO 2375.
use crate::ControlFunction;
macro_rules! independent {
($xx:literal/$yy:literal) => {
ControlFunction::new_independent_control_function(ascii!($xx / $yy))
};
}
/// Coding Method Delimiter.
///
/// `CMD` is used as the delimiter of a string of data coded according to Standard ECMA-35, and to switch to a general
/// level of control.
///
/// The use of `CMD` is not mandatory if the higher level protocol defines means of delimiting the string, for instance,
/// by specifying the length of the string.
pub const CMD: ControlFunction = independent!(06 / 04);
/// Disable Manual Input
///
/// `DMI` causes the manual input facilities of a device to be disabled.
pub const DMI: ControlFunction = independent!(06 / 00);
/// Enable Manual Input.
///
/// `EMI` is used to enable the manual input facilities of a device.
pub const EMI: ControlFunction = independent!(06 / 02);
/// Interrupt.
///
/// `INT` is used to indicate to the receiving device that the current process is to be interrupted and an agreed
/// procedure is to be initiated. This control function is applicable to either direction of transmission.
pub const INT: ControlFunction = independent!(06 / 01);
/// Locking-Shift One Right.
///
/// `LS1R` is used for code extension purposes. It causes the meaning of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `LS1R` is defined in Standard ECMA-35.
pub const LS1R: ControlFunction = independent!(07 / 14);
/// Locking-Shift Two.
///
/// `LS2` is used for code extension purposes. It causes the meaning of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `LS2` is defined in Standard ECMA-35.
pub const LS2: ControlFunction = independent!(06 / 14);
/// Locking-Shift Two Right.
///
/// `LS2R` is used for code extension purposes. It causes the meaning of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `LS2R` is defined in Standard ECMA-35.
pub const LS2R: ControlFunction = independent!(07 / 13);
/// Locking-Shift Three.
///
/// `LS3` is used for code extension purposes. It causes the meaning of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `LS3` is defined in Standard ECMA-35.
pub const LS3: ControlFunction = independent!(06 / 15);
/// Locking-Shift Three Right.
///
/// `LS3R` is used for code extension purposes. It causes the meaning of the bit combinations following it in the data
/// stream to be changed.
///
/// The use of `LS3R` is defined in Standard ECMA-35.
pub const LS3R: ControlFunction = independent!(07 / 12);
/// Reset to Initial State.
///
/// `RIS` causes a device to be reset to its initial state, i.e. the state it has after it is made operational. This
/// may imply, if applicable: clear tabulation stops, remove qualified areas, reset graphic rendition, put all character
/// positions into the erased state, move the active presentation position to the first position of the first line in
/// the presentation component, move the active data position to the first character position of the first line in the
/// data component, set the modes into the reset state, etc..
pub const RIS: ControlFunction = independent!(06 / 03);

166
src/lib.rs Normal file
View File

@ -0,0 +1,166 @@
//! # ANSI Escape Code Library
//!
//! ANSI escape sequences are a standard for in-band signalling to control cursor location, color, font styling, and
//! other options on video text terminals and terminal emulators.
//!
//! This library contains all ANSI Escape Codes that are defined in the [ISO 6429 Standard][iso-6429]. ISO 6429 is
//! the international standard that followed from the efforts of aligning the european [ECMA-48 Standard][ecma-48] and
//! the american [ANSI X3.64 Standard][ansi-x364].
//!
//! ## Notation
//!
//! In the [ECMA-48 Standard][ecma-48] a convention has been adopted to assist the reader of the Standard.
//!
//! Capital letters are used to refer to a specific control function, mode, mode setting, or graphic character in order
//! to avoid confusion, for example, between the concept `space`, and the character `SPACE`.
//!
//! As is intended by the [ECMA-48 Standard][ecma-48], this convention and all acronyms of modes, and control functions
//! are retained in this library, where rust permits.
//!
//! A character from the [ASCII table][ascii-table] is represented in the form `xx/yy`, where `xx` represents the column
//! number `00` to `07` in a 7-bit code table, and `yy` represents the row number `00` to `15`.
//!
//! ## Source Material
//!
//! The second, and newer, editions of the [ECMA-48 Standard][ecma-48] are based on the text of the
//! [ISO 6429 Standard][iso-6429] and are technically identical with it. Since the [ISO 6429 Standard][iso-6429] is not
//! freely available on the internet, this implementation is based on the publicly available documents of the
//! [ECMA-48 Standard][ecma-48]. In particular on edition 5 of the [ECMA-48 Standard][ecma-48], which is identical to
//! the third edition of [ISO-6429][iso-6429].
//!
//! The [ANSI X3.64 Standard][ansi-x364] has been withdrawn by ANSI in 1994 in favour of the international standard.
//!
//! You can read more about the history of the standards on [Wikipedia: ANSI escape code][wikipedia-ansi].
//!
//! [ansi-x364]: https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub86.pdf
//! [ascii-table]: https://en.wikipedia.org/wiki/ASCII#/media/File:USASCII_code_chart.png
//! [ecma-48]: https://www.ecma-international.org/publications-and-standards/standards/ecma-48/
//! [iso-6429]: https://www.iso.org/standard/12782.html
//! [wikipedia-ansi]: https://en.wikipedia.org/wiki/ANSI_escape_code
#![allow(dead_code)]
use std::str;
/// Convert the ascii table notation `xx/yy` into a rust string.
///
/// A character from the [ASCII table][ascii-table] is represented in the form `xx/yy`, where `xx` represents the column
/// number `00` to `07` in a 7-bit code table, and `yy` represents the row number `00` to `15`.
///
/// The macro can be used to convert a single code point into a str, or to convert a sequence of them.
///
/// ```
/// let a: &'static str = ascii!(06 / 01);
/// let abc: &'static str = ascii!(06 / 01, 06 / 02, 06 / 03);
/// ```
///
/// ## Safeness
///
/// This macro converts the given `xx/yy` combination into a ascii code by the formula `(xx << 4) + yy`.
/// The result is passed to the unsafe function std::str::from_utf8_unchecked.
///
/// This will result in an unsafe calculation, if the values for xx and yy are out of range. Valid ranges are:
///
/// - `xx: [0,7]`
/// - `yy: [0,15]`
///
/// Since this macro is not public and only used by the library itself, it is assumed to be used only within safe
/// bounds.
///
/// [ascii-table]: https://en.wikipedia.org/wiki/ASCII#/media/File:USASCII_code_chart.png
macro_rules! ascii {
($($xx:literal/$yy:literal), *) => {
unsafe { std::str::from_utf8_unchecked(&[$(($xx << 4) + $yy),*]) }
};
}
/// The different types of control functions.
enum ControlFunctionType {
/// Elements of the C0 set.
///
/// C0 control functions are represented in 7-bit codes by bit combinations from 00/00 to 01/15.
C0,
/// Elements of the C1 set.
///
/// C1 control functions are represented in 7-bit codes by 2-character escape sequences of the form `ESC Fe`,
/// where `ESC` is represented by bit combination `01/11`, and `Fe` is represented by a bit combination from
/// `04/00` to `05/15`.
C1,
/// Control Sequences.
///
/// Control sequences are strings of bit combinations starting with the control function Control Function Introducer
/// (`CSI`), followed by one or more bit combinations representing parameters, if any, and by one ore more bit
/// combinations identifying the control function. The control function `CSI` itself is an element of the independent_control_function set.
ControlSequence,
/// Independent Control Functions.
///
/// Independent control functions are represented in 7-bit codes by 2-character escape sequences of the form
/// `ESC Fs`, where `ESC` is represented by bit combination `01/11`, and `Fs` is represented by a bit combination
/// from `06/00` to `07/14`
IndependentControlFunction,
/// Control Strings.
///
/// A control string is a string of bit combinations which may occur in the data stream as a logical entity for
/// control purposes. A control string consists of an opening delimiter, a command string or character string, and
/// a terminating delimiter, the String Terminator (`ST`).
ControlString,
}
/// A control function.
pub struct ControlFunction {
function_type: ControlFunctionType,
value: &'static str,
parameters: Vec<String>,
}
impl ControlFunction {
/// Creates a new control function of type [C0][ControlFunctionType::C0].
///
/// C0 control functions do not accept any parameters.
const fn new_c0(value: &'static str) -> Self {
ControlFunction {
function_type: ControlFunctionType::C0,
value,
parameters: vec![],
}
}
/// Creates a new control function of type [C1][ControlFunctionType::C1].
///
/// independent_control_function control functions do not accept any parameters.
const fn new_c1(value: &'static str) -> Self {
ControlFunction {
function_type: ControlFunctionType::C1,
value,
parameters: vec![],
}
}
/// Creates a new control function of type [ControlSequence][ControlFunctionType::ControlSequence].
const fn new_sequence(value: &'static str, parameters: Vec<String>) -> Self {
ControlFunction {
function_type: ControlFunctionType::ControlSequence,
value,
parameters,
}
}
/// Creates a new control function of type [IndependentControlFunction][ControlFunctionType::IndependentControlFunction].
const fn new_independent_control_function(value: &'static str) -> Self {
ControlFunction {
function_type: ControlFunctionType::IndependentControlFunction,
value,
parameters: vec![],
}
}
}
pub mod c0;
pub mod c1;
pub mod control_sequences;
pub mod control_strings;
pub mod independent_control_functions;
pub mod modes;

123
src/modes.rs Normal file
View File

@ -0,0 +1,123 @@
/// Device Modes.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Mode {
/// Guarded Area Transfer Mode `GATM`.
GuardedAreaTransferMode = 1,
/// Keyboard Action Mode `KAM`.
KeyboardActionMode,
/// Control Presentation Mode `CRM`.
ControlPresentationMode,
/// Insertion Replacement Mode `IRM`.
InsertionReplacementMode,
/// Status Report Transfer Mode `SRTM`.
StatusReportTransferMode,
/// Erasure Mode `ERM`.
ErasureMode,
/// Line Editing Mode `VEM`.
LineEditingMode,
/// Bi-directional support mode `BDSM`.
BiDirectionalSupportMode,
/// Device Component Select Mode `DCSM`.
DeviceComponentSelectMode,
/// Character Editing Mode `HEM`.
CharacterEditingMode,
/// Positioning Unit Mode `PUM`.
PositioningUnitMode,
/// Send/Receive Mode `SRM`.
SendReceiveMode,
/// Format Effector Action Mode `FEAM`.
FormatEffectorActionMode,
/// Format Effector Transfer Mode `FETM`.
FormatEffectorTransferMode,
/// Multiple Area Transfer Mode `MATM`.
MultipleAreaTransferMode,
/// Transfer Termination Mode `TTM`.
TransferTerminationMode,
/// Selected Area Transfer Mode `SATM`.
SelectedAreaTransferMode,
/// Tabulation Stop Mode `TSM`.
TabulationStopMode,
/// Graphic Rendition Combination Mode `GRCM`.
GraphicRenditionCombinationMode = 21,
/// Zero Default Mode `ZDM`.
ZeroDefaultMode,
}
/// Guarded Area Transfer Mode `GATM`.
pub const GATM: Mode = Mode::GuardedAreaTransferMode;
/// Keyboard Action Mode `KAM`.
pub const KAM: Mode = Mode::KeyboardActionMode;
/// Control Presentation Mode `CRM`.
pub const CRM: Mode = Mode::ControlPresentationMode;
/// Insertion Replacement Mode `IRM`.
pub const IRM: Mode = Mode::InsertionReplacementMode;
/// Status Report Transfer Mode `SRTM`.
pub const SRTM: Mode = Mode::StatusReportTransferMode;
/// Erasure Mode `ERM`.
pub const ERM: Mode = Mode::ErasureMode;
/// Line Editing Mode `VEM`.
pub const VEM: Mode = Mode::LineEditingMode;
/// Bi-directional support mode `BDSM`.
pub const BDSM: Mode = Mode::BiDirectionalSupportMode;
/// Device Component Select Mode `DCSM`.
pub const DCSM: Mode = Mode::DeviceComponentSelectMode;
/// Character Editing Mode `HEM`.
pub const HEM: Mode = Mode::CharacterEditingMode;
/// Positioning Unit Mode `PUM`.
pub const PUM: Mode = Mode::PositioningUnitMode;
/// Send/Receive Mode `SRM`.
pub const SRM: Mode = Mode::SendReceiveMode;
/// Format Effector Action Mode `FEAM`.
pub const FEAM: Mode = Mode::FormatEffectorActionMode;
/// Format Effector Transfer Mode `FETM`.
pub const FETM: Mode = Mode::FormatEffectorTransferMode;
/// Multiple Area Transfer Mode `MATM`.
pub const MATM: Mode = Mode::MultipleAreaTransferMode;
/// Transfer Termination Mode `TTM`.
pub const TTM: Mode = Mode::TransferTerminationMode;
/// Selected Area Transfer Mode `SATM`.
pub const SATM: Mode = Mode::SelectedAreaTransferMode;
/// Tabulation Stop Mode `TSM`.
pub const TSM: Mode = Mode::TabulationStopMode;
/// Graphic Rendition Combination Mode `GRCM`.
pub const GRCM: Mode = Mode::GraphicRenditionCombinationMode;
/// Zero Default Mode `ZDM`.
pub const ZDM: Mode = Mode::ZeroDefaultMode;