added comments

This commit is contained in:
Thomas Eppers 2021-09-07 16:07:20 +02:00
parent 5fa155381b
commit 0d76f3b8fa
5 changed files with 20 additions and 3 deletions

View File

@ -20,7 +20,6 @@ pub struct Images {
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Tags { pub struct Tags {
// count: i32,
next_page: Option<String>, next_page: Option<String>,
prev_page: Option<String>, prev_page: Option<String>,
pub results: Vec<Images>, pub results: Vec<Images>,
@ -63,6 +62,7 @@ impl Tags {
Ok(tags) Ok(tags)
} }
/// checks the repo name and may add a prefix for official images
pub fn check_repo(mut name: String) -> Result<String, Error> { pub fn check_repo(mut name: String) -> Result<String, Error> {
//check for right set of characters //check for right set of characters
if name.bytes().any(|c| !c.is_ascii()) { if name.bytes().any(|c| !c.is_ascii()) {
@ -87,6 +87,7 @@ impl fmt::Display for Images {
} }
} }
/// converts a given duration to a readable string
fn format_time_nice(time: chrono::Duration) -> String { fn format_time_nice(time: chrono::Duration) -> String {
if time.num_weeks() == 52 { if time.num_weeks() == 52 {
format!("{} Jahr", (time.num_weeks() / 52) as i32) format!("{} Jahr", (time.num_weeks() / 52) as i32)

View File

@ -179,10 +179,12 @@ impl Ui {
terminal.clear().unwrap(); terminal.clear().unwrap();
} }
/// helper function to show information in TagList
fn show_info(&mut self, error: &str) { fn show_info(&mut self, error: &str) {
self.tags = tag_list::TagList::with_status(error); self.tags = tag_list::TagList::with_status(error);
} }
/// create a thread for catching input and send them to core loop
pub fn spawn_stdin_channel(&self) -> mpsc::Receiver<termion::event::Key> { pub fn spawn_stdin_channel(&self) -> mpsc::Receiver<termion::event::Key> {
let (tx, rx) = mpsc::channel::<termion::event::Key>(); let (tx, rx) = mpsc::channel::<termion::event::Key>();

View File

@ -75,6 +75,7 @@ impl RepoEntry {
} }
} }
/// set the widget to unchanged
pub fn confirm(&mut self) { pub fn confirm(&mut self) {
self.old_text = self.text.clone(); self.old_text = self.text.clone();
self.changed = false; self.changed = false;

View File

@ -88,6 +88,7 @@ impl ServiceSwitcher {
(items, &mut self.state) (items, &mut self.state)
} }
/// finds the next image tag in given file
pub fn find_next_match(&mut self) -> bool { pub fn find_next_match(&mut self) -> bool {
let current_line: usize = match self.state.selected() { let current_line: usize = match self.state.selected() {
None => 0, None => 0,
@ -114,6 +115,7 @@ impl ServiceSwitcher {
false false
} }
/// finds the previous image tag in given file
pub fn find_previous_match(&mut self) -> bool { pub fn find_previous_match(&mut self) -> bool {
let current_line: usize = match self.state.selected() { let current_line: usize = match self.state.selected() {
None => 0, None => 0,
@ -146,7 +148,7 @@ impl ServiceSwitcher {
false false
} }
//return the repository from currently selected row /// return the repository from currently selected row
pub fn extract_repo(&self) -> Result<String, Error> { pub fn extract_repo(&self) -> Result<String, Error> {
match self.state.selected() { match self.state.selected() {
None => return Err(Error::NoneSelected), None => return Err(Error::NoneSelected),
@ -161,6 +163,7 @@ impl ServiceSwitcher {
} }
} }
/// replace currently selected line with repo and tag
pub fn change_current_line(&mut self, repo_with_tag: String) { pub fn change_current_line(&mut self, repo_with_tag: String) {
match self.state.selected() { match self.state.selected() {
None => (), None => (),
@ -178,6 +181,7 @@ impl ServiceSwitcher {
self.changed = true; self.changed = true;
} }
/// save the currently opened file
pub fn save(&mut self) -> Result<(), std::io::Error> { pub fn save(&mut self) -> Result<(), std::io::Error> {
let name = "docker-compose.yml"; let name = "docker-compose.yml";
let mut file = File::create(name)?; let mut file = File::create(name)?;

View File

@ -26,23 +26,27 @@ impl fmt::Display for Error {
} }
} }
} }
/// used for creating a TagList
pub enum Type { pub enum Type {
Status(String), Status(String),
Repo(tags::Tags), Repo(tags::Tags),
} }
impl TagList { impl TagList {
pub fn new(typ: Type) -> Self { fn new(typ: Type) -> Self {
Self { Self {
typ, typ,
state: ListState::default(), state: ListState::default(),
} }
} }
/// create a TagList with a status message
pub fn with_status(status: &str) -> Self { pub fn with_status(status: &str) -> Self {
Self::new(Type::Status(String::from(status))) Self::new(Type::Status(String::from(status)))
} }
/// create a TagList
pub fn with_repo(name: String) -> Self { pub fn with_repo(name: String) -> Self {
match tags::Tags::new(name) { match tags::Tags::new(name) {
Err(e) => Self::with_status(&format!("{}", e)), Err(e) => Self::with_status(&format!("{}", e)),
@ -50,6 +54,7 @@ impl TagList {
} }
} }
/// get a list of tag names with info
fn print_lines(&self) -> Vec<String> { fn print_lines(&self) -> Vec<String> {
match &self.typ { match &self.typ {
Type::Status(line) => vec![line.to_string()], Type::Status(line) => vec![line.to_string()],
@ -57,6 +62,7 @@ impl TagList {
} }
} }
/// get the list of tag names
pub fn get_names(&self) -> Result<Vec<String>, Error> { pub fn get_names(&self) -> Result<Vec<String>, Error> {
match &self.typ { match &self.typ {
Type::Status(_) => Err(Error::NoTags), Type::Status(_) => Err(Error::NoTags),
@ -64,6 +70,7 @@ impl TagList {
} }
} }
/// get the selected tag or return an error
pub fn get_selected(&self) -> Result<String, Error> { pub fn get_selected(&self) -> Result<String, Error> {
match &self.typ { match &self.typ {
Type::Status(_) => Err(Error::NoTags), Type::Status(_) => Err(Error::NoTags),
@ -121,6 +128,7 @@ impl TagList {
} }
} }
/// select next tag
pub fn next(&mut self) { pub fn next(&mut self) {
match self.state.selected() { match self.state.selected() {
None if self.print_lines().len() > 0 => self.state.select(Some(0)), None if self.print_lines().len() > 0 => self.state.select(Some(0)),
@ -130,6 +138,7 @@ impl TagList {
} }
} }
/// select previous tag
pub fn previous(&mut self) { pub fn previous(&mut self) {
match self.state.selected() { match self.state.selected() {
None if self.print_lines().len() > 0 => { None if self.print_lines().len() > 0 => {