Add list command

This commit is contained in:
Thom Dickson 2021-12-16 21:43:50 -05:00
parent a3c4a5f2dd
commit c57e2e645d
Signed by: boots
GPG Key ID: 40BE2AF8EBF8D2BB
2 changed files with 27 additions and 2 deletions

View File

@ -16,8 +16,8 @@ pub struct Config {
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
/// The internal representation of a single repository /// The internal representation of a single repository
pub struct Repository { pub struct Repository {
name: String, pub name: String,
url: String, pub url: String,
} }
impl Repository { impl Repository {

View File

@ -20,6 +20,7 @@ enum Cmd {
Add, Add,
Help, Help,
Fetch, Fetch,
List,
DumpConfig, DumpConfig,
} }
@ -94,6 +95,15 @@ impl State {
_ => {} _ => {}
} }
} }
// list command
else if x == "list" {
match state.cmd {
None => {
state.cmd = Some(Cmd::List);
}
_ => {}
}
}
arg = args.next(); arg = args.next();
} }
@ -128,6 +138,21 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
println!("Repository is already being tracked"); println!("Repository is already being tracked");
} }
} }
Some(Cmd::List) => {
// Get the largest entry by name
let width: usize = match config
.repositories
.iter()
.max_by(|x, y| x.name.len().cmp(&y.name.len()))
{
Some(x) => x.name.len(),
None => 0,
};
println!("{:width$} URL", "Name", width = width);
for repo in config.repositories.iter() {
println!("{:width$} {}", repo.name, repo.url, width = width);
}
}
Some(x) => { Some(x) => {
println!("{:?} hasn't been implemented yet!", x) println!("{:?} hasn't been implemented yet!", x)
} }