Skip to content

Commit c60209f

Browse files
committed
[tui] Clean up confusing caret types - raw and scroll adjusted
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.
1 parent 09ccbe1 commit c60209f

23 files changed

+260
-329
lines changed

.vscode/bookmarks.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"mworld",
9999
"nanos",
100100
"neovim",
101+
"nextest",
101102
"Nodesource",
102103
"notcurses",
103104
"nushell",

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/stack_alloc_types/sizes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use smallvec::SmallVec;
2121
/// Intermediate struct used to insert a grapheme cluster segment into an existing unicode
2222
/// string. When this gets larger than `DEFAULT_STRING_SIZE`, it will be
2323
/// [smallvec::SmallVec::spilled] on the heap.
24-
pub type VecStrBuffer<'a> = SmallVec<[&'a str; VEC_STR_BUFFER_CAPACITY]>;
24+
pub type VecArrayStr<'a> = SmallVec<[&'a str; VEC_STR_BUFFER_CAPACITY]>;
2525
const VEC_STR_BUFFER_CAPACITY: usize = 16;
2626

2727
/// Stack allocated string storage for small strings. When this gets larger than

core/src/tui_core/dimens/ch_unit.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub struct ChUnit {
4242

4343
impl ChUnit {
4444
pub fn new(value: ChUnitPrimitiveType) -> Self { Self { value } }
45+
46+
pub fn reset(&mut self) { self.value = 0; }
4547
}
4648

4749
/// ```rust
@@ -291,3 +293,53 @@ pub mod convert_from_other_types_to_ch {
291293
}
292294
}
293295
}
296+
297+
#[cfg(test)]
298+
mod tests {
299+
use crate::{ChUnit, assert_eq2, ch, u16, usize};
300+
301+
#[test]
302+
fn test_from_whatever_into_ch() {
303+
let ch_1: ChUnit = ch(1);
304+
assert_eq2!(*ch_1, 1);
305+
306+
let ch_2: ChUnit = ch(1) + ch(1);
307+
assert_eq2!(*ch_2, 2);
308+
309+
let ch_3: ChUnit = ch(1) - ch(1);
310+
assert_eq2!(*ch_3, 0);
311+
312+
let ch_4: ChUnit = ch(0) - ch(1);
313+
assert_eq2!(*ch_4, 0);
314+
}
315+
316+
#[test]
317+
fn test_from_ch_into_usize() {
318+
let usize_1: usize = usize(ch(1));
319+
assert_eq2!(usize_1, 1);
320+
321+
let usize_2: usize = usize(ch(1) + ch(1));
322+
assert_eq2!(usize_2, 2);
323+
324+
let usize_3: usize = usize(ch(1) - ch(1));
325+
assert_eq2!(usize_3, 0);
326+
327+
let usize_4: usize = usize(ch(0) - ch(1));
328+
assert_eq2!(usize_4, 0);
329+
}
330+
331+
#[test]
332+
fn test_from_ch_into_u16() {
333+
let u16_1: u16 = u16(ch(1));
334+
assert_eq2!(u16_1, 1);
335+
336+
let u16_2: u16 = u16(ch(1) + ch(1));
337+
assert_eq2!(u16_2, 2);
338+
339+
let u16_3: u16 = u16(ch(1) - ch(1));
340+
assert_eq2!(u16_3, 0);
341+
342+
let u16_4: u16 = u16(ch(0) - ch(1));
343+
assert_eq2!(u16_4, 0);
344+
}
345+
}

core/src/tui_core/dimens/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919
pub mod ch_unit;
2020
pub mod percent;
2121
pub mod position;
22-
pub mod requested_size;
22+
pub mod requested_size_percent;
2323
pub mod size;
2424

2525
// Re-export.
2626
pub use ch_unit::*;
2727
pub use percent::*;
2828
pub use position::*;
29-
pub use requested_size::*;
29+
pub use requested_size_percent::*;
3030
pub use size::*;
31-
32-
// Tests.
33-
mod test_ch_unit;
34-
mod test_dimens;

core/src/tui_core/dimens/percent.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,64 @@ impl Percent {
158158
}
159159
}
160160
}
161+
162+
#[cfg(test)]
163+
mod tests {
164+
use crate::{Percent,
165+
STATS_25P_GLYPH,
166+
STATS_50P_GLYPH,
167+
STATS_75P_GLYPH,
168+
STATS_100P_GLYPH,
169+
ch};
170+
171+
#[test]
172+
fn test_percent_works_as_expected() {
173+
let maybe_pc_100 = percent!(100i32);
174+
if let Ok(pc_100) = maybe_pc_100 {
175+
assert_eq!(*pc_100, 100);
176+
let result = pc_100.apply_to(ch(500));
177+
assert_eq!(*result, 500);
178+
} else {
179+
panic!("Failed to create Percent from 100");
180+
}
181+
182+
let pc_50 = Percent::try_from(50i32).unwrap();
183+
assert_eq!(*pc_50, 50);
184+
let result = pc_50.apply_to(ch(500));
185+
assert_eq!(*result, 250);
186+
187+
let pc_0 = Percent::try_from(0i32).unwrap();
188+
assert_eq!(*pc_0, 0);
189+
let result = pc_0.apply_to(ch(500));
190+
assert_eq!(*result, 0);
191+
}
192+
193+
#[test]
194+
fn test_percent_parsing_fails_as_expected() {
195+
Percent::try_from(-1i32).unwrap_err();
196+
197+
Percent::try_from(0i32).unwrap();
198+
Percent::try_from(0u16).unwrap();
199+
200+
Percent::try_from(100i32).unwrap();
201+
Percent::try_from(100u16).unwrap();
202+
203+
Percent::try_from(101i32).unwrap_err();
204+
Percent::try_from(101u16).unwrap_err();
205+
}
206+
207+
#[test]
208+
fn test_percent_to_glyph_works_as_expected() {
209+
let pc_0_to_25 = percent!(25i32).unwrap();
210+
assert_eq!(pc_0_to_25.as_glyph(), STATS_25P_GLYPH);
211+
212+
let pc_25_to_50 = percent!(50i32).unwrap();
213+
assert_eq!(pc_25_to_50.as_glyph(), STATS_50P_GLYPH);
214+
215+
let pc_50_to_75 = percent!(75i32).unwrap();
216+
assert_eq!(pc_50_to_75.as_glyph(), STATS_75P_GLYPH);
217+
218+
let pc_100 = percent!(100i32).unwrap();
219+
assert_eq!(pc_100.as_glyph(), STATS_100P_GLYPH);
220+
}
221+
}

core/src/tui_core/dimens/position.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
18-
use std::{fmt::{self, Debug},
19-
ops::{Add, AddAssign, Mul}};
17+
use std::{fmt::{Debug, Formatter, Result},
18+
ops::{Add, AddAssign, Mul, Sub}};
2019

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

@@ -144,8 +143,6 @@ impl Position {
144143
}
145144

146145
pub mod position_math_ops {
147-
use std::ops::Sub;
148-
149146
use super::*;
150147

151148
impl AddAssign<ChUnit> for Position {
@@ -216,8 +213,6 @@ pub mod convert_position_to_other_type {
216213
}
217214

218215
pub mod position_debug_formatter {
219-
use fmt::{Formatter, Result};
220-
221216
use super::*;
222217

223218
impl Debug for Position {

core/src/tui_core/dimens/test_ch_unit.rs

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)