Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/onefetch.1
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Count hidden files and directories
.IP
Filters output by language type
.IP
If no language of the selected TYPE(s) is found, falls back to counting every language type instead of showing no languages at all
.IP
[default: programming markup]
[possible values: programming, markup, prose, data]
.SS "TEXT FORMATTING:"
Expand Down
2 changes: 2 additions & 0 deletions docs/wiki/command-line-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ INFO:
-T, --type <TYPE>...
Filters output by language type

If no language of the selected TYPE(s) is found, falls back to counting every language type instead of showing no languages at all

[default: programming markup]
[possible values: programming, markup, prose, data]

Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ pub struct InfoCliOptions {
#[arg(long)]
pub include_hidden: bool,
/// Filters output by language type
///
/// If no language of the selected TYPE(s) is found, falls back to counting
/// every language type instead of showing no languages at all
#[arg(
long,
num_args = 1..,
Expand Down
2 changes: 1 addition & 1 deletion src/info/langs/language.tera
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Colors {

const DEFAULT_CHIP_ICON: char = '\u{25CF}';

#[derive(Clone, PartialEq, Eq, Debug, clap::ValueEnum)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, EnumIter, clap::ValueEnum)]
pub enum LanguageType {
Programming,
Markup,
Expand Down
28 changes: 27 additions & 1 deletion src/info/langs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,32 @@ pub fn get_main_language(loc_by_language: &[(Language, usize)]) -> Language {
/// Returns a vector of tuples containing all the languages detected inside the repository.
/// Each tuple is composed of the language and its corresponding loc (lines of code).
/// The vector is sorted by loc in descending order.
///
/// If the given `language_types` filter doesn't match any language in the repository,
/// this falls back to counting every language type instead of reporting nothing.
/// See <https://github.com/o2sh/onefetch/issues/1705>.
pub fn get_loc_by_language_sorted(
dir: &Path,
globs_to_exclude: &[String],
language_types: &[LanguageType],
include_hidden: bool,
) -> Option<Vec<(Language, usize)>> {
let locs = get_locs(dir, globs_to_exclude, language_types, include_hidden);
let loc_by_language_opt = get_loc_by_language(&locs);
let loc_by_language_opt = get_loc_by_language(&locs).or_else(|| {
if all_types_selected(language_types) {
return None;
}
let all_types: Vec<LanguageType> = LanguageType::iter().collect();
let locs = get_locs(dir, globs_to_exclude, &all_types, include_hidden);
get_loc_by_language(&locs)
});
loc_by_language_opt.map(sort_by_loc)
}

fn all_types_selected(language_types: &[LanguageType]) -> bool {
LanguageType::iter().all(|t| language_types.contains(&t))
}

fn sort_by_loc(map: HashMap<Language, usize>) -> Vec<(Language, usize)> {
let mut vec: Vec<(Language, usize)> = map.into_iter().collect();
vec.sort_by_key(|b| cmp::Reverse(b.1));
Expand Down Expand Up @@ -200,4 +215,15 @@ mod test {
let loc_by_language = [(Language::JavaScript, 100), (Language::Markdown, 300)];
assert_eq!(get_total_loc(&loc_by_language), 400);
}

#[test]
fn all_types_selected_is_true_for_every_language_type() {
let all_types: Vec<LanguageType> = LanguageType::iter().collect();
assert!(all_types_selected(&all_types));
}

#[test]
fn all_types_selected_is_false_for_a_partial_selection() {
assert!(!all_types_selected(&[LanguageType::Programming]));
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/make_repo_with_only_prose.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set -eu -o pipefail

git init -q

# BOTH NAME AND EMAIL ARE NEEDED FOR RECOGNITION
git config --local --add "committer.name" "onefetch-committer-name"
git config --local --add "committer.email" "onefetch-committer-email@onefetch.com"

git remote add origin https://github.com/user/repo.git

git checkout -b main

# Markdown is prose, so the default `--type programming markup` matches nothing
# here and onefetch has to fall back to every type (cf. #1705)
cat <<'EOF' > README.md
# Title

Some prose, and no code at all.
EOF
git add README.md
git commit -q -m c1
16 changes: 16 additions & 0 deletions tests/repo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use gix::{Repository, ThreadSafeRepository, open};
use onefetch::cli::{CliOptions, InfoCliOptions, TextForamttingCliOptions};
use onefetch::info::langs::language::Language;
use onefetch::info::{build_info, get_work_dir};

fn repo(name: &str) -> Result<Repository> {
Expand Down Expand Up @@ -116,3 +117,18 @@ fn test_repo_without_code() -> Result<()> {
let _info = build_info(&config).expect("no error");
Ok(())
}

// https://github.com/o2sh/onefetch/issues/1705
#[test]
fn test_repo_with_only_prose_falls_back_to_all_language_types() -> Result<()> {
let repo = repo("make_repo_with_only_prose.sh")?;
let config: CliOptions = CliOptions {
input: repo.path().to_path_buf(),
..Default::default()
};
let info = build_info(&config)?;
// The default `--type programming markup` matches nothing in this repo, so
// without the fallback there is no dominant language and no Languages field.
assert_eq!(info.dominant_language, Some(Language::Markdown));
Ok(())
}
Loading