changed struct of TagList to more sensible options

This commit is contained in:
Thomas Eppers 2021-09-01 20:03:49 +02:00
parent a7c3c66b2a
commit 4df9d3e7ff
4 changed files with 47 additions and 40 deletions

View File

@ -3,5 +3,5 @@ mod ui;
mod widget; mod widget;
fn main() { fn main() {
ui::Ui::run("rocketchat/rocket.chat"); ui::Ui::run("enter a repository or select one from docker-compose.yml");
} }

View File

@ -41,10 +41,10 @@ impl Ui {
let mut ui = Ui { let mut ui = Ui {
state: State::SelectService, state: State::SelectService,
repo: repo_entry::RepoEntry::new(repo_id), 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(), 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 //setup tui
let stdout = io::stdout().into_raw_mode().unwrap(); let stdout = io::stdout().into_raw_mode().unwrap();
@ -92,11 +92,14 @@ impl Ui {
Ok(Key::Char('\n')) => match ui.state { Ok(Key::Char('\n')) => match ui.state {
State::EditRepo => { State::EditRepo => {
ui.repo.confirm(); ui.repo.confirm();
ui.tags = tag_list::TagList::new(ui.repo.get()); ui.tags = tag_list::TagList::with_repo(ui.repo.get());
} }
State::SelectTag => { State::SelectTag => {
let mut repo = ui.services.extract_repo().unwrap(); 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(":");
repo.push_str(&tag); repo.push_str(&tag);
ui.services.change_current_line(repo); ui.services.change_current_line(repo);
@ -105,14 +108,14 @@ impl Ui {
}, },
Ok(Key::Char(key)) => { Ok(Key::Char(key)) => {
if ui.state == State::EditRepo { 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.repo.handle_input(&ui.state, Key::Char(key));
ui.tags.handle_input(&ui.state, Key::Char(key)); ui.tags.handle_input(&ui.state, Key::Char(key));
} }
Ok(Key::Backspace) => { Ok(Key::Backspace) => {
if ui.state == State::EditRepo { 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.repo.handle_input(&ui.state, Key::Backspace);
ui.tags.handle_input(&ui.state, Key::Backspace); ui.tags.handle_input(&ui.state, Key::Backspace);
@ -120,7 +123,7 @@ impl Ui {
Ok(Key::Up) => { Ok(Key::Up) => {
if ui.state == State::SelectService && ui.services.find_previous_match() { if ui.state == State::SelectService && ui.services.find_previous_match() {
match ui.services.extract_repo() { 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), Ok(s) => ui.repo.set(s),
} }
} }
@ -130,10 +133,10 @@ impl Ui {
Ok(Key::Down) => match ui.state { Ok(Key::Down) => match ui.state {
State::SelectService if ui.services.find_next_match() => { State::SelectService if ui.services.find_next_match() => {
match ui.services.extract_repo() { 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) => { Ok(s) => {
ui.repo.set(s); ui.repo.set(s);
ui.tags = tag_list::TagList::new(ui.repo.get()); ui.tags = tag_list::TagList::with_repo(ui.repo.get());
} }
} }
} }

View File

@ -25,7 +25,7 @@ pub struct ServiceSwitcher {
impl ServiceSwitcher { impl ServiceSwitcher {
pub fn new() -> Self { pub fn new() -> Self {
let list = match File::open("docker-compose.yml") { 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) => { Ok(file) => {
let buf = BufReader::new(file); let buf = BufReader::new(file);
buf.lines() buf.lines()
@ -50,8 +50,8 @@ impl ServiceSwitcher {
}; };
let title = match &self.changed { let title = match &self.changed {
true => "*docker-compose.yml*", true => "File: *docker-compose.yml*",
false => "docker-compose.yml", false => "File: docker-compose.yml",
}; };
let items: Vec<tui::widgets::ListItem> = self let items: Vec<tui::widgets::ListItem> = self

View File

@ -6,8 +6,7 @@ use crate::tags;
use crate::ui::State; use crate::ui::State;
pub struct TagList { pub struct TagList {
tags: Option<tags::Tags>, typ: Type,
line: String,
state: ListState, state: ListState,
} }
@ -16,46 +15,51 @@ pub enum Error {
NoTags, NoTags,
} }
impl TagList { pub enum Type {
pub fn new(repo: String) -> Self { Status(String),
let (tags, line) = match tags::Tags::new(repo) { Repo(tags::Tags),
Err(_) => (None, String::from("Could not query tags")), }
Ok(tags) => (Some(tags), String::new()),
};
impl TagList {
pub fn new(typ: Type) -> Self {
Self { Self {
tags, typ,
line,
state: ListState::default(), state: ListState::default(),
} }
} }
pub fn new_line(line: &str) -> Self { pub fn with_status(status: &str) -> Self {
Self { Self::new(Type::Status(String::from(status)))
tags: None, }
line: String::from(line),
state: ListState::default(), 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<String> { fn print_lines(&self) -> Vec<String> {
match &self.tags { match &self.typ {
None => vec![self.line.clone()], Type::Status(line) => vec![line.to_string()],
Some(tags) => tags.results.iter().map(|r| format!("{}", r)).collect(), Type::Repo(tags) => tags.results.iter().map(|r| format!("{}", r)).collect(),
} }
} }
pub fn get_names(&self) -> Result<Vec<String>, Error> { pub fn get_names(&self) -> Result<Vec<String>, Error> {
match &self.tags { match &self.typ {
None => Err(Error::NoTags), Type::Status(_) => Err(Error::NoTags),
Some(tags) => Ok(tags.results.iter().map(|r| r.tag_name.clone()).collect()), Type::Repo(tags) => Ok(tags.results.iter().map(|r| r.tag_name.clone()).collect()),
} }
} }
pub fn get_selected(&self) -> Result<String, Error> { pub fn get_selected(&self) -> Result<String, Error> {
match self.state.selected() { match &self.typ {
None => Err(Error::NoTags), Type::Status(_) => Err(Error::NoTags),
Some(i) => Ok(self.get_names().unwrap()[i].clone()), 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) Style::default().fg(Color::Gray)
}; };
let lines = match &self.tags { let lines = match &self.typ {
None => vec![self.line.clone()], Type::Status(line) => vec![line.clone()],
Some(_) => self.print_lines(), Type::Repo(_) => self.print_lines(),
}; };
let items: Vec<tui::widgets::ListItem> = lines let items: Vec<tui::widgets::ListItem> = lines