interm/interactive/lines.rs
1use std::cell::Cell;
2
3#[derive(Default, Debug, Clone)]
4/// A line that can be updated in the terminal. It is used in the [`Block`] struct.
5pub struct InteractiveLine {
6 pub content: String,
7 pub relative_row: Cell<u8>,
8}
9
10impl InteractiveLine {
11 /// Creates a new [`InteractiveLine`] with the given content.
12 ///
13 /// # Arguments
14 ///
15 /// * `content` - The content of the line.
16 ///
17 /// # Example
18 ///
19 /// ```
20 /// use interm::interactive::Line;
21 ///
22 /// let line = Line::new("Hello, world!");
23 ///
24 /// assert_eq!(line.content, "Hello, world!");
25 /// ```
26 ///
27 /// [`Block`]: struct.Block.html
28 /// [`InteractiveLine`]: struct.InteractiveLine.html
29 /// [`InteractiveLine::content`]: struct.InteractiveLine.html#structfield.content
30 /// [`InteractiveLine::relative_row`]: struct.InteractiveLine.html#structfield.relative_row
31 /// [`InteractiveLine::new`]: struct.InteractiveLine.html#method.new
32 /// [`InteractiveLine::update_content`]: struct.InteractiveLine.html#method.update_content
33 pub fn new(content: &str) -> Self {
34 Self {
35 content: content.to_owned(),
36 ..Default::default()
37 }
38 }
39
40 /// Updates the content of the line.
41 /// # Arguments
42 /// * `content` - The new content of the line.
43 /// # Example
44 /// ```
45 /// use interm::interactive::Line;
46 /// let mut line = Line::new("Hello, world!");
47 /// line.update_content("Hello, world! 2");
48 /// assert_eq!(line.content, "Hello, world! 2");
49 /// ```
50 /// [`Block`]: struct.Block.html
51 /// [`InteractiveLine`]: struct.InteractiveLine.html
52 /// [`InteractiveLine::content`]: struct.InteractiveLine.html#structfield.content
53 /// [`InteractiveLine::relative_row`]: struct.InteractiveLine.html#structfield.relative_row
54 /// [`InteractiveLine::new`]: struct.InteractiveLine.html#method.new
55 /// [`InteractiveLine::update_content`]: struct.InteractiveLine.html#method.update_content
56 ///
57 /// # Notes
58 /// This method is used in the [`Block::update_element`] method.
59 pub fn update_content(&mut self, content: &str) {
60 self.content = content.to_owned();
61 }
62
63 pub(crate) fn update_relative_row(&mut self, row: u8) {
64 *self.relative_row.get_mut() = row;
65 }
66}