simpler value getters

This commit is contained in:
i do not exist 2026-07-25 08:46:53 +03:00
parent b19d5bef24
commit 711e4fe6a9
3 changed files with 37 additions and 39 deletions

View file

@ -73,30 +73,22 @@ fn get_arc_str (_state: &App, src: impl Language) -> Perhaps<Arc<str>> {
} }
fn get_u16 (state: &App, src: impl Language) -> Perhaps<u16> { fn get_u16 (state: &App, src: impl Language) -> Perhaps<u16> {
if let Some(sym) = src.word()? { Ok(Some(match src.word()? {
Ok(Some(match sym { Some(":w/sidebar") => state.project.w_sidebar(state.editor().is_some()),
":w/sidebar" => state.project.w_sidebar(state.editor().is_some()), Some(":h/sample-detail") => 6.max(state.size.h() as u16 * 3 / 9),
":h/sample-detail" => 6.max(state.size.h() as u16 * 3 / 9), _ => return try_to_u16(src)
_ => return Err(format!("not a u16: {sym}").into()) }))
}))
} else {
try_to_u16(src)
}
} }
fn get_usize (state: &App, src: impl Language) -> Perhaps<usize> { fn get_usize (state: &App, src: impl Language) -> Perhaps<usize> {
if let Some(sym) = src.word()? { Ok(Some(match src.word()? {
Ok(Some(match sym { Some(":scene-count") => state.scenes().len(),
":scene-count" => state.scenes().len(), Some(":track-count") => state.tracks().len(),
":track-count" => state.tracks().len(), Some(":device-kind") => state.dialog.device_kind().unwrap_or(0),
":device-kind" => state.dialog.device_kind().unwrap_or(0), Some(":device-kind/next") => state.dialog.device_kind_next().unwrap_or(0),
":device-kind/next" => state.dialog.device_kind_next().unwrap_or(0), Some(":device-kind/prev") => state.dialog.device_kind_prev().unwrap_or(0),
":device-kind/prev" => state.dialog.device_kind_prev().unwrap_or(0), _ => return try_to_usize(src)
_ => return Err(format!("not usize: {sym}").into()) }))
}))
} else {
try_to_usize(src)
}
} }
fn get_bool (state: &App, src: impl Language) -> Perhaps<bool> { fn get_bool (state: &App, src: impl Language) -> Perhaps<bool> {
@ -132,33 +124,37 @@ fn get_selection (state: &App, src: impl Language) -> Perhaps<Selection> {
})).transpose() })).transpose()
} }
fn get_color (state: &App, src: impl Language) -> Perhaps<Color> { fn get_color (_state: &App, src: impl Language) -> Perhaps<Color> {
Ok(if let Some(sym) = src.word()? { Ok(if let Ok(Some(expr)) = src.expr() {
match sym {
":color/bg" => Some(Color::Rgb(28, 32, 36)),
_ => return Err(format!("not a color: {sym}").into())
}
} else if let Some(expr) = src.expr()? {
match expr.head()? { match expr.head()? {
Some("g") => { Some("g") => {
let n = try_to_u8(expr.tail())? let n = try_to_u8(expr.tail())?.ok_or::<Box<dyn Error>>(
.ok_or::<Box<dyn Error>>(format!("not gray: {expr}").into())?; format!("not gray: {expr}").into()
)?;
Some(Color::Rgb(n, n, n)) Some(Color::Rgb(n, n, n))
}, },
Some("rgb") => { Some("rgb") => {
let r = try_to_u8(expr.tail().head())? let r = try_to_u8(expr.tail().head())?.ok_or::<Box<dyn Error>>(
.ok_or::<Box<dyn Error>>(format!("not red: {expr}").into())?; format!("not red: {expr}").into()
let g = try_to_u8(expr.tail().tail().head())? )?;
.ok_or::<Box<dyn Error>>(format!("not green: {expr}").into())?; let g = try_to_u8(expr.tail().tail().head())?.ok_or::<Box<dyn Error>>(
let b = try_to_u8(expr.tail().tail().tail().head())? format!("not green: {expr}").into()
.ok_or::<Box<dyn Error>>(format!("not blue: {expr}").into())?; )?;
let b = try_to_u8(expr.tail().tail().tail().head())?.ok_or::<Box<dyn Error>>(
format!("not blue: {expr}").into()
)?;
Some(Color::Rgb(r, g, b)) Some(Color::Rgb(r, g, b))
}, },
Some(_) => return Err(format!("not a color expression: {expr}").into()), Some(_) => return Err(format!("not a color expression: {expr}").into()),
None => return Err(format!("not a color expression: {expr}").into()) None => return Err(format!("not a color expression: {expr}").into())
} }
} else if let Ok(Some(sym)) = src.word() {
match sym {
":color/bg" => Some(Color::Rgb(28, 32, 36)),
_ => return Err(format!("not a color: {sym}").into())
}
} else { } else {
None return Err(format!("not a color: {:?}", src.src()?).into())
}) })
} }

View file

@ -14,7 +14,9 @@ impl View<Tui> for App {
} }
for (index, dsl) in self.mode.view.iter().enumerate() { for (index, dsl) in self.mode.view.iter().enumerate() {
if let Err(e) = self.interpret(to, dsl) { if let Err(e) = self.interpret(to, dsl) {
*self.error.write().unwrap() = Some(format!("view #{index}: {e}").into()); *self.error.write().unwrap() = Some(format!(
"mode {:?} view #{index}: {e}", &self.mode.name,
).into());
break; break;
} }
} }

2
tengri

@ -1 +1 @@
Subproject commit be98e666f53eedf95618396239641ad846a772bd Subproject commit ad1a514eddbf3104b7631dedef786389863988bf