mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-22 12:58:26 +00:00
Adds a little logic to main to get other mappings from config
This commit is contained in:
parent
92e93682c6
commit
48b4a6a906
@ -8,7 +8,7 @@ mod config;
|
|||||||
mod directories;
|
mod directories;
|
||||||
mod input;
|
mod input;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{BufReader, Write};
|
use std::io::{BufReader, Write};
|
||||||
@ -22,9 +22,11 @@ use crate::{
|
|||||||
app::App,
|
app::App,
|
||||||
config::{config_file, generate_config_file},
|
config::{config_file, generate_config_file},
|
||||||
};
|
};
|
||||||
|
|
||||||
use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
|
use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
|
||||||
use clap::crate_version;
|
use clap::crate_version;
|
||||||
use directories::PROJECT_DIRS;
|
use directories::PROJECT_DIRS;
|
||||||
|
use globset::GlobMatcher;
|
||||||
|
|
||||||
use bat::{
|
use bat::{
|
||||||
assets::HighlightingAssets,
|
assets::HighlightingAssets,
|
||||||
@ -33,6 +35,7 @@ use bat::{
|
|||||||
error::*,
|
error::*,
|
||||||
input::Input,
|
input::Input,
|
||||||
style::{StyleComponent, StyleComponents},
|
style::{StyleComponent, StyleComponents},
|
||||||
|
MappingTarget,
|
||||||
};
|
};
|
||||||
|
|
||||||
const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs");
|
const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs");
|
||||||
@ -59,15 +62,43 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_syntax_mapping_to_paths(
|
||||||
|
mappings: Vec<(GlobMatcher, MappingTarget)>,
|
||||||
|
) -> HashMap<String, Vec<String>> {
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
for mapping in mappings {
|
||||||
|
match mapping.1 {
|
||||||
|
MappingTarget::MapToUnknown => {}
|
||||||
|
MappingTarget::MapTo(s) => {
|
||||||
|
let globs = map.entry(s.into()).or_insert(Vec::new());
|
||||||
|
globs.push(mapping.0.glob().glob().into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map
|
||||||
|
}
|
||||||
|
|
||||||
pub fn list_languages(config: &Config) -> Result<()> {
|
pub fn list_languages(config: &Config) -> Result<()> {
|
||||||
let assets = assets_from_cache_or_binary()?;
|
let assets = assets_from_cache_or_binary()?;
|
||||||
let mut languages = assets
|
let mut languages = assets
|
||||||
.syntaxes()
|
.syntaxes()
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
|
.filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
|
||||||
|
.cloned()
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
languages.sort_by_key(|lang| lang.name.to_uppercase());
|
languages.sort_by_key(|lang| lang.name.to_uppercase());
|
||||||
|
|
||||||
|
let configured_languages =
|
||||||
|
get_syntax_mapping_to_paths(config.syntax_mapping.mappings().clone());
|
||||||
|
|
||||||
|
for lang in languages.iter_mut() {
|
||||||
|
if configured_languages.contains_key(&lang.name) {
|
||||||
|
let additional_paths = configured_languages.get(&lang.name).unwrap();
|
||||||
|
lang.file_extensions
|
||||||
|
.extend(additional_paths.iter().cloned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut stdout = stdout.lock();
|
let mut stdout = stdout.lock();
|
||||||
|
|
||||||
|
@ -59,6 +59,10 @@ impl<'a> SyntaxMapping<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn mappings(&self) -> &Vec<(GlobMatcher, MappingTarget<'a>)> {
|
||||||
|
&self.mappings
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
||||||
let candidate = Candidate::new(path.as_ref());
|
let candidate = Candidate::new(path.as_ref());
|
||||||
let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
|
let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user