prettytty/opt.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
//! Helper module with the options for connecting to terminals.
//!
//! This module provides the options for a terminal connection and the
//! corresponding builder.
//!
//!
//! # Example
//!
//! ```
//! # use prettytty::opt::Options;
//! let options = Options::builder()
//! .timeout(50)
//! .build();
//!
//! assert_eq!(options.timeout(), 50);
//! ```
/// The diagnostic logging volume.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Volume {
Silent,
Regular,
Detailed,
}
/// A terminal mode.
///
/// Currently four terminal modes are supported:
///
/// * __Charred mode__ considers the terminal configuration as too hot to
/// touch and makes no changes.
///
/// * __Cooked mode__ is the usual mode of operation on Unix and includes
/// several features that go beyond character-based I/O, including editing
/// the input line by line, turning key presses such as control-c into
/// signals, and translating line endings.
///
/// On Windows, this mode optimizes for interoperability, enables the UTF-8
/// code page for input and output, while also activating
/// `ENABLE_VIRTUAL_TERMINAL_INPUT`, `ENABLE_PROCESSED_OUTPUT`, and
/// `ENABLE_VIRTUAL_TERMINAL_PROCESSING`.
///
/// * __Rare mode__, also called cbreak mode, disables the line editor but
/// leaves other terminal convenience features such as processing control-c
/// enabled. This is the default mode for prettytty.
///
/// * __Raw mode__ disables all features beyond character-based I/O and ANSI
/// escape sequences. It maximizes the application's control over input and
/// output, but it also places the burden of implementing features at least as
/// good as line editing on the application developer.
///
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Mode {
/// Charred mode doesn't dare to touch the terminal configuration; it's too
/// hot.
Charred,
/// Cooked mode means turning control-c/d into signals, fiddling with
/// line-endings in the output, and always editing the input line by line.
/// Still, it allows for ANSI escape sequences.
Cooked,
/// Rare or cbreak mode.
#[default]
Rare,
/// Raw mode.
Raw,
}
#[derive(Clone, Debug)]
struct OptionData {
volume: Volume,
mode: Mode,
timeout: u8,
pathological_size: usize,
read_buffer_size: usize,
write_buffer_size: usize,
}
impl OptionData {
pub const fn new() -> Self {
Self {
volume: Volume::Silent,
mode: Mode::Rare,
timeout: 10,
pathological_size: 512,
read_buffer_size: 256,
write_buffer_size: 1_024,
}
}
}
/// A builder of options objects.
#[derive(Debug)]
pub struct OptionBuilder(OptionData);
impl OptionBuilder {
/// Set the volume.
pub fn volume(&mut self, volume: Volume) -> &mut Self {
self.0.volume = volume;
self
}
/// Set rare or raw mode.
pub fn mode(&mut self, mode: Mode) -> &mut Self {
self.0.mode = mode;
self
}
/// Set the timeout in deciseconds (0.1s).
pub fn timeout(&mut self, timeout: u8) -> &mut Self {
self.0.timeout = timeout;
self
}
/// Set the minimum length for pathological ANSI escape sequences.
///
/// This method ensures that the given size is at least double the read
/// buffer size, updating it if necessary.
pub fn pathological_size(&mut self, size: usize) -> &mut Self {
self.0.pathological_size = size.max(
self.0
.read_buffer_size
.saturating_add(self.0.read_buffer_size),
);
self
}
/// Set the read buffer size.
///
/// This method also updates the pathological size to twice the given size.
///
/// The read buffer must be large enough to hold the entire escape sequence
/// being recognized. When querying colors, that is 27 bytes: A response for
/// the 16th ANSI color *bright white* starts with `‹OSC›4;15;rgb:` followed
/// by three four-digit hexadecimal numbers separated by forward slashes,
/// such as `ffff/ffff/ffff`, and then the terminating `‹ST›`. Both OSC and
/// ST require at most two bytes, resulting in a maximum sequence length of
/// 27 bytes.
///
/// This method ensures that the pathological size is at least double the
/// given size, updating it if necessary.
pub fn read_buffer_size(&mut self, size: usize) -> &mut Self {
self.0.read_buffer_size = size;
self.0.pathological_size = self.0.pathological_size.max(size.saturating_add(size));
self
}
/// Set the write buffer size.
pub fn write_buffer_size(&mut self, size: usize) -> &mut Self {
self.0.write_buffer_size = size;
self
}
/// Instantiate the options.
#[must_use = "the only reason to invoke method is to access the returned value"]
pub fn build(&self) -> Options {
Options(self.0.clone())
}
}
/// An options object.
#[derive(Debug)]
pub struct Options(OptionData);
impl Default for Options {
fn default() -> Self {
Self(OptionData::new())
}
}
impl Options {
/// Create a new builder with the default option values.
pub fn builder() -> OptionBuilder {
OptionBuilder(OptionData::new())
}
/// Instantiate the default options but with regular debugging output
/// enabled.
pub fn with_log() -> Self {
Self::builder().volume(Volume::Regular).build()
}
/// Instantiate the default options but with detailed debugging output
/// enabled.
pub fn with_detailed_log() -> Self {
Self::builder().volume(Volume::Detailed).build()
}
/// Get the volume.
pub fn volume(&self) -> Volume {
self.0.volume
}
/// Get the terminal mode.
pub fn mode(&self) -> Mode {
self.0.mode
}
/// Get the timeout in 0.1s increments for blocking read operations.
pub fn timeout(&self) -> u8 {
self.0.timeout
}
/// Get the pathological size.
pub fn pathological_size(&self) -> usize {
self.0.pathological_size
}
/// Get the size of the read buffer.
pub fn read_buffer_size(&self) -> usize {
self.0.read_buffer_size
}
/// Get the size of the write buffer.
pub fn write_buffer_size(&self) -> usize {
self.0.write_buffer_size
}
}