add OneBased helper
This commit is contained in:
1 parent
e68bf65b2f
commit
29c7610ead
3 files changed
+60
-15
No files matched your search
@@ -1495,7 +1495,12 @@ impl TerminalState {
|
||||
right,
|
||||
..
|
||||
} => {
|
||||
let checksum = self.checksum_rectangle(left, top, right, bottom);
|
||||
let checksum = self.checksum_rectangle(
|
||||
left.as_zero_based(),
|
||||
top.as_zero_based(),
|
||||
right.as_zero_based(),
|
||||
bottom.as_zero_based(),
|
||||
);
|
||||
write!(host.writer(), "\x1bP{}!~{:04x}\x1b\\", request_id, checksum).ok();
|
||||
}
|
||||
Window::Iconify | Window::DeIconify => {}
|
||||
|
||||
+10
-14
@@ -1,3 +1,4 @@
|
||||
use super::OneBased;
|
||||
use crate::cell::{Blink, Intensity, Underline};
|
||||
use crate::color::{AnsiColor, ColorSpec, RgbColor};
|
||||
use crate::input::{Modifiers, MouseButtons};
|
||||
@@ -270,10 +271,10 @@ pub enum Window {
|
||||
ChecksumRectangularArea {
|
||||
request_id: i64,
|
||||
page_number: i64,
|
||||
top: u32,
|
||||
left: u32,
|
||||
bottom: u32,
|
||||
right: u32,
|
||||
top: OneBased,
|
||||
left: OneBased,
|
||||
bottom: OneBased,
|
||||
right: OneBased,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -339,12 +340,7 @@ impl Display for Window {
|
||||
} => write!(
|
||||
f,
|
||||
"{};{};{};{};{};{}*y",
|
||||
request_id,
|
||||
page_number,
|
||||
top + 1,
|
||||
left + 1,
|
||||
bottom + 1,
|
||||
right + 1
|
||||
request_id, page_number, top, left, bottom, right,
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -1229,10 +1225,10 @@ impl<'a> CSIParser<'a> {
|
||||
}
|
||||
let request_id = p(params, 0)?;
|
||||
let page_number = p(params, 1)?;
|
||||
let top = to_1b_u32(p(params, 2)?)?.saturating_sub(1);
|
||||
let left = to_1b_u32(p(params, 3)?)?.saturating_sub(1);
|
||||
let bottom = to_1b_u32(p(params, 4)?)?.saturating_sub(1);
|
||||
let right = to_1b_u32(p(params, 5)?)?.saturating_sub(1);
|
||||
let top = OneBased::from_optional_esc_param(params.get(2))?;
|
||||
let left = OneBased::from_optional_esc_param(params.get(3))?;
|
||||
let bottom = OneBased::from_optional_esc_param(params.get(4))?;
|
||||
let right = OneBased::from_optional_esc_param(params.get(5))?;
|
||||
Ok(CSI::Window(Window::ChecksumRectangularArea {
|
||||
request_id,
|
||||
page_number,
|
||||
|
||||
@@ -136,3 +136,47 @@ pub enum ControlCode {
|
||||
PM = 0x9e,
|
||||
APC = 0x9f,
|
||||
}
|
||||
|
||||
/// A helper type to avoid accidentally tripping over problems with
|
||||
/// 1-based values in escape sequences.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct OneBased {
|
||||
value: u32,
|
||||
}
|
||||
|
||||
impl OneBased {
|
||||
pub fn new(value: u32) -> Self {
|
||||
debug_assert!(
|
||||
value != 0,
|
||||
"programmer error: deliberately assigning zero to a OneBased"
|
||||
);
|
||||
Self { value }
|
||||
}
|
||||
|
||||
/// Map a value from an escape sequence parameter
|
||||
pub fn from_esc_param(v: i64) -> Result<Self, ()> {
|
||||
if v == 0 {
|
||||
Ok(Self { value: num::one() })
|
||||
} else if v > 0 && v <= i64::from(u32::max_value()) {
|
||||
Ok(Self { value: v as u32 })
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Map a value from an optional escape sequence parameter
|
||||
pub fn from_optional_esc_param(o: Option<&i64>) -> Result<Self, ()> {
|
||||
Self::from_esc_param(o.map(|x| *x).unwrap_or(1))
|
||||
}
|
||||
|
||||
/// Return the underlying value as a 0-based value
|
||||
pub fn as_zero_based(self) -> u32 {
|
||||
self.value.saturating_sub(1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for OneBased {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
|
||||
self.value.fmt(f)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user