pub struct Position {
pub x: u16,
pub y: u16,
}
Expand description
Position in the terminal
The position is relative to the top left corner of the terminal window, with the top left corner being (0, 0). The x axis is horizontal increasing to the right, and the y axis is vertical increasing downwards.
§Examples
use ratatui::layout::{Position, Rect};
// the following are all equivalent
let position = Position { x: 1, y: 2 };
let position = Position::new(1, 2);
let position = Position::from((1, 2));
let position = Position::from(Rect::new(1, 2, 3, 4));
// position can be converted back into the components when needed
let (x, y) = position.into();
Fields§
§x: u16
The x coordinate of the position
The x coordinate is relative to the left edge of the terminal window, with the left edge being 0.
y: u16
The y coordinate of the position
The y coordinate is relative to the top edge of the terminal window, with the top edge being 0.
Implementations§
Source§impl Position
impl Position
Sourcepub const fn new(x: u16, y: u16) -> Self
pub const fn new(x: u16, y: u16) -> Self
Create a new position
Examples found in repository?
More examples
examples/colors_rgb.rs (line 218)
206 fn render(self, area: Rect, buf: &mut Buffer) {
207 self.setup_colors(area);
208 let colors = &self.colors;
209 for (xi, x) in (area.left()..area.right()).enumerate() {
210 // animate the colors by shifting the x index by the frame number
211 let xi = (xi + self.frame_count) % (area.width as usize);
212 for (yi, y) in (area.top()..area.bottom()).enumerate() {
213 // render a half block character for each row of pixels with the foreground color
214 // set to the color of the pixel and the background color set to the color of the
215 // pixel below it
216 let fg = colors[yi * 2][xi];
217 let bg = colors[yi * 2 + 1][xi];
218 buf[Position::new(x, y)].set_char('▀').set_fg(fg).set_bg(bg);
219 }
220 }
221 self.frame_count += 1;
222 }
examples/user_input.rs (lines 217-223)
169 fn draw(&self, frame: &mut Frame) {
170 let vertical = Layout::vertical([
171 Constraint::Length(1),
172 Constraint::Length(3),
173 Constraint::Min(1),
174 ]);
175 let [help_area, input_area, messages_area] = vertical.areas(frame.area());
176
177 let (msg, style) = match self.input_mode {
178 InputMode::Normal => (
179 vec![
180 "Press ".into(),
181 "q".bold(),
182 " to exit, ".into(),
183 "e".bold(),
184 " to start editing.".bold(),
185 ],
186 Style::default().add_modifier(Modifier::RAPID_BLINK),
187 ),
188 InputMode::Editing => (
189 vec![
190 "Press ".into(),
191 "Esc".bold(),
192 " to stop editing, ".into(),
193 "Enter".bold(),
194 " to record the message".into(),
195 ],
196 Style::default(),
197 ),
198 };
199 let text = Text::from(Line::from(msg)).patch_style(style);
200 let help_message = Paragraph::new(text);
201 frame.render_widget(help_message, help_area);
202
203 let input = Paragraph::new(self.input.as_str())
204 .style(match self.input_mode {
205 InputMode::Normal => Style::default(),
206 InputMode::Editing => Style::default().fg(Color::Yellow),
207 })
208 .block(Block::bordered().title("Input"));
209 frame.render_widget(input, input_area);
210 match self.input_mode {
211 // Hide the cursor. `Frame` does this by default, so we don't need to do anything here
212 InputMode::Normal => {}
213
214 // Make the cursor visible and ask ratatui to put it at the specified coordinates after
215 // rendering
216 #[allow(clippy::cast_possible_truncation)]
217 InputMode::Editing => frame.set_cursor_position(Position::new(
218 // Draw the cursor at the current position in the input field.
219 // This position is can be controlled via the left and right arrow key
220 input_area.x + self.character_index as u16 + 1,
221 // Move one line down, from the border to the input line
222 input_area.y + 1,
223 )),
224 }
225
226 let messages: Vec<ListItem> = self
227 .messages
228 .iter()
229 .enumerate()
230 .map(|(i, m)| {
231 let content = Line::from(Span::raw(format!("{i}: {m}")));
232 ListItem::new(content)
233 })
234 .collect();
235 let messages = List::new(messages).block(Block::bordered().title("Messages"));
236 frame.render_widget(messages, messages_area);
237 }
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Position
impl<'de> Deserialize<'de> for Position
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Ord for Position
impl Ord for Position
Source§impl PartialOrd for Position
impl PartialOrd for Position
impl Copy for Position
impl Eq for Position
impl StructuralPartialEq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnwindSafe for Position
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Convert the source color to the destination color using the specified
method.
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Convert the source color to the destination color using the bradford
method by default.
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Cast a collection of colors into a collection of arrays.
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Cast this collection of arrays into a collection of colors.
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
The number type that’s used in
parameters
when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
Converts
self
into C
, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Cast a collection of colors into a collection of color components.
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
Performs a conversion from
angle
.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
Converts
other
into Self
, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
Performs a conversion into
T
.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
The number type that’s used in
parameters
when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
Converts
self
into C
, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Convert into T with values clamped to the color defined bounds Read more
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Convert into T. The resulting color might be invalid in its color space Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
Converts
self
into T
, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
Fallible version of
ToCompactString::to_compact_string()
Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
Converts the given value to a
CompactString
. Read moreSource§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
The error for when
try_into_colors
fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Try to cast this collection of color components into a collection of
colors. Read more
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
Convert into T, returning ok if the color is inside of its defined
range, otherwise an
OutOfBounds
error is returned which contains
the unclamped color. Read moreSource§impl<C, U> UintsFrom<C> for Uwhere
C: IntoUints<U>,
impl<C, U> UintsFrom<C> for Uwhere
C: IntoUints<U>,
Source§fn uints_from(colors: C) -> U
fn uints_from(colors: C) -> U
Cast a collection of colors into a collection of unsigned integers.
Source§impl<C, U> UintsInto<C> for Uwhere
C: FromUints<U>,
impl<C, U> UintsInto<C> for Uwhere
C: FromUints<U>,
Source§fn uints_into(self) -> C
fn uints_into(self) -> C
Cast this collection of unsigned integers into a collection of colors.