lib-nested/nested/src/editors/product/segment.rs

83 lines
2.6 KiB
Rust
Raw Normal View History

2022-05-08 23:30:49 +02:00
use {
2023-02-13 18:39:45 +01:00
r3vi::{
view::{
OuterViewPort
}
},
2022-05-08 23:30:49 +02:00
crate::{
2023-08-12 19:03:14 +02:00
type_system::{Context, TypeTerm},
2022-05-08 23:30:49 +02:00
terminal::{
2022-12-18 01:39:01 +01:00
TerminalStyle, TerminalView,
2022-05-08 23:30:49 +02:00
make_label
},
2023-01-02 18:49:32 +01:00
utils::color::{bg_style_from_depth, fg_style_from_depth},
2022-12-19 11:26:07 +01:00
tree::{NestedNode, TreeNav}
2022-05-08 23:30:49 +02:00
},
2022-12-18 01:39:01 +01:00
std::{sync::{Arc, RwLock}},
2022-05-08 23:30:49 +02:00
};
#[derive(Clone)]
2022-06-24 15:54:05 +02:00
pub enum ProductEditorSegment {
2022-06-19 23:13:21 +02:00
T( String, usize ),
2022-05-08 23:30:49 +02:00
N {
2023-08-12 19:03:14 +02:00
t: TypeTerm,
2022-12-19 11:26:07 +01:00
editor: Option<NestedNode>,
ed_depth: usize,
cur_depth: usize,
cur_dist: isize
2022-05-08 23:30:49 +02:00
}
}
2022-06-24 15:54:05 +02:00
impl ProductEditorSegment {
2022-05-08 23:30:49 +02:00
pub fn get_view(&self, ctx: Arc<RwLock<Context>>) -> OuterViewPort<dyn TerminalView> {
match self {
2022-06-24 15:54:05 +02:00
ProductEditorSegment::T(t, depth) =>
2022-05-08 23:30:49 +02:00
make_label(t.as_str())
2022-06-19 23:13:21 +02:00
.map_item({
let depth = *depth;
2022-12-18 01:39:01 +01:00
move |_i, x|
x.add_style_back(fg_style_from_depth(depth)).add_style_back(TerminalStyle::italic(true))
2022-06-19 23:13:21 +02:00
}
),
2022-05-08 23:30:49 +02:00
2022-12-18 01:39:01 +01:00
ProductEditorSegment::N { t: _, editor: Some(e), ed_depth, cur_depth: _, cur_dist } =>
2022-12-19 11:26:07 +01:00
e.get_view()
2022-08-12 18:57:40 +02:00
.map_item({
let e = e.clone();
let d = *ed_depth;
let cur_dist = *cur_dist;
2022-12-18 01:39:01 +01:00
move |_i, x| {
2022-12-19 11:26:07 +01:00
let c = e.get_cursor();
let cur_depth = c.tree_addr.len();
let select =
2022-10-15 03:52:38 +02:00
if cur_dist == 0 {
cur_depth
} else {
2022-10-15 03:52:38 +02:00
usize::MAX
};
return x
2022-10-15 03:52:38 +02:00
.add_style_back(bg_style_from_depth(select))
.add_style_back(TerminalStyle::bold(select==1))
.add_style_back(fg_style_from_depth(d));
2022-08-12 18:57:40 +02:00
}
2022-05-08 23:30:49 +02:00
}),
ProductEditorSegment::N{ t, editor: None, ed_depth, cur_depth, cur_dist } =>
2023-08-12 19:03:14 +02:00
make_label(&ctx.read().unwrap().type_term_to_str(t))
2022-06-19 23:13:21 +02:00
.map_item({
2022-12-18 01:39:01 +01:00
let _cur_depth = *cur_depth;
let _ed_depth = *ed_depth;
let cur_dist = *cur_dist;
2022-12-18 01:39:01 +01:00
move |_i, x|
2022-10-15 03:52:38 +02:00
x.add_style_back(TerminalStyle::fg_color((215,140,95)))
.add_style_back(bg_style_from_depth(if cur_dist == 0 { 0 } else { usize::MAX }))
.add_style_back(TerminalStyle::bold(cur_dist == 0))
2022-05-08 23:30:49 +02:00
})
}
}
}