Skip to content

Commit

Permalink
Make the output order deterministic
Browse files Browse the repository at this point in the history
Fixes #63.
  • Loading branch information
tavianator authored and sharkdp committed Jun 13, 2022
1 parent 700c9a6 commit 499f813
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,21 @@ fn run() -> Result<()> {
if let Some(sub_matches) = matches.subcommand_matches("generate") {
let theme = load_theme(&sub_matches, &user_config_path, color_mode)?;

let mut filetypes_list = filetypes.mapping.keys().collect::<Vec<_>>();
filetypes_list.sort_unstable_by_key(|entry| entry.len());

let mut ls_colors: Vec<String> = vec![];
for filetype in filetypes_list {
let category = &filetypes.mapping[filetype];
ls_colors.push(format!("{}={}", filetype, theme.get_style(&category)?));
}
let mut mapping = filetypes
.mapping
.iter()
.map(|(filetype, category)| (filetype, theme.get_style(category)))
.map(|(filetype, style)| style.map(|style| (filetype, style)))
.collect::<Result<Vec<_>>>()?;

// Sort the keys deterministically. Shorter keys come first so that e.g.
// *README.md will override *.md.
mapping.sort_unstable_by_key(|&(filetype, _)| (filetype.len(), filetype));

let ls_colors: Vec<_> = mapping
.iter()
.map(|(filetype, style)| format!("{}={}", filetype, style))
.collect();

writeln!(stdout_lock, "{}", ls_colors.join(":")).ok();
} else if let Some(sub_matches) = matches.subcommand_matches("preview") {
Expand Down

0 comments on commit 499f813

Please sign in to comment.