From c57e2e645d72f5bdd817620c9490a5a718ae3fce Mon Sep 17 00:00:00 2001
From: Thom Dickson <td3of4@gmail.com>
Date: Thu, 16 Dec 2021 21:43:50 -0500
Subject: [PATCH] Add list command

---
 src/config.rs |  4 ++--
 src/lib.rs    | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/config.rs b/src/config.rs
index 527c0d4..a7b9b33 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -16,8 +16,8 @@ pub struct Config {
 #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
 /// The internal representation of a single repository
 pub struct Repository {
-    name: String,
-    url: String,
+    pub name: String,
+    pub url: String,
 }
 
 impl Repository {
diff --git a/src/lib.rs b/src/lib.rs
index 59ebf35..e3c355f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -20,6 +20,7 @@ enum Cmd {
     Add,
     Help,
     Fetch,
+    List,
     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();
         }
 
@@ -128,6 +138,21 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
                 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) => {
             println!("{:?} hasn't been implemented yet!", x)
         }