OneBased

Struct OneBased 

Source
pub struct OneBased(/* private fields */);
Expand description

A helper type which avoids tripping over Unix terminal’s one-indexed conventions.

Coordinates and terminal dimensions are one-based in Termina on both Unix and Windows.

Implementations§

Source§

impl OneBased

Source

pub const fn new(n: u16) -> Option<Self>

Source

pub const fn from_zero_based(n: u16) -> Self

Source

pub const fn get(self) -> u16

Source

pub const fn get_zero_based(self) -> u16

Examples found in repository?
examples/event-read.rs (line 96)
36fn main() -> io::Result<()> {
37    println!("{HELP}");
38
39    let mut terminal = PlatformTerminal::new()?;
40    terminal.enter_raw_mode()?;
41
42    write!(
43        terminal,
44        "{}{}{}{}{}{}{}{}",
45        csi::Csi::Keyboard(csi::Keyboard::PushFlags(
46            KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES
47                | KittyKeyboardFlags::REPORT_ALTERNATE_KEYS
48        )),
49        decset!(FocusTracking),
50        decset!(BracketedPaste),
51        decset!(MouseTracking),
52        decset!(ButtonEventMouse),
53        decset!(AnyEventMouse),
54        decset!(RXVTMouse),
55        decset!(SGRMouse),
56    )?;
57    terminal.flush()?;
58
59    let mut size = terminal.get_dimensions()?;
60    loop {
61        let event = terminal.read(|event| !event.is_escape())?;
62
63        println!("Event: {event:?}\r");
64
65        match event {
66            Event::Key(KeyEvent {
67                code: KeyCode::Escape,
68                ..
69            }) => break,
70            Event::Key(KeyEvent {
71                code: KeyCode::Char('c'),
72                ..
73            }) => {
74                write!(
75                    terminal,
76                    "{}",
77                    csi::Csi::Cursor(csi::Cursor::RequestActivePositionReport),
78                )?;
79                terminal.flush()?;
80                let filter = |event: &Event| {
81                    matches!(
82                        event,
83                        Event::Csi(csi::Csi::Cursor(csi::Cursor::ActivePositionReport { .. }))
84                    )
85                };
86                if terminal.poll(filter, Some(Duration::from_millis(50)))? {
87                    let Event::Csi(csi::Csi::Cursor(csi::Cursor::ActivePositionReport {
88                        line,
89                        col,
90                    })) = terminal.read(filter)?
91                    else {
92                        unreachable!()
93                    };
94                    println!(
95                        "Cursor position: {:?}\r",
96                        (line.get_zero_based(), col.get_zero_based())
97                    );
98                } else {
99                    eprintln!("Failed to read the cursor position within 50msec\r");
100                }
101            }
102            Event::WindowResized(dimensions) => {
103                let new_size = flush_resize_events(&terminal, dimensions);
104                println!("Resize from {size:?} to {new_size:?}\r");
105                size = new_size;
106            }
107            _ => (),
108        }
109    }
110
111    write!(
112        terminal,
113        "{}{}{}{}{}{}{}{}",
114        csi::Csi::Keyboard(csi::Keyboard::PopFlags(1)),
115        decreset!(FocusTracking),
116        decreset!(BracketedPaste),
117        decreset!(MouseTracking),
118        decreset!(ButtonEventMouse),
119        decreset!(AnyEventMouse),
120        decreset!(RXVTMouse),
121        decreset!(SGRMouse),
122    )?;
123
124    Ok(())
125}

Trait Implementations§

Source§

impl Clone for OneBased

Source§

fn clone(&self) -> OneBased

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OneBased

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for OneBased

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for OneBased

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<NonZero<u16>> for OneBased

Source§

fn from(n: NonZeroU16) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for OneBased

Source§

fn eq(&self, other: &OneBased) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for OneBased

Source§

impl Eq for OneBased

Source§

impl StructuralPartialEq for OneBased

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.