Skip to content

Commit 4fcc458

Browse files
committed
Merge branch 'master' into remove-panic-from-info-fmt
2 parents d8d0ff7 + 1c07cfb commit 4fcc458

File tree

10 files changed

+199
-132
lines changed

10 files changed

+199
-132
lines changed

Diff for: src/main.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// `error_chain!` can recurse deeply
22
#![recursion_limit = "1024"]
33

4-
use onefetch::{cli::Cli, error::*, info};
4+
use onefetch::{cli::Cli, cli_utils, error::*, info};
55

66
use {
77
process::{Command, Stdio},
8-
std::process,
8+
std::{io, process},
99
};
1010

1111
mod onefetch;
@@ -22,12 +22,15 @@ fn run() -> Result<()> {
2222
let options = Cli::new()?;
2323

2424
if options.print_languages {
25-
return Cli::print_supported_languages();
25+
return cli_utils::print_supported_languages();
2626
}
2727

2828
let info = info::Info::new(options)?;
2929

30-
print!("{}", info);
30+
let mut printer = cli_utils::Printer::new(io::BufWriter::new(io::stdout()), info);
31+
32+
printer.print()?;
33+
3134
Ok(())
3235
}
3336

@@ -38,7 +41,7 @@ fn main() {
3841
process::exit(0);
3942
}
4043
Err(error) => {
41-
let stderr = std::io::stderr();
44+
let stderr = io::stderr();
4245
default_error_handler(&error, &mut stderr.lock());
4346
process::exit(1);
4447
}

Diff for: src/onefetch/ascii_art.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use colored::{Color, Colorize};
22

