| 1 |
// Based on code from https://github.com/sharkdp/bat a1b9334a44a2c652f52dddaa83dbacba57372468 |
| 2 |
// See src/utils/bat/LICENSE |
| 3 |
|
| 4 |
use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 5 |
|
| •6 |
use ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Colorrust-analyzer cargo nu-ansi-term 0.50.3 style/Color#::Greenrust-analyzer cargo nu-ansi-term 0.50.3 style/Color#Green#; |
| 7 |
use ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style#; |
| 8 |
use batrust-analyzer cargo bat 0.26.1 crate/; |
| 9 |
|
| 10 |
use craterust-analyzer cargo git-delta 0.19.2 crate/::utilsrust-analyzer cargo git-delta 0.19.2 utils/; |
| 11 |
|
| 12 |
pub fn load_highlighting_assetsrust-analyzer cargo git-delta 0.19.2 utils/bat/assets/load_highlighting_assets().() -> batrust-analyzer cargo bat 0.26.1 crate/::assetsrust-analyzer cargo bat 0.26.1 assets/::HighlightingAssetsrust-analyzer cargo bat 0.26.1 assets/HighlightingAssets# { |
| 13 |
batrust-analyzer cargo bat 0.26.1 crate/::assetsrust-analyzer cargo bat 0.26.1 assets/::HighlightingAssetsrust-analyzer cargo bat 0.26.1 assets/HighlightingAssets#::from_cacherust-analyzer cargo bat 0.26.1 assets/impl#[HighlightingAssets]from_cache().(utilsrust-analyzer cargo git-delta 0.19.2 utils/::batrust-analyzer cargo git-delta 0.19.2 utils/bat/::dirsrust-analyzer cargo git-delta 0.19.2 utils/bat/dirs/::PROJECT_DIRSrust-analyzer cargo git-delta 0.19.2 utils/bat/dirs/PROJECT_DIRS..cache_dirrust-analyzer cargo git-delta 0.19.2 utils/bat/dirs/impl#[BatProjectDirs]cache_dir().()) |
| 14 |
.unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| batrust-analyzer cargo bat 0.26.1 crate/::assetsrust-analyzer cargo bat 0.26.1 assets/::HighlightingAssetsrust-analyzer cargo bat 0.26.1 assets/HighlightingAssets#::from_binaryrust-analyzer cargo bat 0.26.1 assets/impl#[HighlightingAssets]from_binary().()) |
| 15 |
} |
| 16 |
|
| 17 |
pub fn list_languagesrust-analyzer cargo git-delta 0.19.2 utils/bat/assets/list_languages().() -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 18 |
let assets = utilsrust-analyzer cargo git-delta 0.19.2 utils/::batrust-analyzer cargo git-delta 0.19.2 utils/bat/::assetsrust-analyzer cargo git-delta 0.19.2 utils/bat/assets/::load_highlighting_assetsrust-analyzer cargo git-delta 0.19.2 utils/bat/assets/load_highlighting_assets().(); |
| 19 |
let mut languages = assets |
| 20 |
.get_syntaxesrust-analyzer cargo bat 0.26.1 assets/impl#[HighlightingAssets]get_syntaxes().() |
| 21 |
.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 22 |
.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 23 |
.filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|syntax| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().syntax.hiddenrust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#hidden. && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().syntax.file_extensionsrust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#file_extensions..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().()) |
| 24 |
.collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 25 |
languages.sort_by_keyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by_key().(|lang| lang.namerust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#name..to_uppercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_uppercase().()); |
| 26 |
|
| 27 |
let loop_through = false; |
| 28 |
let colored_output = true; |
| 29 |
|
| 30 |
let stdout = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().(); |
| 31 |
let mut stdout = stdout.lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout]lock().(); |
| 32 |
|
| 33 |
if loop_through { |
| 34 |
for lang in languages { |
| 35 |
writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stdout, "{}:{}", lang.namerust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#name., lang.file_extensionsrust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#file_extensions..joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(","))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 36 |
} |
| 37 |
} else { |
| 38 |
let longest = languages |
| 39 |
.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 40 |
.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|syntax| syntax.namerust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#name..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().()) |
| 41 |
.maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#max().() |
| 42 |
.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]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 |
Greenrust-analyzer cargo nu-ansi-term 0.50.3 style/Color#Green#.normalrust-analyzer cargo nu-ansi-term 0.50.3 style/impl#[Color]normal().() |
| 51 |
} else { |
| 52 |
Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style#::defaultrust-analyzer cargo nu-ansi-term 0.50.3 style/impl#[Style][Default]default().() |
| 53 |
}; |
| 54 |
|
| 55 |
for lang in languages { |
| 56 |
writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(stdout, "{:width$}{}", lang.namerust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#name., separator, width = longest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 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_extensionsrust-analyzer cargo syntect 5.3.0 parsing/syntax_set/SyntaxReference#file_extensions..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().(); |
| 62 |
while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(word) = extension.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().() { |
| 63 |
// If we can't fit this word in, then create a line break and align it in. |
| 64 |
let new_chars = word.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().comma_separator.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().(); |
| 65 |
if num_chars rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().new_chars rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().desired_width { |
| 66 |
num_chars = 0; |
| 67 |
writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(stdout, "\n{:width$}{}", "", separator, width = longest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 68 |
} |
| 69 |
|
| 70 |
num_chars rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().new_chars; |
| 71 |
writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(stdout, "{}", style.paintrust-analyzer cargo nu-ansi-term 0.50.3 display/impl#[Style]paint().(&word[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 72 |
if extension.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 73 |
writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(stdout, "{comma_separator}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 74 |
} |
| 75 |
} |
| 76 |
writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stdout)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 81 |
} |