Skip to content

Commit 306e305

Browse files
o2shclaude
andauthored
refactor: move misplaced functions to the modules that own them (#1888)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent d9fc75b commit 306e305

12 files changed

Lines changed: 166 additions & 166 deletions

File tree

‎src/cli.rs‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use clap::builder::styling::AnsiColor;
1111
use clap::builder::{PossibleValuesParser, Styles, TypedValueParser as _};
1212
use clap::{Args, Parser, ValueHint, value_parser};
1313
use clap_complete::Shell;
14-
use std::env;
1514
use std::path::PathBuf;
1615

1716
const COLOR_RESOLUTIONS: [&str; 5] = ["16", "32", "64", "128", "256"];
@@ -306,22 +305,6 @@ impl Default for ImageArgs {
306305
}
307306
}
308307

309-
fn is_truecolor_terminal() -> bool {
310-
env::var("COLORTERM")
311-
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
312-
.unwrap_or(false)
313-
}
314-
315-
impl Cli {
316-
pub fn true_color(&self) -> bool {
317-
match self.ascii.true_color {
318-
When::Always => true,
319-
When::Never => false,
320-
When::Auto => is_truecolor_terminal(),
321-
}
322-
}
323-
}
324-
325308
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
326309
pub enum When {
327310
Auto,

‎src/data/fields/url.rs‎

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use anyhow::Result;
2-
use gix::Repository;
3-
use regex::regex;
41
use serde::Serialize;
52

63
#[derive(Serialize)]
@@ -15,100 +12,3 @@ impl Url {
1512
}
1613
}
1714
}
18-
19-
pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> Result<String> {
20-
let remote = match repo.find_default_remote(gix::remote::Direction::Fetch) {
21-
Some(remote) => remote?,
22-
None => return Ok(String::new()),
23-
};
24-
25-
Ok(remote
26-
.url(gix::remote::Direction::Fetch)
27-
.map(|url| format_url(&url.to_string(), hide_token, http_url))
28-
.unwrap_or_default())
29-
}
30-
31-
fn format_url(url: &str, hide_token: bool, http_url: bool) -> String {
32-
let formatted_url = if hide_token {
33-
remove_token_from_url(url)
34-
} else {
35-
String::from(url)
36-
};
37-
38-
if http_url && !formatted_url.starts_with("http") {
39-
create_http_url_from_ssh(&formatted_url)
40-
} else {
41-
formatted_url
42-
}
43-
}
44-
45-
fn remove_token_from_url(url: &str) -> String {
46-
regex!(r"(https?://)([^@]+@)")
47-
.replace(url, "$1")
48-
.to_string()
49-
}
50-
51-
fn create_http_url_from_ssh(url: &str) -> String {
52-
regex!(r"([^@]+)@([^:]+):(.*)")
53-
.replace(url, "https://${2}/${3}")
54-
.to_string()
55-
}
56-
57-
#[cfg(test)]
58-
mod test {
59-
use super::*;
60-
use rstest::rstest;
61-
62-
#[rstest]
63-
#[case(
64-
"https://username:token@github.com/user/repo",
65-
true,
66-
false,
67-
"https://github.com/user/repo"
68-
)]
69-
#[case(
70-
"https://user:token@gitlab.com/user/repo",
71-
true,
72-
false,
73-
"https://gitlab.com/user/repo"
74-
)]
75-
#[case(
76-
"git@github.com:user/repo.git",
77-
false,
78-
true,
79-
"https://github.com/user/repo.git"
80-
)]
81-
#[case(
82-
"git@gitlab.com:user/repo",
83-
false,
84-
true,
85-
"https://gitlab.com/user/repo"
86-
)]
87-
#[case(
88-
"https://github.com/user/repo",
89-
true,
90-
true,
91-
"https://github.com/user/repo"
92-
)]
93-
#[case(
94-
"https://username:token@github.com/user/repo",
95-
false,
96-
false,
97-
"https://username:token@github.com/user/repo"
98-
)]
99-
fn test_format_url(
100-
#[case] url: &str,
101-
#[case] hide_token: bool,
102-
#[case] http_url: bool,
103-
#[case] expected: &str,
104-
) {
105-
assert_eq!(format_url(url, hide_token, http_url), expected);
106-
}
107-
108-
#[rstest]
109-
#[case("git@github.com:user/repo.git", "https://github.com/user/repo.git")]
110-
#[case("git@gitlab.com:user/repo", "https://gitlab.com/user/repo")]
111-
fn test_create_http_url_from_ssh(#[case] url: &str, #[case] expected: &str) {
112-
assert_eq!(create_http_url_from_ssh(url), expected);
113-
}
114-
}

