Skip to content

Commit 9993dcb

Browse files
committed
Commit of missing changes of last commit
1 parent 65c7a54 commit 9993dcb

File tree

10 files changed

+76
-9
lines changed

10 files changed

+76
-9
lines changed

Cargo.lock

+18-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ image = "0.23.12"
3333
regex = "1"
3434
error-chain = "0.12"
3535
toml = "0.5.7"
36+
serde = "1.0.118"
37+
serde_json = "1.0.60"
3638

3739
[target.'cfg(windows)'.dependencies]
3840
ansi_term = "0.12"
@@ -44,6 +46,7 @@ base64 = "0.13.0"
4446
[dev-dependencies]
4547
more-asserts = "0.2"
4648
paste = "1"
49+
serde_test = "1.0.117"
4750

4851
[features]
4952
fail-on-deprecated = []

src/main.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ fn run() -> Result<()> {
3030
return Err("please run onefetch inside of a non-bare git repository".into());
3131
}
3232

33+
let print_json = config.print_json;
34+
3335
let info = info::Info::new(config)?;
3436

3537
let mut printer = Printer::new(io::BufWriter::new(io::stdout()), info);
3638

37-
printer.print()?;
39+
if print_json {
40+
printer.print_json()?;
41+
} else {
42+
printer.print()?;
43+
}
3844

3945
Ok(())
4046
}

src/onefetch/cli.rs

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct Cli {
3131
pub excluded: Vec<String>,
3232
pub print_languages: bool,
3333
pub print_package_managers: bool,
34+
pub print_json: bool,
3435
pub true_color: bool,
3536
pub art_off: bool,
3637
pub text_colors: Vec<String>,
@@ -228,6 +229,12 @@ impl Cli {
228229
.takes_value(true)
229230
.help("Ignore all files & directories matching EXCLUDE."),
230231
)
232+
.arg(
233+
Arg::with_name("json")
234+
.short("j")
235+
.long("json")
236+
.help("Print out a json representation of the normal onefetch output")
237+
)
231238
.get_matches();
232239

233240
let true_color = cli_utils::is_truecolor_terminal();
@@ -236,6 +243,7 @@ impl Cli {
236243
let no_color_palette = matches.is_present("no-color-palette");
237244
let print_languages = matches.is_present("languages");
238245
let print_package_managers = matches.is_present("package-managers");
246+
let print_json = matches.is_present("json");
239247

240248
let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields")
241249
{
@@ -331,6 +339,7 @@ impl Cli {
331339
excluded,
332340
print_languages,
333341
print_package_managers,
342+
print_json,
334343
true_color,
335344
text_colors,
336345
art_off,

src/onefetch/commit_info.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use git2::Oid;
2+
use serde::Serialize;
23

4+
#[derive(Serialize)]
35
pub struct CommitInfo {
6+
#[serde(skip_serializing)]
47
commit: Oid,
58
refs: Vec<String>,
69
}

src/onefetch/info.rs

+17
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ use {
44
language::Language, license::Detector, repo::Repo, text_color::TextColor,
55
},
66
colored::{Color, ColoredString, Colorize},
7+
// serde::ser::{Serialize, SerializeStruct, Serializer},
8+
serde::Serialize,
79
};
810

11+
#[derive(Serialize)]
912
pub struct Info {
1013
git_username: String,
1114
git_version: String,
@@ -26,8 +29,11 @@ pub struct Info {
2629
repo_size: String,
2730
license: String,
2831
pub dominant_language: Language,
32+
// #[serde(with = "Vec<ColorDef>")]
33+
#[serde(skip_serializing)]
2934
pub ascii_colors: Vec<Color>,
3035
pub text_colors: TextColor,
36+
#[serde(skip_serializing)]
3137
pub config: Cli,
3238
}
3339

@@ -371,3 +377,14 @@ impl Info {
371377
}
372378
}
373379
}
380+
381+
// impl Serialize for Info {
382+
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok>
383+
// where
384+
// S: Serializer,
385+
// {
386+
// let mut state = serializer.serialize_struct("Info", 19)?;
387+
388+
// state.end()
389+
// }
390+
// }

src/onefetch/language.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use {
22
crate::onefetch::{error::*, utils::num_to_color},
33
colored::Color,
44
regex::Regex,
5+
serde::Serialize,
56
std::collections::HashMap,
67
std::env,
78
strum::{EnumIter, EnumString, IntoStaticStr},
@@ -22,7 +23,7 @@ macro_rules! define_languages {
2223
($( { $name:ident, $ascii:literal, $display:literal, $colors:expr $(, $serialize:literal )? } ),* ,) => {
2324

2425
#[strum(serialize_all = "lowercase")]
25-
#[derive(PartialEq, Eq, Hash, Clone, EnumString, EnumIter, IntoStaticStr)]
26+
#[derive(PartialEq, Eq, Hash, Clone, EnumString, EnumIter, IntoStaticStr, Serialize)]
2627
pub enum Language {
2728
$(
2829
$( #[strum(serialize = $serialize)] )?

src/onefetch/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ mod language;
1212
mod license;
1313
pub mod printer;
1414
pub mod repo;
15+
mod serializer;
1516
mod text_color;
1617
mod utils;

src/onefetch/printer.rs

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ impl<W: Write> Printer<W> {
7171
Ok(())
7272
}
7373

74+
pub fn print_json(&mut self) -> Result<()> {
75+
write!(self.writer, "{}", serde_json::to_string_pretty(&self.info).unwrap())?;
76+
Ok(())
77+
}
78+
7479
fn get_ascii(&self) -> &str {
7580
let language = if let Some(ascii_language) = &self.info.config.ascii_language {
7681
ascii_language

src/onefetch/text_color.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
use {crate::onefetch::utils::num_to_color, colored::Color};
1+
use {
2+
crate::onefetch::serializer::ColorDef, crate::onefetch::utils::num_to_color, colored::Color,
3+
serde::Serialize,
4+
};
25

6+
#[derive(Serialize)]
37
pub struct TextColor {
8+
#[serde(with = "ColorDef")]
49
pub title: Color,
10+
#[serde(with = "ColorDef")]
511
pub tilde: Color,
12+
#[serde(with = "ColorDef")]
613
pub underline: Color,
14+
#[serde(with = "ColorDef")]
715
pub subtitle: Color,
16+
#[serde(with = "ColorDef")]
817
pub colon: Color,
18+
#[serde(with = "ColorDef")]
919
pub info: Color,
1020
}
1121

0 commit comments

Comments
 (0)