rusty_bubbletea/cursor.rs
1//! Cleanroom Rust port of upstream Go source file: `cursor.go`
2//! Upstream Target Tag / Version: `v2.0.8`
3//!
4//! <public-docs>
5//! # Cursor Position & Shape
6//!
7//! Terminal cursor position structures, shape definitions, and position query requests.
8//! </public-docs>
9
10use crate::color::Color;
11use crate::model::Cmd;
12
13/// Position represents a position in the terminal.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct Position {
16 /// X coordinate (column).
17 pub x: usize,
18 /// Y coordinate (row).
19 pub y: usize,
20}
21
22/// CursorPositionMsg represents the terminal cursor position.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub struct CursorPositionMsg {
25 /// X coordinate (column).
26 pub x: usize,
27 /// Y coordinate (row).
28 pub y: usize,
29}
30
31/// CursorShape represents a terminal cursor shape.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum CursorShape {
34 /// Block cursor shape.
35 CursorBlock,
36 /// Underline cursor shape.
37 CursorUnderline,
38 /// Bar cursor shape.
39 CursorBar,
40}
41
42/// Cursor configuration for a View.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct Cursor {
45 /// Cursor position.
46 pub position: Position,
47 /// Cursor shape.
48 pub shape: CursorShape,
49 /// Whether cursor is blinking.
50 pub blink: bool,
51 /// Cursor color.
52 pub color: Option<Color>,
53}
54
55impl Cursor {
56 /// <upstream-comment>NewCursor returns a new cursor with the default settings and the given
57 /// position.</upstream-comment>
58 pub fn new(x: usize, y: usize) -> Cursor {
59 Cursor {
60 position: Position { x, y },
61 shape: CursorShape::CursorBlock,
62 blink: true,
63 color: None,
64 }
65 }
66}
67
68/// RequestCursorPosMsg is a message that requests the cursor position.
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub struct RequestCursorPosMsg;
71
72/// RequestCursorPosition is a command that requests the cursor position.
73/// The cursor position will be sent as a [`CursorPositionMsg`] message.
74pub fn request_cursor_position() -> Cmd {
75 Some(Box::new(|| Some(Box::new(RequestCursorPosMsg))))
76}