‎src/data/git/mod.rs‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ pub mod bots;
77
mod churn;
88
pub(crate) mod identity;
99
pub(crate) mod metrics;
10+
mod remote;
1011
mod traversal;
1112

13+
pub(crate) use remote::get_repo_url;
1214
pub(crate) use traversal::traverse_commit_graph;
1315

1416
pub(crate) fn uses_reftables(repo: &Repository) -> bool {
@@ -23,19 +25,3 @@ pub fn get_work_dir(repo: &Repository) -> Result<PathBuf> {
2325
.context("please run onefetch inside of a non-bare git repository")?
2426
.to_owned())
2527
}
26-
27-
pub(crate) fn get_git_username(repo: &Repository) -> String {
28-
repo.committer()
29-
.and_then(Result::ok)
30-
.map(|c| c.name.to_string())
31-
.unwrap_or_default()
32-
}
33-
34-
pub(crate) fn get_git_version() -> String {
35-
let version = std::process::Command::new("git").arg("--version").output();
36-
37-
match version {
38-
Ok(v) => String::from_utf8_lossy(&v.stdout).replace('\n', ""),
39-
Err(_) => String::new(),
40-
}
41-
}

‎src/data/git/remote.rs‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use anyhow::Result;
2+
use gix::Repository;
3+
use regex::regex;
4+
5+
pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> Result<String> {
6+
let remote = match repo.find_default_remote(gix::remote::Direction::Fetch) {
7+
Some(remote) => remote?,
8+
None => return Ok(String::new()),
9+
};
10+
11+
Ok(remote
12+
.url(gix::remote::Direction::Fetch)
13+
.map(|url| format_url(&url.to_string(), hide_token, http_url))
14+
.unwrap_or_default())
15+
}
16+
17+
fn format_url(url: &str, hide_token: bool, http_url: bool) -> String {
18+
let formatted_url = if hide_token {
19+
remove_token_from_url(url)
20+
} else {
21+
String::from(url)
22+
};
23+
24+
if http_url && !formatted_url.starts_with("http") {
25+
create_http_url_from_ssh(&formatted_url)
26+
} else {
27+
formatted_url
28+
}
29+
}
30+
31+
fn remove_token_from_url(url: &str) -> String {
32+
regex!(r"(https?://)([^@]+@)")
33+
.replace(url, "$1")
34+
.to_string()
35+
}
36+
37+
fn create_http_url_from_ssh(url: &str) -> String {
38+
regex!(r"([^@]+)@([^:]+):(.*)")
39+
.replace(url, "https://${2}/${3}")
40+
.to_string()
41+
}
42+
43+
#[cfg(test)]
44+
mod test {
45+
use super::*;
46+
use rstest::rstest;
47+
48+
#[rstest]
49+
#[case(
50+
"https://username:token@github.com/user/repo",
51+
true,
52+
false,
53+
"https://github.com/user/repo"
54+
)]
55+
#[case(
56+
"https://user:token@gitlab.com/user/repo",
57+
true,
58+
false,
59+
"https://gitlab.com/user/repo"
60+
)]
61+
#[case(
62+
"git@github.com:user/repo.git",
63+
false,
64+
true,
65+
"https://github.com/user/repo.git"
66+
)]
67+
#[case(
68+
"git@gitlab.com:user/repo",
69+
false,
70+
true,
71+
"https://gitlab.com/user/repo"
72+
)]
73+
#[case(
74+
"https://github.com/user/repo",
75+
true,
76+
true,
77+
"https://github.com/user/repo"
78+
)]
79+
#[case(
80+
"https://username:token@github.com/user/repo",
81+
false,
82+
false,
83+
"https://username:token@github.com/user/repo"
84+
)]
85+
fn test_format_url(
86+
#[case] url: &str,
87+
#[case] hide_token: bool,
88+
#[case] http_url: bool,
89+
#[case] expected: &str,
90+
) {
91+
assert_eq!(format_url(url, hide_token, http_url), expected);
92+
}
93+
94+
#[rstest]
95+
#[case("git@github.com:user/repo.git", "https://github.com/user/repo.git")]
96+
#[case("git@gitlab.com:user/repo", "https://gitlab.com/user/repo")]
97+
fn test_create_http_url_from_ssh(#[case] url: &str, #[case] expected: &str) {
98+
assert_eq!(create_http_url_from_ssh(url), expected);
99+
}
100+
}

