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;
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 {
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());
}
}
}

View File

@ -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<tui::widgets::ListItem> = self

View File

@ -6,8 +6,7 @@ use crate::tags;
use crate::ui::State;
pub struct TagList {
tags: Option<tags::Tags>,
line: String,
typ: Type,
state: ListState,
}
@ -16,46 +15,51 @@ pub enum Error {
NoTags,
}
pub enum Type {
Status(String),
Repo(tags::Tags),
}
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 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<String> {
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<Vec<String>, 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<String, Error> {
match self.state.selected() {
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<tui::widgets::ListItem> = lines