Skip to content

Commit

Permalink
feat: determine system locale to use in locale-aware unicode sorting
Browse files Browse the repository at this point in the history
Prerequisite to an implementation of #1006[1]
following the example of tidy's `sort_carefully`[2].

[1]: #1006
[2]: https://github.com/sts10/tidy/blob/main/src/list_manipulations.rs#L24
  • Loading branch information
LeoniePhiline committed Jul 4, 2024
1 parent d414d4b commit 1549944
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ name = "eza"
nu-ansi-term = "0.50.0"
chrono = { version = "0.4.34", default-features = false, features = ["clock"] }
glob = "0.3"
icu_locid = "1.5.0"
libc = "0.2"
locale = "0.2"
log = "0.4"
Expand Down
23 changes: 23 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ fn main() {
}
}

let lang = if let Ok(lang) = std::env::var("LANG") {
// Split e.g. "en_US.UTF-8", extracting "en_US".
match lang.split_once('.') {
Some((lang, _)) => lang.to_owned(),
None => lang,
}
} else {
// Fall back to "en_US" if the `LANG` environment variable is not set.
warn!("`LANG` is not set. Falling back to 'en_US'.");
String::from("en_US")
};

let locale: icu_locid::Locale = if let Ok(locale) = lang.parse() {
locale
} else {
warn!("Your locale '{lang}' could not be parsed. Falling back to 'en_US'.");
"en_US".parse().unwrap()
};

let git = git_options(&options, &input_paths);
let writer = io::stdout();
let git_repos = git_repos(&options, &input_paths);
Expand All @@ -91,6 +110,7 @@ fn main() {
options,
writer,
input_paths,
locale,
theme,
console_width,
git,
Expand Down Expand Up @@ -149,6 +169,9 @@ pub struct Exa<'args> {
/// names (anything that isn’t an option).
pub input_paths: Vec<&'args OsStr>,

/// The system locale, used for locale-aware unicode string sorting.
pub locale: icu_locid::Locale,

/// The theme that has been configured from the command-line options and
/// environment variables. If colours are disabled, this is a theme with
/// every style set to the default.
Expand Down

0 comments on commit 1549944

Please sign in to comment.