Skip to content

Commit 98c5785

Browse files
committed
Working on table output for CLI, also using Chrono crate in core crate to parse dates in metadata
1 parent 68b413d commit 98c5785

File tree

8 files changed

+344
-66
lines changed

8 files changed

+344
-66
lines changed

rust/Cargo.lock

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/portablemc-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ portablemc.workspace = true
1313

1414
clap = { version = "4.5", features = ["derive", "wrap_help"] }
1515
uuid = { version = "1.10", features = [] }
16+
chrono = "0.4"
1617

1718
[[bin]]
1819
name = "portablemc"

rust/portablemc-cli/src/main.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ pub mod output;
55

66
use std::time::Instant;
77

8+
use chrono::{Local, TimeDelta, Utc};
89
use clap::Parser;
910

1011
use portablemc::{download, standard, mojang};
1112

1213
use parse::{CliArgs, CliCmd, CliOutput, LoginArgs, LogoutArgs, SearchArgs, SearchKind, ShowArgs, StartArgs, StartResolution, StartVersion};
13-
use output::{Output, LogOutput, LogLevel};
14+
use output::{Output, LogOutput, LogLevel, TimeDeltaDisplay};
1415

1516

1617
fn main() {
@@ -24,7 +25,6 @@ fn main() {
2425
CliOutput::Machine => Output::tab_separated(),
2526
};
2627

27-
println!("{args:?}");
2828
cmd_cli(&mut out, &args);
2929

3030
}
@@ -41,10 +41,10 @@ fn cmd_cli(out: &mut Output, args: &CliArgs) {
4141

4242
fn cmd_search(out: &mut Output, cli_args: &CliArgs, args: &SearchArgs) {
4343

44-
let _ = (out, cli_args);
44+
let _ = cli_args;
4545

4646
match args.kind {
47-
SearchKind::Mojang => cmd_search_mojang(&args.query),
47+
SearchKind::Mojang => cmd_search_mojang(&mut *out, &args.query),
4848
SearchKind::Local => todo!(),
4949
SearchKind::Forge => todo!(),
5050
SearchKind::Fabric => todo!(),
@@ -54,8 +54,51 @@ fn cmd_search(out: &mut Output, cli_args: &CliArgs, args: &SearchArgs) {
5454

5555
}
5656

57-
fn cmd_search_mojang(_query: &str) {
58-
mojang::request_manifest(()).unwrap();
57+
fn cmd_search_mojang(out: &mut Output, _query: &str) {
58+
59+
let manifest = mojang::request_manifest(()).unwrap();
60+
61+
let mut table = out.table(3);
62+
table.cell("id").format("Identifier");
63+
table.cell("type").format("Type");
64+
table.cell("release_date").format("Release date");
65+
66+
let today = Utc::now();
67+
68+
for version in &manifest.versions {
69+
70+
table.cell(&version.id);
71+
72+
let is_latest = manifest.latest.get(&version.r#type)
73+
.map(|id| id == &version.id)
74+
.unwrap_or(false);
75+
76+
let (type_id, type_fmt) = match version.r#type {
77+
standard::serde::VersionType::Release => ("release", "Release"),
78+
standard::serde::VersionType::Snapshot => ("snapshot", "Snapshot"),
79+
standard::serde::VersionType::OldBeta => ("old_beta", "Beta"),
80+
standard::serde::VersionType::OldAlpha => ("old_alpha", "Alpha"),
81+
};
82+
83+
if is_latest {
84+
table.cell(format_args!("{type_id}*")).format(format_args!("{type_fmt}*"));
85+
} else {
86+
table.cell(format_args!("{type_id}")).format(format_args!("{type_fmt}"));
87+
}
88+
89+
let mut cell = table.cell(&version.release_time.to_rfc3339());
90+
let local_release_date = version.release_time.with_timezone(&Local);
91+
let local_release_data_fmt: _ = version.release_time.format("%a %b %e %T %Y");
92+
93+
let delta = today.signed_duration_since(&local_release_date);
94+
if is_latest || delta <= TimeDelta::weeks(4) {
95+
cell.format(format_args!("{} ({})", local_release_data_fmt, TimeDeltaDisplay(delta)));
96+
} else {
97+
cell.format(format_args!("{}", local_release_data_fmt));
98+
}
99+
100+
}
101+
59102
}
60103

61104
fn cmd_start(out: &mut Output, cli_args: &CliArgs, args: &StartArgs) {

0 commit comments

Comments
 (0)