From 4df9d3e7ff71554d0a636b6f54b2132cce2860d7 Mon Sep 17 00:00:00 2001 From: Thomas Eppers Date: Wed, 1 Sep 2021 20:03:49 +0200 Subject: [PATCH] changed struct of TagList to more sensible options --- src/main.rs | 2 +- src/ui.rs | 21 ++++++------ src/widget/service_switcher.rs | 6 ++-- src/widget/tag_list.rs | 58 ++++++++++++++++++---------------- 4 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8241206..6374c95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,5 +3,5 @@ mod ui; mod widget; fn main() { - ui::Ui::run("rocketchat/rocket.chat"); + ui::Ui::run("enter a repository or select one from docker-compose.yml"); } diff --git a/src/ui.rs b/src/ui.rs index 18fef50..7721926 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -41,10 +41,10 @@ impl Ui { let mut ui = Ui { state: State::SelectService, repo: repo_entry::RepoEntry::new(repo_id), - tags: tag_list::TagList::new_line("Fetching Tags"), + tags: tag_list::TagList::with_status("Fetching Tags"), services: service_switcher::ServiceSwitcher::new(), }; - ui.tags = tag_list::TagList::new(ui.repo.get()); + ui.tags = tag_list::TagList::with_repo(ui.repo.get()); //setup tui let stdout = io::stdout().into_raw_mode().unwrap(); @@ -92,11 +92,14 @@ impl Ui { Ok(Key::Char('\n')) => match ui.state { State::EditRepo => { ui.repo.confirm(); - ui.tags = tag_list::TagList::new(ui.repo.get()); + ui.tags = tag_list::TagList::with_repo(ui.repo.get()); } State::SelectTag => { let mut repo = ui.services.extract_repo().unwrap(); - let tag = ui.tags.get_selected().unwrap(); + let tag = match ui.tags.get_selected() { + Err(_) => continue, + Ok(tag) => tag, + }; repo.push_str(":"); repo.push_str(&tag); ui.services.change_current_line(repo); @@ -105,14 +108,14 @@ impl Ui { }, Ok(Key::Char(key)) => { if ui.state == State::EditRepo { - ui.tags = tag_list::TagList::new_line("Editing Repository"); + ui.tags = tag_list::TagList::with_status("Editing Repository"); } ui.repo.handle_input(&ui.state, Key::Char(key)); ui.tags.handle_input(&ui.state, Key::Char(key)); } Ok(Key::Backspace) => { if ui.state == State::EditRepo { - ui.tags = tag_list::TagList::new_line("Editing Repository"); + ui.tags = tag_list::TagList::with_status("Editing Repository"); } ui.repo.handle_input(&ui.state, Key::Backspace); ui.tags.handle_input(&ui.state, Key::Backspace); @@ -120,7 +123,7 @@ impl Ui { Ok(Key::Up) => { if ui.state == State::SelectService && ui.services.find_previous_match() { match ui.services.extract_repo() { - Err(_) => ui.tags = tag_list::TagList::new_line("no image found"), + Err(_) => ui.tags = tag_list::TagList::with_status("no image found"), Ok(s) => ui.repo.set(s), } } @@ -130,10 +133,10 @@ impl Ui { Ok(Key::Down) => match ui.state { State::SelectService if ui.services.find_next_match() => { match ui.services.extract_repo() { - Err(_) => ui.tags = tag_list::TagList::new_line("no image found"), + Err(_) => ui.tags = tag_list::TagList::with_status("no image found"), Ok(s) => { ui.repo.set(s); - ui.tags = tag_list::TagList::new(ui.repo.get()); + ui.tags = tag_list::TagList::with_repo(ui.repo.get()); } } } diff --git a/src/widget/service_switcher.rs b/src/widget/service_switcher.rs index 2785552..0af2c43 100644 --- a/src/widget/service_switcher.rs +++ b/src/widget/service_switcher.rs @@ -25,7 +25,7 @@ pub struct ServiceSwitcher { impl ServiceSwitcher { pub fn new() -> Self { let list = match File::open("docker-compose.yml") { - Err(e) => vec![format!("no docker-compose.yml: {}", e)], + Err(e) => vec![format!("No docker-compose.yml found: {}", e)], Ok(file) => { let buf = BufReader::new(file); buf.lines() @@ -50,8 +50,8 @@ impl ServiceSwitcher { }; let title = match &self.changed { - true => "*docker-compose.yml*", - false => "docker-compose.yml", + true => "File: *docker-compose.yml*", + false => "File: docker-compose.yml", }; let items: Vec = self diff --git a/src/widget/tag_list.rs b/src/widget/tag_list.rs index b46f925..832893c 100644 --- a/src/widget/tag_list.rs +++ b/src/widget/tag_list.rs @@ -6,8 +6,7 @@ use crate::tags; use crate::ui::State; pub struct TagList { - tags: Option, - line: String, + typ: Type, state: ListState, } @@ -16,46 +15,51 @@ pub enum Error { NoTags, } -impl TagList { - pub fn new(repo: String) -> Self { - let (tags, line) = match tags::Tags::new(repo) { - Err(_) => (None, String::from("Could not query tags")), - Ok(tags) => (Some(tags), String::new()), - }; +pub enum Type { + Status(String), + Repo(tags::Tags), +} +impl TagList { + pub fn new(typ: Type) -> Self { Self { - tags, - line, + typ, state: ListState::default(), } } - pub fn new_line(line: &str) -> Self { - Self { - tags: None, - line: String::from(line), - state: ListState::default(), + pub fn with_status(status: &str) -> Self { + Self::new(Type::Status(String::from(status))) + } + + pub fn with_repo(name: String) -> Self { + match tags::Tags::new(name) { + Err(_) => Self::with_status("Couldn't query tags: no images found"), + Ok(tags) => Self::new(Type::Repo(tags)), } } fn print_lines(&self) -> Vec { - match &self.tags { - None => vec![self.line.clone()], - Some(tags) => tags.results.iter().map(|r| format!("{}", r)).collect(), + match &self.typ { + Type::Status(line) => vec![line.to_string()], + Type::Repo(tags) => tags.results.iter().map(|r| format!("{}", r)).collect(), } } pub fn get_names(&self) -> Result, Error> { - match &self.tags { - None => Err(Error::NoTags), - Some(tags) => Ok(tags.results.iter().map(|r| r.tag_name.clone()).collect()), + match &self.typ { + Type::Status(_) => Err(Error::NoTags), + Type::Repo(tags) => Ok(tags.results.iter().map(|r| r.tag_name.clone()).collect()), } } pub fn get_selected(&self) -> Result { - match self.state.selected() { - None => Err(Error::NoTags), - Some(i) => Ok(self.get_names().unwrap()[i].clone()), + match &self.typ { + Type::Status(_) => Err(Error::NoTags), + Type::Repo(_) => match self.state.selected() { + None => Err(Error::NoTags), + Some(i) => Ok(self.get_names().unwrap()[i].clone()), + }, } } @@ -66,9 +70,9 @@ impl TagList { Style::default().fg(Color::Gray) }; - let lines = match &self.tags { - None => vec![self.line.clone()], - Some(_) => self.print_lines(), + let lines = match &self.typ { + Type::Status(line) => vec![line.clone()], + Type::Repo(_) => self.print_lines(), }; let items: Vec = lines