33
pub struct AsciiArt<'a> {
44
content: Box<dyn 'a + Iterator<Item = &'a str>>,
5-
colors: Vec<Color>,
5+
colors: &'a [Color],
66
bold: bool,
77
start: usize,
88
end: usize,
99
}
1010
impl<'a> AsciiArt<'a> {
11-
pub fn new(input: &'a str, colors: Vec<Color>, bold: bool) -> AsciiArt<'a> {
11+
pub fn new(input: &'a str, colors: &'a [Color], bold: bool) -> AsciiArt<'a> {
1212
let mut lines: Vec<_> = input.lines().skip_while(|line| line.is_empty()).collect();
1313
while let Some(line) = lines.last() {
1414
if Tokens(line).is_empty() {

Diff for: src/onefetch/cli.rs

+36-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use {
22
crate::onefetch::{
3-
error::*, image_backends, info_fields, info_fields::InfoFields, language::Language,
3+
cli_utils,
4+
error::*,
5+
image_backends,
6+
info_fields::{self, InfoFields},
7+
language::Language,
48
},
59
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
610
image::DynamicImage,
@@ -17,12 +21,14 @@ pub struct Cli {
1721
pub no_bold: bool,
1822
pub image: Option<DynamicImage>,
1923
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
24+
pub image_colors: usize,
2025
pub no_merges: bool,
2126
pub no_color_blocks: bool,
2227
pub number_of_authors: usize,
2328
pub excluded: Vec<String>,
2429
pub print_languages: bool,
2530
pub true_color: bool,
31+
pub art_off: bool,
2632
}
2733

2834
impl Cli {
@@ -116,7 +122,7 @@ impl Cli {
116122
Arg::with_name("languages")
117123
.short("l")
118124
.long("languages")
119-
.help("Prints out supported languages"),
125+
.help("Prints out supported languages."),
120126
)
121127
.arg(
122128
Arg::with_name("image")
@@ -125,7 +131,7 @@ impl Cli {
125131
.value_name("IMAGE")
126132
.takes_value(true)
127133
.max_values(1)
128-
.help("Path to the IMAGE file"),
134+
.help("Path to the IMAGE file."),
129135
)
130136
.arg(
131137
Arg::with_name("image-backend")
@@ -137,15 +143,25 @@ impl Cli {
137143
.possible_values(&possible_backends)
138144
.help("Which image BACKEND to use."),
139145
)
146+
.arg(
147+
Arg::with_name("color-resolution")
148+
.long("color-resolution")
149+
.value_name("VALUE")
150+
.takes_value(true)
151+
.max_values(1)
152+
.possible_values(&["16", "32", "64", "128", "256"])
153+
.default_value("16")
154+
.help("VALUE of color resolution to use with SIXEL backend."),
155+
)
140156
.arg(
141157
Arg::with_name("no-merge-commits")
142158
.long("no-merge-commits")
143-
.help("Ignores merge commits"),
159+
.help("Ignores merge commits."),
144160
)
145161
.arg(
146162
Arg::with_name("no-color-blocks")
147163
.long("no-color-blocks")
148-
.help("Hides the color blocks"),
164+
.help("Hides the color blocks."),
149165
)
150166
.arg(
151167
Arg::with_name("authors-number")
@@ -173,13 +189,20 @@ impl Cli {
173189
.multiple(true)
174190
.takes_value(true)
175191
.help("Ignore all files & directories matching EXCLUDE."),
192+
)
193+
.arg(
194+
Arg::with_name("off")
195+
.long("off")
196+
.help("Only shows the info lines.")
197+
.conflicts_with_all(&["image", "ascii-language", "ascii-input"]),
176198
).get_matches();
177199

178200
let no_bold = matches.is_present("no-bold");
179201
let no_merges = matches.is_present("no-merge-commits");
180202
let no_color_blocks = matches.is_present("no-color-blocks");
181203
let print_languages = matches.is_present("languages");
182-
let true_color = is_truecolor_terminal();
204+
let art_off = matches.is_present("off");
205+
let true_color = cli_utils::is_truecolor_terminal();
183206

184207
let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields")
185208
{
@@ -208,6 +231,11 @@ impl Cli {
208231
if image.is_some() && image_backend.is_none() {
209232
return Err("Could not detect a supported image backend".into());
210233
}
234+
let image_colors: usize = matches
235+
.value_of("color-resolution")
236+
.unwrap()
237+
.parse()
238+
.unwrap();
211239

212240
let path = String::from(matches.value_of("input").unwrap());
213241

@@ -248,28 +276,14 @@ impl Cli {
248276
no_bold,
249277
image,
250278
image_backend,
279+
image_colors,
251280
no_merges,
252281
no_color_blocks,
253282
number_of_authors,
254283
excluded,
255284
print_languages,
256285
true_color,
286+
art_off,
257287
})
258288
}
259-
260-
pub fn print_supported_languages() -> Result<()> {
261-
let iterator = Language::iter().filter(|x| *x != Language::Unknown);
262-
263-
for l in iterator {
264-
println!("{}", l);
265-
}
266-
267-
Ok(())
268-
}
269-
}
270-
271-
fn is_truecolor_terminal() -> bool {
272-
env::var("COLORTERM")
273-
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
274-
.unwrap_or(false)
275289
}

Diff for: src/onefetch/cli_utils.rs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use crate::onefetch::{ascii_art::AsciiArt, error::*, info::Info, language::Language};
2+
use colored::Color;
3+
use std::env;
4+
use std::io::Write;
5+
use strum::IntoEnumIterator;
6+
7+
pub struct Printer<W> {
8+
writer: W,
9+
info: Info,
10+
}
11+
12+
impl<W: Write> Printer<W> {
13+
pub fn new(writer: W, info: Info) -> Self {
14+
Self { writer, info }
15+
}
16+
17+
pub fn print(&mut self) -> Result<()> {
18+
let center_pad = " ";
19+
let info_str = format!("{}", &self.info);
20+
let mut info_lines = info_str.lines();
21+
let colors: Vec<Color> = Vec::new();
22+
let mut buf = String::new();
23+
24+
if self.info.config.art_off {
25+
buf.push_str(&info_str);
26+
} else if let Some(custom_image) = &self.info.config.image {
27+
buf.push_str(&self.info.config.image_backend.as_ref().unwrap().add_image(
28+
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
29+
custom_image,
30+
self.info.config.image_colors,
31+
));
32+
} else {
33+
let mut logo_lines = if let Some(custom_ascii) = &self.info.config.ascii_input {
34+
AsciiArt::new(custom_ascii, &colors, !self.info.config.no_bold)
35+
} else {
36+
AsciiArt::new(
37+
self.get_ascii(),
38+
&self.info.colors,
39+
!self.info.config.no_bold,
40+
)
41+
};
42+
43+
loop {
44+
match (logo_lines.next(), info_lines.next()) {
45+
(Some(logo_line), Some(info_line)) => {
46+
buf.push_str(&format!("{}{}{:^}\n", logo_line, center_pad, info_line))
47+
}
48+
(Some(logo_line), None) => buf.push_str(&format!("{}\n", logo_line)),
49+
(None, Some(info_line)) => buf.push_str(&format!(
50+
"{:<width$}{}{:^}\n",
51+
"",
52+
center_pad,
53+
info_line,
54+
width = logo_lines.width()
55+
)),
56+
(None, None) => {
57+
buf.push('\n');
58+
break;
59+
}
60+
}
61+
}
62+
}
63+
64+
write!(self.writer, "{}", buf)?;
65+
66+
Ok(())
67+
}
68+
69+
fn get_ascii(&self) -> &str {
70+
let language = if let Language::Unknown = self.info.config.ascii_language {
71+
&self.info.dominant_language
72+
} else {
73+
&self.info.config.ascii_language
74+
};
75+
76+
language.get_ascii_art()
77+
}
78+
}
79+
80+
pub fn print_supported_languages() -> Result<()> {
81+
let iterator = Language::iter().filter(|x| *x != Language::Unknown);
82+
83+
for l in iterator {
84+
println!("{}", l);
85+
}
86+
87+
Ok(())
88+
}
89+
90+
pub fn is_truecolor_terminal() -> bool {
91+
env::var("COLORTERM")
92+
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
93+
.unwrap_or(false)
94+
}

Diff for: src/onefetch/image_backends/kitty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl KittyBackend {
7777
}
7878

7979
impl super::ImageBackend for KittyBackend {
80-
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
80+
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, _colors: usize) -> String {
8181
let tty_size = unsafe {
8282
let tty_size: winsize = std::mem::zeroed();
8383
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);

Diff for: src/onefetch/image_backends/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod kitty;
77
pub mod sixel;
88

99
pub trait ImageBackend {
10-
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String;
10+
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> String;
1111
}
1212

1313
#[cfg(not(windows))]

Diff for: src/onefetch/image_backends/sixel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl SixelBackend {
7171
}
7272

7373
impl super::ImageBackend for SixelBackend {
74-
fn add_image(&self, lines: Vec<String>, image: &DynamicImage) -> String {
74+
fn add_image(&self, lines: Vec<String>, image: &DynamicImage, colors: usize) -> String {
7575
let tty_size = unsafe {
7676
let tty_size: winsize = std::mem::zeroed();
7777
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
@@ -97,7 +97,7 @@ impl super::ImageBackend for SixelBackend {
9797
// reduce the amount of colors using dithering
9898
colorops::dither(
9999
&mut rgba_image,
100-
&NeuQuant::new(10, 16, flat_samples.image_slice().unwrap()),
100+
&NeuQuant::new(10, colors, flat_samples.image_slice().unwrap()),
101101
);
102102
let rgb_image = ImageBuffer::from_fn(rgba_image.width(), rgba_image.height(), |x, y| {
103103
let rgba_pixel = rgba_image.get_pixel(x, y);

0 commit comments

Comments
 (0)