Compare commits

...

3 Commits

Author SHA1 Message Date
Thomas Eppers
246124d4d1 added some comments
All checks were successful
continuous-integration/woodpecker the build was successful
2021-11-03 15:58:01 +01:00
Thomas Eppers
eec1836dd3 added some comments 2021-11-03 15:39:32 +01:00
Thomas Eppers
79577de0f9 take a generic object for displaying in the info widget 2021-11-03 15:06:54 +01:00
5 changed files with 40 additions and 6 deletions

View File

@ -25,6 +25,9 @@ pub enum Repo {
}
/// check if yaml line matches and returns the split of repo string and rest
/// the first &str is the image tag
/// it will be used to not change the identation
/// the second &str will the the identifier for the image
pub fn match_yaml_image(input: &str) -> Result<(&str, &str), Error> {
lazy_static::lazy_static! {
static ref REGEX: Regex = Regex::new(r"^( +image *: *)([a-z0-9\./:]+)").unwrap();
@ -37,6 +40,7 @@ pub fn match_yaml_image(input: &str) -> Result<(&str, &str), Error> {
Ok((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()))
}
/// takes the identifier and splits off the tag it exists
pub fn split_tag_from_repo(input: &str) -> Result<(&str, &str), Error> {
lazy_static::lazy_static! {
static ref REGEX: Regex = Regex::new(r"^([a-z0-9\./[^:]]*):?([a-z0-9._\-]*)").unwrap();
@ -59,6 +63,7 @@ pub fn split_tag_from_repo(input: &str) -> Result<(&str, &str), Error> {
Ok((front, back))
}
/// takes an identifier and changes it to a Repo enum
pub fn split_repo_without_tag(repo: &str) -> Result<Repo, Error> {
let repo = repo.trim();
let split_repo: Vec<&str> = repo.split('/').collect();

View File

@ -28,6 +28,16 @@ pub enum State {
SelectService,
}
impl std::fmt::Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
State::EditRepo => write!(f, "Edit repository"),
State::SelectTag => write!(f, "Select a tag"),
State::SelectService => write!(f, "Select a image"),
}
}
}
impl std::iter::Iterator for State {
type Item = Self;
@ -103,13 +113,14 @@ impl Ui {
Ok(Key::Ctrl('q')) => break 'core, //quit program without saving
Ok(Key::Char('\t')) => {
ui.state.next();
ui.info.set_info(&ui.state);
}
Ok(Key::Ctrl('s')) => match ui.services.save() {
Err(e) => {
ui.info.set_info(&format!("{}", e));
continue;
}
Ok(_) => ui.info.set_info("Saved compose file"),
Ok(_) => ui.info.set_text("Saved compose file"),
},
Ok(Key::Ctrl('r')) => {
ui.repo.confirm();
@ -139,7 +150,7 @@ impl Ui {
Ok(Key::Char(key)) => match ui.state {
State::SelectService => (),
State::EditRepo => {
ui.info.set_info("Editing Repository");
ui.info.set_text("Editing Repository");
ui.repo.handle_input(Key::Char(key));
}
State::SelectTag => (),
@ -147,7 +158,7 @@ impl Ui {
Ok(Key::Backspace) => match ui.state {
State::SelectService => (),
State::EditRepo => {
ui.info.set_info("Editing Repository");
ui.info.set_text("Editing Repository");
ui.repo.handle_input(Key::Backspace);
}
State::SelectTag => (),

View File

@ -17,6 +17,15 @@ pub enum State {
SelectTag,
}
impl std::fmt::Display for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
State::EditRepo => write!(f, "Edit repository"),
State::SelectTag => write!(f, "Select a tag"),
}
}
}
impl std::iter::Iterator for State {
type Item = Self;
@ -97,6 +106,7 @@ impl NoYaml {
Ok(Key::Ctrl('q')) => break 'core,
Ok(Key::Char('\t')) => {
ui.state.next();
ui.info.set_info(&ui.state);
}
Ok(Key::Ctrl('r')) => {
ui.repo.confirm();
@ -111,7 +121,7 @@ impl NoYaml {
},
Ok(Key::Char(key)) => match ui.state {
State::EditRepo => {
ui.info.set_info("Editing Repository");
ui.info.set_text("Editing Repository");
ui.repo.handle_input(Key::Char(key));
}
State::SelectTag => {
@ -120,7 +130,7 @@ impl NoYaml {
},
Ok(Key::Backspace) => match ui.state {
State::EditRepo => {
ui.info.set_info("Editing Repository");
ui.info.set_text("Editing Repository");
ui.repo.handle_input(Key::Backspace);
}
State::SelectTag => (),

View File

@ -27,7 +27,13 @@ impl Info {
.highlight_style(Style::default().bg(Color::Black))
}
pub fn set_info(&mut self, info: &str) {
/// set a text to display
pub fn set_text(&mut self, info: &str) {
self.info = String::from(info);
}
/// print a text to display
pub fn set_info(&mut self, text: &dyn std::fmt::Display) {
self.info = format!("{}", text);
}
}

View File

@ -34,6 +34,7 @@ pub struct ServiceSwitcher {
impl ServiceSwitcher {
pub fn new(file: &Option<PathBuf>) -> Option<Self> {
//gather possible filenames
let mut file_list = vec![
PathBuf::from("docker-compose.yml"),
PathBuf::from("docker-compose.yaml"),
@ -43,6 +44,7 @@ impl ServiceSwitcher {
Some(file) => file_list.insert(0, file.clone()),
}
//try filenames
for file in file_list {
let list = match File::open(&file) {
Err(_) => continue,