‎src/data/manifest.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ pub fn get_manifests<P: AsRef<Path>>(path: P) -> Result<Vec<Manifest>> {
4444
Ok(manifests)
4545
}
4646

47+
pub(crate) fn get_manifest(repo_path: &Path) -> Result<Option<Manifest>> {
48+
let manifests = get_manifests(repo_path)?;
49+
50+
if manifests.is_empty() {
51+
Ok(None)
52+
} else {
53+
Ok(manifests.first().cloned())
54+
}
55+
}
56+
4757
fn parse_cargo_manifest(path: &Path) -> Result<Manifest> {
4858
let m = cargo_toml::Manifest::from_path(path)
4959
.with_context(|| format!("Failed to parse Cargo.toml at '{}'", path.display()))?;

‎src/data/mod.rs‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use self::builder::InfoBuilder;
2-
use self::fields::url::get_repo_url;
32
use self::git::bots::BotRegex;
4-
use self::git::{get_work_dir, traverse_commit_graph, uses_reftables};
5-
use self::manifest::Manifest;
3+
use self::git::{get_repo_url, get_work_dir, traverse_commit_graph, uses_reftables};
4+
use self::manifest::get_manifest;
65
use self::title::Title;
76
use crate::language::{Language, LanguageType};
87
use anyhow::{Context, Result, bail};
98
use serde::Serialize;
10-
use std::path::{Path, PathBuf};
9+
use std::path::PathBuf;
1110

1211
mod builder;
1312
pub(crate) mod dates;
@@ -116,13 +115,3 @@ pub fn build_info(options: &InfoOptions) -> Result<Info> {
116115
.license(&repo_path, manifest.as_ref())?
117116
.build(dominant_language))
118117
}
119-
120-
fn get_manifest(repo_path: &Path) -> Result<Option<Manifest>> {
121-
let manifests = manifest::get_manifests(repo_path)?;
122-
123-
if manifests.is_empty() {
124-
Ok(None)
125-
} else {
126-
Ok(manifests.first().cloned())
127-
}
128-
}

‎src/data/title.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::data::git::{get_git_username, get_git_version};
21
use gix::Repository;
32
use serde::Serialize;
43

@@ -17,3 +16,19 @@ impl Title {
1716
}
1817
}
1918
}
19+
20+
fn get_git_username(repo: &Repository) -> String {
21+
repo.committer()
22+
.and_then(Result::ok)
23+
.map(|c| c.name.to_string())
24+
.unwrap_or_default()
25+
}
26+
27+
fn get_git_version() -> String {
28+
let version = std::process::Command::new("git").arg("--version").output();
29+
30+
match version {
31+
Ok(v) => String::from_utf8_lossy(&v.stdout).replace('\n', ""),
32+
Err(_) => String::new(),
33+
}
34+
}

‎src/options.rs‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
//! collect from the repository (`InfoOptions`) and how to print it
33
//! (`RenderOptions`). Neither `data` nor `render` depends on `cli`.
44
5-
use crate::cli::Cli;
5+
use crate::cli::{Cli, When};
66
use crate::data::InfoOptions;
77
use crate::render::RenderOptions;
8+
use std::env;
89

910
impl From<&Cli> for InfoOptions {
1011
fn from(cli: &Cli) -> Self {
@@ -43,17 +44,30 @@ impl From<&Cli> for RenderOptions {
4344
no_bold: cli.text_formatting.no_bold,
4445
number_separator: cli.text_formatting.number_separator,
4546
iso_time: cli.text_formatting.iso_time,
46-
true_color: cli.true_color(),
47+
true_color: true_color(&cli.ascii.true_color),
4748
nerd_fonts: cli.visuals.nerd_fonts,
4849
no_color_palette: cli.visuals.no_color_palette,
4950
}
5051
}
5152
}
5253

54+
fn true_color(when: &When) -> bool {
55+
match when {
56+
When::Always => true,
57+
When::Never => false,
58+
When::Auto => is_truecolor_terminal(),
59+
}
60+
}
61+
62+
fn is_truecolor_terminal() -> bool {
63+
env::var("COLORTERM")
64+
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
65+
.unwrap_or(false)
66+
}
67+
5368
#[cfg(test)]
5469
mod tests {
5570
use super::*;
56-
use crate::cli::When;
5771

5872
#[test]
5973
fn default_render_options_match_cli_defaults() {

0 commit comments

Comments
 (0)