Skip to content

Commit

Permalink
[tui] Clean up confusing caret types - raw and scroll adjusted
Browse files Browse the repository at this point in the history
Caret position can be of two kinds:
- Raw
- Scroll adjusted

Make this explicity using the type system and provide conversions
between the two.

Introduce new concrete types for:
- Pos
  - row
  - col
- Dim
  - width
  - height
- Use them in the codebase instead of ChUnit.
- Use ChUnit as the "inner" type of these new concrete types.
  • Loading branch information
nazmulidris committed Feb 3, 2025
1 parent 09ccbe1 commit c60209f
Show file tree
Hide file tree
Showing 23 changed files with 260 additions and 329 deletions.
24 changes: 0 additions & 24 deletions .vscode/bookmarks.json

This file was deleted.

1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"mworld",
"nanos",
"neovim",
"nextest",
"Nodesource",
"notcurses",
"nushell",
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/src/stack_alloc_types/sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use smallvec::SmallVec;
/// Intermediate struct used to insert a grapheme cluster segment into an existing unicode
/// string. When this gets larger than `DEFAULT_STRING_SIZE`, it will be
/// [smallvec::SmallVec::spilled] on the heap.
pub type VecStrBuffer<'a> = SmallVec<[&'a str; VEC_STR_BUFFER_CAPACITY]>;
pub type VecArrayStr<'a> = SmallVec<[&'a str; VEC_STR_BUFFER_CAPACITY]>;
const VEC_STR_BUFFER_CAPACITY: usize = 16;

/// Stack allocated string storage for small strings. When this gets larger than
Expand Down
52 changes: 52 additions & 0 deletions core/src/tui_core/dimens/ch_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct ChUnit {

impl ChUnit {
pub fn new(value: ChUnitPrimitiveType) -> Self { Self { value } }

pub fn reset(&mut self) { self.value = 0; }
}

/// ```rust
Expand Down Expand Up @@ -291,3 +293,53 @@ pub mod convert_from_other_types_to_ch {
}
}
}

#[cfg(test)]
mod tests {
use crate::{ChUnit, assert_eq2, ch, u16, usize};

#[test]
fn test_from_whatever_into_ch() {
let ch_1: ChUnit = ch(1);
assert_eq2!(*ch_1, 1);

let ch_2: ChUnit = ch(1) + ch(1);
assert_eq2!(*ch_2, 2);

let ch_3: ChUnit = ch(1) - ch(1);
assert_eq2!(*ch_3, 0);

let ch_4: ChUnit = ch(0) - ch(1);
assert_eq2!(*ch_4, 0);
}

#[test]
fn test_from_ch_into_usize() {
let usize_1: usize = usize(ch(1));
assert_eq2!(usize_1, 1);

let usize_2: usize = usize(ch(1) + ch(1));
assert_eq2!(usize_2, 2);

let usize_3: usize = usize(ch(1) - ch(1));
assert_eq2!(usize_3, 0);

let usize_4: usize = usize(ch(0) - ch(1));
assert_eq2!(usize_4, 0);
}

#[test]
fn test_from_ch_into_u16() {
let u16_1: u16 = u16(ch(1));
assert_eq2!(u16_1, 1);

let u16_2: u16 = u16(ch(1) + ch(1));
assert_eq2!(u16_2, 2);

let u16_3: u16 = u16(ch(1) - ch(1));
assert_eq2!(u16_3, 0);

let u16_4: u16 = u16(ch(0) - ch(1));
assert_eq2!(u16_4, 0);
}
}
8 changes: 2 additions & 6 deletions core/src/tui_core/dimens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
pub mod ch_unit;
pub mod percent;
pub mod position;
pub mod requested_size;
pub mod requested_size_percent;
pub mod size;

// Re-export.
pub use ch_unit::*;
pub use percent::*;
pub use position::*;
pub use requested_size::*;
pub use requested_size_percent::*;
pub use size::*;

// Tests.
mod test_ch_unit;
mod test_dimens;
61 changes: 61 additions & 0 deletions core/src/tui_core/dimens/percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,64 @@ impl Percent {
}
}
}

#[cfg(test)]
mod tests {
use crate::{Percent,
STATS_25P_GLYPH,
STATS_50P_GLYPH,
STATS_75P_GLYPH,
STATS_100P_GLYPH,
ch};

#[test]
fn test_percent_works_as_expected() {
let maybe_pc_100 = percent!(100i32);
if let Ok(pc_100) = maybe_pc_100 {
assert_eq!(*pc_100, 100);
let result = pc_100.apply_to(ch(500));
assert_eq!(*result, 500);
} else {
panic!("Failed to create Percent from 100");
}

let pc_50 = Percent::try_from(50i32).unwrap();
assert_eq!(*pc_50, 50);
let result = pc_50.apply_to(ch(500));
assert_eq!(*result, 250);

let pc_0 = Percent::try_from(0i32).unwrap();
assert_eq!(*pc_0, 0);
let result = pc_0.apply_to(ch(500));
assert_eq!(*result, 0);
}

#[test]
fn test_percent_parsing_fails_as_expected() {
Percent::try_from(-1i32).unwrap_err();

Percent::try_from(0i32).unwrap();
Percent::try_from(0u16).unwrap();

Percent::try_from(100i32).unwrap();
Percent::try_from(100u16).unwrap();

Percent::try_from(101i32).unwrap_err();
Percent::try_from(101u16).unwrap_err();
}

#[test]
fn test_percent_to_glyph_works_as_expected() {
let pc_0_to_25 = percent!(25i32).unwrap();
assert_eq!(pc_0_to_25.as_glyph(), STATS_25P_GLYPH);

let pc_25_to_50 = percent!(50i32).unwrap();
assert_eq!(pc_25_to_50.as_glyph(), STATS_50P_GLYPH);

let pc_50_to_75 = percent!(75i32).unwrap();
assert_eq!(pc_50_to_75.as_glyph(), STATS_75P_GLYPH);

let pc_100 = percent!(100i32).unwrap();
assert_eq!(pc_100.as_glyph(), STATS_100P_GLYPH);
}
}
9 changes: 2 additions & 7 deletions core/src/tui_core/dimens/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use std::{fmt::{self, Debug},
ops::{Add, AddAssign, Mul}};
use std::{fmt::{Debug, Formatter, Result},
ops::{Add, AddAssign, Mul, Sub}};

use crate::{ChUnit, Size, ch};

Expand Down Expand Up @@ -144,8 +143,6 @@ impl Position {
}

pub mod position_math_ops {
use std::ops::Sub;

use super::*;

impl AddAssign<ChUnit> for Position {
Expand Down Expand Up @@ -216,8 +213,6 @@ pub mod convert_position_to_other_type {
}

pub mod position_debug_formatter {
use fmt::{Formatter, Result};

use super::*;

impl Debug for Position {
Expand Down
File renamed without changes.
66 changes: 0 additions & 66 deletions core/src/tui_core/dimens/test_ch_unit.rs

This file was deleted.

Loading

0 comments on commit c60209f

Please sign in to comment.