Debian Codegraph @

src/utils/bat/assets.rs

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Based on code from https://github.com/sharkdp/bat a1b9334a44a2c652f52dddaa83dbacba57372468
2 // See src/utils/bat/LICENSE
3
4 use std::io::{self, Write};
5
6 use ansi_term::Colour::Green;
7 use ansi_term::Style;
8 use bat;
9
10 use crate::utils;
11
12 pub fn load_highlighting_assets() -> bat::assets::HighlightingAssets {
13 bat::assets::HighlightingAssets::from_cache(utils::bat::dirs::PROJECT_DIRS.cache_dir())
14 .unwrap_or_else(|_| bat::assets::HighlightingAssets::from_binary())
15 }
16
17 pub fn list_languages() -> std::io::Result<()> {
18 let assets = utils::bat::assets::load_highlighting_assets();
19 let mut languages = assets
20 .get_syntaxes()
21 .unwrap()
22 .iter()
23 .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
24 .collect::<Vec<_>>();
25 languages.sort_by_key(|lang| lang.name.to_uppercase());
26
27 let loop_through = false;
28 let colored_output = true;
29
30 let stdout = io::stdout();
31 let mut stdout = stdout.lock();
32
33 if loop_through {
34 for lang in languages {
35 writeln!(stdout, "{}:{}", lang.name, lang.file_extensions.join(","))?;
36 }
37 } else {
38 let longest = languages
39 .iter()
40 .map(|syntax| syntax.name.len())
41 .max()
42 .unwrap_or(32); // Fallback width if they have no language definitions.
43
44 let comma_separator = ", ";
45 let separator = " ";
46 // Line-wrapping for the possible file extension overflow.
47 let desired_width = 100;
48
49 let style = if colored_output {
50 Green.normal()
51 } else {
52 Style::default()
53 };
54
55 for lang in languages {
56 write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
57
58 // Number of characters on this line so far, wrap before `desired_width`
59 let mut num_chars = 0;
60
61 let mut extension = lang.file_extensions.iter().peekable();
62 while let Some(word) = extension.next() {
63 // If we can't fit this word in, then create a line break and align it in.
64 let new_chars = word.len() + comma_separator.len();
65 if num_chars + new_chars >= desired_width {
66 num_chars = 0;
67 write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
68 }
69
70 num_chars += new_chars;
71 write!(stdout, "{}", style.paint(&word[..]))?;
72 if extension.peek().is_some() {
73 write!(stdout, "{comma_separator}")?;
74 }
75 }
76 writeln!(stdout)?;
77 }
78 }
79
80 Ok(())
81 }