Skip to content

Commit 7a37b01

Browse files
committed
reorganize project file structure
1 parent 735a435 commit 7a37b01

23 files changed

+213
-218
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
### Adding support for a new language
1313

14-
Adding support for a new Language consists in adding a new entry to the `define_language!` macro in [language.rs](https://raw.githubusercontent.com/o2sh/onefetch/main/src/language.rs) and filling it in with the right data.
14+
Adding support for a new Language consists in adding a new entry to the `define_language!` macro in [language.rs](src/repo/language.rs) and filling it in with the right data.
1515

1616
**Example**:
1717

@@ -59,15 +59,15 @@ Remarks:
5959

6060
To add a new package manager, make sure to follow these steps:
6161

62-
1. Add a new entry in the `define_package_managers!` macro in [package_manager.rs](src/onefetch/package_managers/package_manager.rs).
62+
1. Add a new entry in the `define_package_managers!` macro in [package_manager.rs](src/repo/deps/package_manager.rs).
6363

6464
**Example**:
6565

6666
`{ Cargo, "cargo", [ ("Cargo.toml", cargo) ] },`
6767

6868
The first item `Cargo` corresponds to the name of the package manager. The second item `cargo` is the display name. Then we have the name of the package manager file that will be parsed: `Cargo.toml` along with its parser `cargo` (cf. step 2), notice that the third item is an array of tuple in case the package manager has multiple parsers (e.g. pip).
6969

70-
2. in `src/onefetch/deps/package_parser.rs`: create a function that takes an input of type `&str` representing the content of the package manager's file, and returns a `usize` as its number of dependencies.
70+
2. in `src/repo/deps/package_parser.rs`: create a function that takes an input of type `&str` representing the content of the package manager's file, and returns a `usize` as its number of dependencies.
7171

7272
```rust
7373
pub fn cargo(contents: &str) -> Result<usize> {

src/onefetch/cli.rs renamed to src/cli.rs

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
use {
2-
crate::onefetch::{
3-
cli_utils,
4-
error::*,
5-
image_backends,
6-
info_field::{InfoField, InfoFieldOff},
7-
language::Language,
8-
printer::SerializationFormat,
9-
},
10-
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
11-
image::DynamicImage,
12-
regex::Regex,
13-
std::{convert::From, env, str::FromStr},
14-
strum::IntoEnumIterator,
15-
term_size,
16-
};
1+
use crate::error::*;
2+
use crate::repo::deps::package_manager::PackageManager;
3+
use crate::repo::language::Language;
4+
use crate::ui::image_backends;
5+
use crate::ui::image_backends::ImageBackend;
6+
use crate::ui::info::info_field::{InfoField, InfoFieldOff};
7+
use crate::ui::printer::SerializationFormat;
8+
use clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg};
9+
use image::DynamicImage;
10+
use regex::Regex;
11+
use std::process::Command;
12+
use std::{convert::From, env, str::FromStr};
13+
use strum::IntoEnumIterator;
14+
use term_size;
1715

1816
const MAX_TERM_WIDTH: usize = 95;
1917

20-
pub struct Cli {
18+
pub struct Config {
2119
pub repo_path: String,
2220
pub ascii_input: Option<String>,
2321
pub ascii_language: Option<Language>,
2422
pub ascii_colors: Vec<String>,
2523
pub disabled_fields: InfoFieldOff,
2624
pub no_bold: bool,
2725
pub image: Option<DynamicImage>,
28-
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
26+
pub image_backend: Option<Box<dyn ImageBackend>>,
2927
pub image_color_resolution: usize,
3028
pub no_merges: bool,
3129
pub no_color_palette: bool,
@@ -42,7 +40,7 @@ pub struct Cli {
4240
pub show_email: bool,
4341
}
4442

45-
impl Cli {
43+
impl Config {
4644
pub fn new() -> Result<Self> {
4745
#[cfg(not(windows))]
4846
let possible_backends = ["kitty", "iterm", "sixel"];
@@ -280,7 +278,7 @@ impl Cli {
280278
let true_color = match matches.value_of("true-color") {
281279
Some("always") => true,
282280
Some("never") => false,
283-
Some("auto") => cli_utils::is_truecolor_terminal(),
281+
Some("auto") => is_truecolor_terminal(),
284282
_ => unreachable!(),
285283
};
286284
let no_bold = matches.is_present("no-bold");
@@ -375,7 +373,7 @@ impl Cli {
375373
.value_of("no-bots")
376374
.map_or(Regex::from_str(r"\[bot\]").unwrap(), |s| Regex::from_str(s).unwrap())
377375
});
378-
Ok(Cli {
376+
Ok(Config {
379377
repo_path,
380378
ascii_input,
381379
ascii_language,
@@ -401,3 +399,34 @@ impl Cli {
401399
})
402400
}
403401
}
402+
403+
pub fn print_supported_languages() -> Result<()> {
404+
for l in Language::iter() {
405+
println!("{}", l);
406+
}
407+
408+
Ok(())
409+
}
410+
411+
pub fn print_supported_package_managers() -> Result<()> {
412+
for p in PackageManager::iter() {
413+
println!("{}", p);
414+
}
415+
416+
Ok(())
417+
}
418+
419+
pub fn is_truecolor_terminal() -> bool {
420+
env::var("COLORTERM")
421+
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
422+
.unwrap_or(false)
423+
}
424+
425+
pub fn get_git_version() -> String {
426+
let version = Command::new("git").arg("--version").output();
427+
428+
match version {
429+
Ok(v) => String::from_utf8_lossy(&v.stdout).replace('\n', ""),
430+
Err(_) => String::new(),
431+
}
432+
}
File renamed without changes.

src/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,35 @@
22
#![recursion_limit = "1024"]
33
#![cfg_attr(feature = "fail-on-deprecated", deny(deprecated))]
44

5-
use onefetch::{cli::Cli, cli_utils, error::*, info, printer::Printer, repo};
6-
5+
use cli::Config;
6+
use error::*;
77
use std::{io, process};
8+
use ui::info::Info;
9+
use ui::printer::Printer;
810

9-
mod onefetch;
11+
mod cli;
12+
mod error;
13+
mod repo;
14+
mod ui;
1015

1116
fn run() -> Result<()> {
1217
#[cfg(windows)]
1318
let _ = ansi_term::enable_ansi_support();
1419

15-
let config = Cli::new()?;
20+
let config = Config::new()?;
1621

1722
if config.print_languages {
18-
return cli_utils::print_supported_languages();
23+
return cli::print_supported_languages();
1924
}
2025

2126
if config.print_package_managers {
22-
return cli_utils::print_supported_package_managers();
27+
return cli::print_supported_package_managers();
2328
}
2429

2530
if !repo::is_valid(&config.repo_path)? {
2631
return Err("please run onefetch inside of a non-bare git repository".into());
2732
}
28-
let info = info::Info::new(config)?;
33+
let info = Info::new(config)?;
2934

3035
let mut printer = Printer::new(io::BufWriter::new(io::stdout()), info);
3136

src/onefetch/cli_utils.rs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/onefetch/mod.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/onefetch/utils.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/onefetch/package_managers/mod.rs renamed to src/repo/deps/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
crate::onefetch::error::*,
2+
crate::error::*,
33
std::collections::HashMap,
44
std::{ffi::OsStr, fs},
55
};

src/onefetch/package_managers/package_manager.rs renamed to src/repo/deps/package_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::onefetch::error::*;
1+
use crate::error::*;
22
use std::collections::HashMap;
33
use {regex::Regex, strum::EnumIter, toml::Value, yaml_rust::YamlLoader};
44

src/onefetch/commit_info.rs renamed to src/repo/head_refs.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use git2::Oid;
22
use serde::ser::SerializeStruct;
33
use serde::Serialize;
44

5-
pub struct CommitInfo {
5+
pub struct HeadRefs {
66
commit: Oid,
77
refs: Vec<String>,
88
}
99

10-
impl CommitInfo {
11-
pub fn new(commit: Oid, refs: Vec<String>) -> CommitInfo {
12-
CommitInfo { commit, refs }
10+
impl HeadRefs {
11+
pub fn new(commit: Oid, refs: Vec<String>) -> HeadRefs {
12+
HeadRefs { commit, refs }
1313
}
1414
}
1515

16-
impl std::fmt::Display for CommitInfo {
16+
impl std::fmt::Display for HeadRefs {
1717
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1818
let short_commit = self.commit.to_string().chars().take(7).collect::<String>();
1919
if !self.refs.is_empty() {
@@ -30,12 +30,12 @@ impl std::fmt::Display for CommitInfo {
3030
}
3131
}
3232

33-
impl Serialize for CommitInfo {
33+
impl Serialize for HeadRefs {
3434
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
3535
where
3636
S: serde::Serializer,
3737
{
38-
let mut state = serializer.serialize_struct("CommitInfo", 2)?;
38+
let mut state = serializer.serialize_struct("HeadRefs", 2)?;
3939
state.serialize_field("refs", &self.refs)?;
4040
state
4141
.serialize_field("oid", &self.commit.to_string().chars().take(7).collect::<String>())?;

src/onefetch/language.rs renamed to src/repo/language.rs

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use {
2-
crate::onefetch::{error::*, utils::num_to_color},
3-
colored::Color,
4-
regex::Regex,
5-
serde::Serialize,
6-
std::collections::HashMap,
7-
std::env,
8-
strum::{EnumIter, EnumString, IntoStaticStr},
9-
};
1+
use crate::error::*;
2+
use colored::Color;
3+
use regex::Regex;
4+
use serde::Serialize;
5+
use std::collections::HashMap;
6+
use std::env;
7+
use strum::{EnumIter, EnumString, IntoStaticStr};
108

119
pub struct Colors {
1210
basic_colors: Vec<Color>,
@@ -129,7 +127,7 @@ macro_rules! define_languages {
129127

130128
#[cfg(test)]
131129
mod ascii_size {
132-
use crate::onefetch::ascii_art::get_min_start_max_end;
130+
use crate::ui::ascii_art::get_min_start_max_end;
133131
use more_asserts::assert_le;
134132
use paste::paste;
135133

@@ -368,33 +366,4 @@ impl Language {
368366

369367
languages
370368
}
371-
372-
pub fn get_ascii_colors(
373-
ascii_language: &Option<Language>,
374-
dominant_language: &Language,
375-
ascii_colors: &[String],
376-
true_color: bool,
377-
) -> Vec<Color> {
378-
let language = if let Some(ascii_language) = ascii_language {
379-
ascii_language
380-
} else {
381-
&dominant_language
382-
};
383-
384-
let colors = language.get_colors(true_color);
385-
386-
let colors: Vec<Color> = colors
387-
.iter()
388-
.enumerate()
389-
.map(|(index, default_color)| {
390-
if let Some(color_num) = ascii_colors.get(index) {
391-
if let Some(color) = num_to_color(color_num) {
392-
return color;
393-
}
394-
}
395-
*default_color
396-
})
397-
.collect();
398-
colors
399-
}
400369
}

0 commit comments

Comments
 (0)