some issue

This commit is contained in:
2024-10-31 19:00:39 +01:00
parent 92ab35c4ef
commit 55f52f786f
4 changed files with 83 additions and 35 deletions
+2 -1
View File
@@ -86,8 +86,9 @@ impl App {
match self.element.update(key::read_key()) { match self.element.update(key::read_key()) {
UpdateResponse::Exit => { UpdateResponse::Exit => {
crossterm::terminal::disable_raw_mode().unwrap(); crossterm::terminal::disable_raw_mode().unwrap();
utils::show_cursor();
utils::clear(); utils::clear();
utils::show_cursor();
println!("");
return; return;
} }
UpdateResponse::Tick(ticks) => { UpdateResponse::Tick(ticks) => {
+43 -13
View File
@@ -1,23 +1,53 @@
use osui::{oml, ui::*, App}; use osui::{key::KeyKind, oml, ui::*, App};
fn main() { fn main() {
let mut app_screen = App::new(); let mut app_screen = App::new();
app_screen.set_component(app()); app_screen.set_component(oml!(
app_screen.run();
tab {
div {
// Some welcome text
text("Welcome! This is the OSUI Tutorial! Press tab to go to the next Page or press Shift+Tab to go to a previous page";)
} }
fn app() -> Box<Tab> { // A button example With a div
oml!(
tab { div {
text("Hello!"; style = Style { text("On the bottom there is a button. To hover it click the down key, Then press enter to select it";)
hover_fg: Color::Red,
// Button
button("Click me!"; y = 3, style = Style {
hover_fg: Color::Magenta,
..Default::default() ..Default::default()
}, on_click = |btn| {
btn.text = "Press tab to go to the next page".to_string();
}) })
button("Hello 2!"; y = 1, style = Style {
hover_fg: Color::Green, }
..Default::default()
div {
text("This is a menu, ";)
// Button
menu(y = 2, items = vec![
String::from("Item 1"),
String::from("Item 2"),
String::from("Item 3"),
String::from("Item 4"),
String::from("Item 5"),
String::from("Item 6"),
], on_click = |m, k| {
if k.kind == KeyKind::Escape {
return ClickResponse::Exit;
} else if k.kind == KeyKind::Enter {
m.items[m.selected] = "Press Tab to go to the next page".to_string();
}
ClickResponse::None
}) })
menu(items = vec![String::from("Hello, World!"), String::from("Item 2!")])
} }
) }
));
app_screen.run();
} }
+25 -9
View File
@@ -207,9 +207,9 @@ element! {
let mut v: String = " ".repeat((self.width / 2) - self.children.len()); let mut v: String = " ".repeat((self.width / 2) - self.children.len());
for (i, _) in (&self.children).into_iter().enumerate() { for (i, _) in (&self.children).into_iter().enumerate() {
if i == self.child { if i == self.child {
v += self.style.write_selected("*").as_str() v += self.style.write_selected("•").as_str()
} else { } else {
v += self.style.write("*").as_str() v += self.style.write("•").as_str()
} }
} }
format!("{}\n{}", v, frame.join("\n")) format!("{}\n{}", v, frame.join("\n"))
@@ -233,11 +233,16 @@ element! {
pub items: Vec<String>, pub items: Vec<String>,
pub selected: usize, pub selected: usize,
pub on_click: fn(&mut Menu, _: Key) -> ClickResponse, pub on_click: fn(&mut Menu, _: Key) -> ClickResponse,
pub binds: HashMap<KeyKind, String>,
} }
defaults { defaults {
items: Vec::new(), items: Vec::new(),
selected: 0, selected: 0,
on_click: |_, _|ClickResponse::None on_click: |_, _|ClickResponse::None,
binds: HashMap::from([
(KeyKind::Char(String::from("w")), String::from("up")),
(KeyKind::Char(String::from("s")), String::from("down"))
])
} }
fn render(&mut self) -> String { fn render(&mut self) -> String {
let mut res: Vec<String> = Vec::new(); let mut res: Vec<String> = Vec::new();
@@ -257,17 +262,28 @@ element! {
} }
fn update(&mut self, k: Key) -> UpdateResponse { fn update(&mut self, k: Key) -> UpdateResponse {
if k.kind == KeyKind::Down { if let Some(c) = self.binds.get(&k.kind) {
match c.as_str() {
"up" => {
if self.selected > 0 {
self.selected -= 1;
} else {
self.selected = self.items.len() - 1;
}
}
"down" => {
if self.items.len() > self.selected + 1 { if self.items.len() > self.selected + 1 {
self.selected += 1; self.selected += 1;
} else { } else {
self.selected = 0; self.selected = 0;
} }
} else if k.kind == KeyKind::Up { }
if self.child > 0 { _ => {
self.selected -= 1; return match (self.on_click)(self, k) {
} else { ClickResponse::Exit => UpdateResponse::Exit,
self.selected = self.items.len() - 1; ClickResponse::None => UpdateResponse::None,
}
}
} }
} else { } else {
return match (self.on_click)(self, k) { return match (self.on_click)(self, k) {
+3 -2
View File
@@ -26,9 +26,10 @@ pub fn compress_string(input: &str, re: &Regex) -> (String, HashMap<usize, Strin
let mut i = 0; let mut i = 0;
while i < input_chars.len() { while i < input_chars.len() {
if let Some(loc) = re.find(&input[i..]) { if let Some(loc) = re.find(&(input_chars[i..]).iter().collect::<String>()) {
if loc.start() == 0 { if loc.start() == 0 {
let ansi_seq = &input[i..i + loc.end()]; let ansi_seq = &(input_chars[i..i + loc.end()]).iter().collect::<String>();
// let ansi_seq = &input[i..i + loc.end()];
matches_map.insert(res.len(), ansi_seq.to_string()); matches_map.insert(res.len(), ansi_seq.to_string());
i += ansi_seq.chars().count(); i += ansi_seq.chars().count();
} else { } else {