1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::collections::BTreeMap;

use crate::native::Workspace;
use anyhow::Result;
use noosphere_core::data::ContentType;
use noosphere_storage::MemoryStore;

pub fn status_section(
    name: &str,
    entries: &BTreeMap<String, Option<ContentType>>,
    section: &mut Vec<(String, String, String)>,
    max_name_length: &mut usize,
    max_content_type_length: &mut usize,
) {
    for (slug, content_type) in entries {
        let content_type = content_type
            .as_ref()
            .map(|content_type| content_type.to_string())
            .unwrap_or_else(|| "Unknown".into());

        *max_name_length = *max_name_length.max(&mut slug.len());
        *max_content_type_length = *max_content_type_length.max(&mut content_type.len());

        section.push((slug.to_string(), content_type, String::from(name)));
    }
}

pub async fn status(workspace: &Workspace) -> Result<()> {
    let identity = workspace.sphere_identity().await?;

    println!("This sphere's identity is {identity}");
    println!("Here is a summary of the current changes to sphere content:\n");

    let mut memory_store = MemoryStore::default();

    let (_, mut changes) = match workspace
        .get_file_content_changes(&mut memory_store)
        .await?
    {
        Some((content, content_changes)) if !content_changes.is_empty() => {
            (content, content_changes)
        }
        _ => {
            println!("No new changes to sphere content!");
            return Ok(());
        }
    };

    let mut content = Vec::new();

    let mut max_name_length = 7usize;
    let mut max_content_type_length = 16usize;

    status_section(
        "Updated",
        &mut changes.updated,
        &mut content,
        &mut max_name_length,
        &mut max_content_type_length,
    );

    status_section(
        "New",
        &mut changes.new,
        &mut content,
        &mut max_name_length,
        &mut max_content_type_length,
    );

    status_section(
        "Removed",
        &mut changes.removed,
        &mut content,
        &mut max_name_length,
        &mut max_content_type_length,
    );

    println!(
        "{:max_name_length$}  {:max_content_type_length$}  STATUS",
        "NAME", "CONTENT-TYPE"
    );

    for (slug, content_type, status) in content {
        println!(
            "{:max_name_length$}  {:max_content_type_length$}  {}",
            slug, content_type, status
        );
    }

    Ok(())
}