Skip to main content

gitkraft_core/utils/
mod.rs

1//! Utility helpers shared across the crate — OID formatting, relative time, text, etc.
2
3pub mod text;
4pub mod time;
5
6pub use text::truncate_str;
7pub use time::{fmt_oid, relative_time, short_oid, short_oid_str};
8
9/// Build an ascending `Vec<usize>` spanning from `anchor` to `target`
10/// (inclusive), regardless of which is larger.
11///
12/// Used for range-selection in commit and file lists in both GUI and TUI.
13///
14/// # Examples
15/// ```
16/// assert_eq!(gitkraft_core::ascending_range(3, 7), vec![3, 4, 5, 6, 7]);
17/// assert_eq!(gitkraft_core::ascending_range(7, 3), vec![3, 4, 5, 6, 7]);
18/// assert_eq!(gitkraft_core::ascending_range(5, 5), vec![5]);
19/// ```
20pub fn ascending_range(anchor: usize, target: usize) -> Vec<usize> {
21    let (start, end) = if anchor <= target {
22        (anchor, target)
23    } else {
24        (target, anchor)
25    };
26    (start..=end).collect()
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn ascending_range_low_to_high() {
35        assert_eq!(ascending_range(2, 5), vec![2, 3, 4, 5]);
36    }
37
38    #[test]
39    fn ascending_range_high_to_low_is_still_ascending() {
40        assert_eq!(ascending_range(5, 2), vec![2, 3, 4, 5]);
41    }
42
43    #[test]
44    fn ascending_range_same_returns_single() {
45        assert_eq!(ascending_range(4, 4), vec![4]);
46    }
47}