adopt OneBased type for a number of row/col escape sequences
This commit is contained in:
1 parent
93020c7553
commit
222e278720
5 files changed
+106
-41
No files matched your search
@@ -11,7 +11,7 @@ use termwiz::escape::csi::{
|
||||
Sgr, TerminalMode, TerminalModeCode, Window,
|
||||
};
|
||||
use termwiz::escape::osc::{ITermFileData, ITermProprietary};
|
||||
use termwiz::escape::{Action, ControlCode, Esc, EscCode, OperatingSystemCommand, CSI};
|
||||
use termwiz::escape::{Action, ControlCode, Esc, EscCode, OneBased, OperatingSystemCommand, CSI};
|
||||
use termwiz::hyperlink::Rule as HyperlinkRule;
|
||||
use termwiz::image::{ImageCell, ImageData, TextureCoordinate};
|
||||
|
||||
@@ -1649,9 +1649,8 @@ impl TerminalState {
|
||||
match cursor {
|
||||
Cursor::SetTopAndBottomMargins { top, bottom } => {
|
||||
let rows = self.screen().physical_rows;
|
||||
let mut top = i64::from(top).saturating_sub(1).min(rows as i64 - 1).max(0);
|
||||
let mut bottom = i64::from(bottom)
|
||||
.saturating_sub(1)
|
||||
let mut top = i64::from(top.as_zero_based()).min(rows as i64 - 1).max(0);
|
||||
let mut bottom = i64::from(bottom.as_zero_based())
|
||||
.min(rows as i64 - 1)
|
||||
.max(0);
|
||||
if top > bottom {
|
||||
@@ -1683,12 +1682,12 @@ impl TerminalState {
|
||||
}
|
||||
Cursor::CharacterAndLinePosition { line, col } | Cursor::Position { line, col } => self
|
||||
.set_cursor_pos(
|
||||
&Position::Absolute((i64::from(col)).saturating_sub(1)),
|
||||
&Position::Absolute((i64::from(line)).saturating_sub(1)),
|
||||
&Position::Absolute(i64::from(col.as_zero_based())),
|
||||
&Position::Absolute(i64::from(line.as_zero_based())),
|
||||
),
|
||||
Cursor::CharacterAbsolute(col) | Cursor::CharacterPositionAbsolute(col) => self
|
||||
.set_cursor_pos(
|
||||
&Position::Absolute((i64::from(col)).saturating_sub(1)),
|
||||
&Position::Absolute(i64::from(col.as_zero_based())),
|
||||
&Position::Relative(0),
|
||||
),
|
||||
Cursor::CharacterPositionBackward(col) => self.set_cursor_pos(
|
||||
@@ -1722,8 +1721,8 @@ impl TerminalState {
|
||||
// we don't need to process it as a terminal command
|
||||
}
|
||||
Cursor::RequestActivePositionReport => {
|
||||
let line = self.cursor.y as u32 + 1;
|
||||
let col = self.cursor.x as u32 + 1;
|
||||
let line = OneBased::from_zero_based(self.cursor.y as u32);
|
||||
let col = OneBased::from_zero_based(self.cursor.x as u32);
|
||||
let report = CSI::Cursor(Cursor::ActivePositionReport { line, col });
|
||||
write!(host.writer(), "{}", report).ok();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ mod csi;
|
||||
mod selection;
|
||||
use std::sync::Arc;
|
||||
use termwiz::escape::csi::{Edit, EraseInDisplay, EraseInLine};
|
||||
use termwiz::escape::{OperatingSystemCommand, CSI};
|
||||
use termwiz::escape::{OneBased, OperatingSystemCommand, CSI};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct TestHost {
|
||||
@@ -487,8 +487,10 @@ fn test_scroll_margins() {
|
||||
term.print("1\n2\n3\n4\n");
|
||||
assert_all_contents(&term, &["1", "2", "3", "4", " "]);
|
||||
|
||||
let margins =
|
||||
CSI::Cursor(termwiz::escape::csi::Cursor::SetTopAndBottomMargins { top: 1, bottom: 2 });
|
||||
let margins = CSI::Cursor(termwiz::escape::csi::Cursor::SetTopAndBottomMargins {
|
||||
top: OneBased::new(1),
|
||||
bottom: OneBased::new(2),
|
||||
});
|
||||
term.print(format!("{}", margins));
|
||||
|
||||
term.print("z\n");
|
||||
|
||||
+46
-20
@@ -498,12 +498,12 @@ pub enum Cursor {
|
||||
|
||||
/// CHA: Moves cursor to the Ps-th column of the active line. The default
|
||||
/// value of Ps is 1.
|
||||
CharacterAbsolute(u32),
|
||||
CharacterAbsolute(OneBased),
|
||||
|
||||
/// HPA CHARACTER POSITION ABSOLUTE
|
||||
/// HPA Moves cursor to the Ps-th column of the active line. The default
|
||||
/// value of Ps is 1.
|
||||
CharacterPositionAbsolute(u32),
|
||||
CharacterPositionAbsolute(OneBased),
|
||||
|
||||
/// HPB - CHARACTER POSITION BACKWARD
|
||||
/// HPB Moves cursor to the left Ps columns. The default value of Ps is 1.
|
||||
@@ -517,8 +517,8 @@ pub enum Cursor {
|
||||
/// HVP Moves cursor to the Ps1-th line and to the Ps2-th column. The
|
||||
/// default value of Ps1 and Ps2 is 1.
|
||||
CharacterAndLinePosition {
|
||||
line: u32,
|
||||
col: u32,
|
||||
line: OneBased,
|
||||
col: OneBased,
|
||||
},
|
||||
|
||||
/// VPA - LINE POSITION ABSOLUTE
|
||||
@@ -563,8 +563,8 @@ pub enum Cursor {
|
||||
/// Pn1 and m equals the value of Pn2. CPR may be solicited by a DEVICE
|
||||
/// STATUS REPORT (DSR) or be sent unsolicited .
|
||||
ActivePositionReport {
|
||||
line: u32,
|
||||
col: u32,
|
||||
line: OneBased,
|
||||
col: OneBased,
|
||||
},
|
||||
|
||||
/// CPR: this is the request from the client.
|
||||
@@ -597,8 +597,8 @@ pub enum Cursor {
|
||||
/// Moves cursor to the Ps1-th line and to the Ps2-th column. The default
|
||||
/// value of Ps1 and Ps2 is 1.
|
||||
Position {
|
||||
line: u32,
|
||||
col: u32,
|
||||
line: OneBased,
|
||||
col: OneBased,
|
||||
},
|
||||
|
||||
/// CUU - Cursor Up
|
||||
@@ -613,8 +613,8 @@ pub enum Cursor {
|
||||
|
||||
/// DECSTBM - Set top and bottom margins.
|
||||
SetTopAndBottomMargins {
|
||||
top: u32,
|
||||
bottom: u32,
|
||||
top: OneBased,
|
||||
bottom: OneBased,
|
||||
},
|
||||
|
||||
CursorStyle(CursorStyle),
|
||||
@@ -748,6 +748,16 @@ impl EncodeCSIParam for u32 {
|
||||
}
|
||||
}
|
||||
|
||||
impl EncodeCSIParam for OneBased {
|
||||
fn write_csi(&self, f: &mut Formatter, control: &str) -> Result<(), FmtError> {
|
||||
if self.as_one_based() == 1 {
|
||||
write!(f, "{}", control)
|
||||
} else {
|
||||
write!(f, "{}{}", *self, control)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Edit {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
||||
match self {
|
||||
@@ -791,7 +801,7 @@ impl Display for Cursor {
|
||||
Cursor::LinePositionBackward(n) => n.write_csi(f, "k")?,
|
||||
Cursor::LinePositionForward(n) => n.write_csi(f, "e")?,
|
||||
Cursor::SetTopAndBottomMargins { top, bottom } => {
|
||||
if *top == 1 && *bottom == u32::max_value() {
|
||||
if top.as_one_based() == 1 && bottom.as_one_based() == u32::max_value() {
|
||||
write!(f, "r")?;
|
||||
} else {
|
||||
write!(f, "{};{}r", top, bottom)?;
|
||||
@@ -827,15 +837,31 @@ impl ParseParams for u32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse an input parameter into a 1-based unsigned value
|
||||
impl ParseParams for OneBased {
|
||||
fn parse_params(params: &[i64]) -> Result<OneBased, ()> {
|
||||
if params.is_empty() {
|
||||
Ok(OneBased::new(1))
|
||||
} else if params.len() == 1 {
|
||||
OneBased::from_esc_param(params[0])
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a pair of 1-based unsigned values into a tuple.
|
||||
/// This is typically used to build a struct comprised of
|
||||
/// the pair of values.
|
||||
impl ParseParams for (u32, u32) {
|
||||
fn parse_params(params: &[i64]) -> Result<(u32, u32), ()> {
|
||||
impl ParseParams for (OneBased, OneBased) {
|
||||
fn parse_params(params: &[i64]) -> Result<(OneBased, OneBased), ()> {
|
||||
if params.is_empty() {
|
||||
Ok((1, 1))
|
||||
Ok((OneBased::new(1), OneBased::new(1)))
|
||||
} else if params.len() == 2 {
|
||||
Ok((to_1b_u32(params[0])?, to_1b_u32(params[1])?))
|
||||
Ok((
|
||||
OneBased::from_esc_param(params[0])?,
|
||||
OneBased::from_esc_param(params[1])?,
|
||||
))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
@@ -1162,7 +1188,7 @@ macro_rules! parse {
|
||||
}};
|
||||
|
||||
($ns:ident, $variant:ident, $first:ident, $second:ident, $params:expr) => {{
|
||||
let (p1, p2): (u32, u32) = ParseParams::parse_params($params)?;
|
||||
let (p1, p2): (OneBased, OneBased) = ParseParams::parse_params($params)?;
|
||||
Ok(CSI::$ns($ns::$variant {
|
||||
$first: p1,
|
||||
$second: p2,
|
||||
@@ -1308,16 +1334,16 @@ impl<'a> CSIParser<'a> {
|
||||
fn decstbm(&mut self, params: &'a [i64]) -> Result<CSI, ()> {
|
||||
if params.is_empty() {
|
||||
Ok(CSI::Cursor(Cursor::SetTopAndBottomMargins {
|
||||
top: 1,
|
||||
bottom: u32::max_value(),
|
||||
top: OneBased::new(1),
|
||||
bottom: OneBased::new(u32::max_value()),
|
||||
}))
|
||||
} else if params.len() == 2 {
|
||||
Ok(self.advance_by(
|
||||
2,
|
||||
params,
|
||||
CSI::Cursor(Cursor::SetTopAndBottomMargins {
|
||||
top: to_1b_u32(params[0])?,
|
||||
bottom: to_1b_u32(params[1])?,
|
||||
top: OneBased::from_esc_param(params[0])?,
|
||||
bottom: OneBased::from_esc_param(params[1])?,
|
||||
}),
|
||||
))
|
||||
} else {
|
||||
|
||||
@@ -153,6 +153,10 @@ impl OneBased {
|
||||
Self { value }
|
||||
}
|
||||
|
||||
pub fn from_zero_based(value: u32) -> Self {
|
||||
Self { value: value + 1 }
|
||||
}
|
||||
|
||||
/// Map a value from an escape sequence parameter
|
||||
pub fn from_esc_param(v: i64) -> Result<Self, ()> {
|
||||
if v == 0 {
|
||||
@@ -173,6 +177,10 @@ impl OneBased {
|
||||
pub fn as_zero_based(self) -> u32 {
|
||||
self.value.saturating_sub(1)
|
||||
}
|
||||
|
||||
pub fn as_one_based(self) -> u32 {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for OneBased {
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::cell::{AttributeChange, Blink, CellAttributes, Intensity, Underline};
|
||||
use crate::color::{ColorAttribute, ColorSpec};
|
||||
use crate::escape::csi::{Cursor, Edit, EraseInDisplay, EraseInLine, Sgr, CSI};
|
||||
use crate::escape::osc::{ITermDimension, ITermFileData, ITermProprietary, OperatingSystemCommand};
|
||||
use crate::escape::OneBased;
|
||||
use crate::image::TextureCoordinate;
|
||||
use crate::surface::{Change, CursorShape, Position};
|
||||
use crate::terminal::unix::UnixTty;
|
||||
@@ -319,7 +320,10 @@ impl TerminfoRenderer {
|
||||
write!(
|
||||
out,
|
||||
"{}",
|
||||
CSI::Cursor(Cursor::Position { line: 1, col: 1 })
|
||||
CSI::Cursor(Cursor::Position {
|
||||
line: OneBased::new(1),
|
||||
col: OneBased::new(1)
|
||||
})
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -336,7 +340,14 @@ impl TerminfoRenderer {
|
||||
if let Some(attr) = self.get_capability::<cap::CursorHome>() {
|
||||
attr.expand().to(out.by_ref())?;
|
||||
} else {
|
||||
write!(out, "{}", CSI::Cursor(Cursor::Position { line: 1, col: 1 }))?;
|
||||
write!(
|
||||
out,
|
||||
"{}",
|
||||
CSI::Cursor(Cursor::Position {
|
||||
line: OneBased::new(1),
|
||||
col: OneBased::new(1)
|
||||
})
|
||||
)?;
|
||||
}
|
||||
|
||||
let size = out.get_size()?;
|
||||
@@ -444,7 +455,14 @@ impl TerminfoRenderer {
|
||||
if let Some(attr) = self.get_capability::<cap::CursorHome>() {
|
||||
attr.expand().to(out.by_ref())?;
|
||||
} else {
|
||||
write!(out, "{}", CSI::Cursor(Cursor::Position { line: 1, col: 1 }))?;
|
||||
write!(
|
||||
out,
|
||||
"{}",
|
||||
CSI::Cursor(Cursor::Position {
|
||||
line: OneBased::new(1),
|
||||
col: OneBased::new(1)
|
||||
})
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Change::CursorPosition {
|
||||
@@ -488,8 +506,8 @@ impl TerminfoRenderer {
|
||||
out,
|
||||
"{}",
|
||||
CSI::Cursor(Cursor::Position {
|
||||
line: x + 1,
|
||||
col: y + 1,
|
||||
line: OneBased::from_zero_based(x),
|
||||
col: OneBased::from_zero_based(y),
|
||||
})
|
||||
)?;
|
||||
}
|
||||
@@ -820,7 +838,10 @@ mod test {
|
||||
assert_eq!(
|
||||
result,
|
||||
vec![
|
||||
Action::CSI(CSI::Cursor(Cursor::Position { line: 1, col: 1 })),
|
||||
Action::CSI(CSI::Cursor(Cursor::Position {
|
||||
line: OneBased::new(1),
|
||||
col: OneBased::new(1)
|
||||
})),
|
||||
Action::CSI(CSI::Edit(Edit::EraseInDisplay(
|
||||
EraseInDisplay::EraseDisplay,
|
||||
))),
|
||||
@@ -841,7 +862,10 @@ mod test {
|
||||
result,
|
||||
vec![
|
||||
Action::CSI(CSI::Sgr(Sgr::Background(AnsiColor::Maroon.into()))),
|
||||
Action::CSI(CSI::Cursor(Cursor::Position { line: 1, col: 1 })),
|
||||
Action::CSI(CSI::Cursor(Cursor::Position {
|
||||
line: OneBased::new(1),
|
||||
col: OneBased::new(1)
|
||||
})),
|
||||
Action::CSI(CSI::Edit(Edit::EraseInDisplay(
|
||||
EraseInDisplay::EraseDisplay,
|
||||
))),
|
||||
@@ -866,7 +890,10 @@ mod test {
|
||||
assert_eq!(
|
||||
result,
|
||||
vec![
|
||||
Action::CSI(CSI::Cursor(Cursor::Position { line: 1, col: 1 })),
|
||||
Action::CSI(CSI::Cursor(Cursor::Position {
|
||||
line: OneBased::new(1),
|
||||
col: OneBased::new(1)
|
||||
})),
|
||||
Action::CSI(CSI::Edit(Edit::EraseInDisplay(
|
||||
EraseInDisplay::EraseDisplay,
|
||||
))),
|
||||
@@ -887,7 +914,10 @@ mod test {
|
||||
result,
|
||||
vec![
|
||||
Action::CSI(CSI::Sgr(Sgr::Background(AnsiColor::Maroon.into()))),
|
||||
Action::CSI(CSI::Cursor(Cursor::Position { line: 1, col: 1 })),
|
||||
Action::CSI(CSI::Cursor(Cursor::Position {
|
||||
line: OneBased::new(1),
|
||||
col: OneBased::new(1)
|
||||
})),
|
||||
// bce is not known to be available, so we emit a bunch of spaces.
|
||||
// TODO: could we use ECMA-48 REP for this?
|
||||
Action::Print(' '),
|
||||
|
||||
Reference in new issue
Block a user