Skip to content

Commit

Permalink
Refactor: utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Nov 16, 2021
1 parent 24a07ff commit a8dd6a9
Show file tree
Hide file tree
Showing 22 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use structopt::{clap, StructOpt};
use syntect::highlighting::Theme as SyntaxTheme;
use syntect::parsing::SyntaxSet;

use crate::bat_utils::assets::HighlightingAssets;
use crate::bat_utils::output::PagingMode;
use crate::git_config::{GitConfig, GitConfigEntry};
use crate::options;
use crate::utils::bat::assets::HighlightingAssets;
use crate::utils::bat::output::PagingMode;

// No Default trait as this ignores `default_value = ..`
#[derive(StructOpt)]
Expand Down
9 changes: 4 additions & 5 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use ansi_term::Color;
use lazy_static::lazy_static;
use syntect::highlighting::Color as SyntectColor;

use crate::bat_utils::terminal::to_ansi_color;
use crate::fatal;
use crate::syntect_utils;
use crate::utils;

pub fn parse_color(s: &str, true_color: bool) -> Option<Color> {
if s == "normal" {
Expand All @@ -21,11 +20,11 @@ pub fn parse_color(s: &str, true_color: bool) -> Option<Color> {
} else {
s.parse::<u8>()
.ok()
.and_then(syntect_utils::syntect_color_from_ansi_number)
.or_else(|| syntect_utils::syntect_color_from_ansi_name(s))
.and_then(utils::syntect::syntect_color_from_ansi_number)
.or_else(|| utils::syntect::syntect_color_from_ansi_name(s))
.unwrap_or_else(die)
};
to_ansi_color(syntect_color, true_color)
utils::bat::terminal::to_ansi_color(syntect_color, true_color)
}

pub fn color_to_string(color: Color) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use syntect::parsing::SyntaxSet;
use unicode_segmentation::UnicodeSegmentation;

use crate::ansi;
use crate::bat_utils::output::PagingMode;
use crate::cli;
use crate::color;
use crate::delta::State;
Expand All @@ -21,8 +20,9 @@ use crate::git_config::{GitConfig, GitConfigEntry};
use crate::minusplus::MinusPlus;
use crate::paint::BgFillMethod;
use crate::style::{self, Style};
use crate::syntect_utils::FromDeltaStyle;
use crate::tests::TESTING;
use crate::utils::bat::output::PagingMode;
use crate::utils::syntect::FromDeltaStyle;
use crate::wrapping::WrapConfig;

pub const INLINE_SYMBOL_WIDTH_1: usize = 1;
Expand Down Expand Up @@ -647,9 +647,9 @@ pub const HEADER_LEN: usize = 7;

#[cfg(test)]
pub mod tests {
use crate::bat_utils::output::PagingMode;
use crate::cli;
use crate::tests::integration_test_utils;
use crate::utils::bat::output::PagingMode;
use std::fs::remove_file;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/blame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'a> StateMachine<'a> {

// Emit syntax-highlighted code
if matches!(self.state, State::Unknown) {
if let Some(lang) = utils::git_blame_filename_extension()
if let Some(lang) = utils::process::git_blame_filename_extension()
.as_ref()
.or_else(|| self.config.default_language.as_ref())
{
Expand Down
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ extern crate error_chain;

mod align;
mod ansi;
#[cfg(not(tarpaulin_include))]
mod bat_utils;
mod cli;
mod color;
mod config;
Expand All @@ -26,7 +24,6 @@ mod utils;
mod wrapping;

mod subcommands;
mod syntect_utils;

mod tests;

Expand All @@ -35,9 +32,9 @@ use std::process;

use bytelines::ByteLinesReader;

use crate::bat_utils::assets::{list_languages, HighlightingAssets};
use crate::bat_utils::output::OutputType;
use crate::delta::delta;
use crate::utils::bat::assets::{list_languages, HighlightingAssets};
use crate::utils::bat::output::OutputType;

pub fn fatal<T>(errmsg: T) -> !
where
Expand Down
6 changes: 3 additions & 3 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::str::FromStr;
use console::Term;
use structopt::clap;

use crate::bat_utils::assets::HighlightingAssets;
use crate::bat_utils::output::PagingMode;
use crate::cli;
use crate::config;
use crate::env;
Expand All @@ -17,6 +15,8 @@ use crate::features;
use crate::git_config::{GitConfig, GitConfigEntry};
use crate::options::option_value::{OptionValue, ProvenancedOptionValue};
use crate::options::theme;
use crate::utils::bat::assets::HighlightingAssets;
use crate::utils::bat::output::PagingMode;

macro_rules! set_options {
([$( $field_ident:ident ),* ],
Expand Down Expand Up @@ -644,9 +644,9 @@ fn set_git_config_entries(opt: &mut cli::Opt, git_config: &mut GitConfig) {
pub mod tests {
use std::fs::remove_file;

use crate::bat_utils::output::PagingMode;
use crate::cli;
use crate::tests::integration_test_utils;
use crate::utils::bat::output::PagingMode;

#[test]
fn test_options_can_be_set_in_git_config() {
Expand Down
2 changes: 1 addition & 1 deletion src/options/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
/// default is selected.
use syntect::highlighting::ThemeSet;

use crate::bat_utils::assets::HighlightingAssets;
use crate::cli;
use crate::env;
use crate::utils::bat::assets::HighlightingAssets;

#[allow(non_snake_case)]
pub fn set__is_light_mode__syntax_theme__syntax_set(
Expand Down
2 changes: 1 addition & 1 deletion src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,8 @@ fn is_whitespace_error(sections: &[(Style, &str)]) -> bool {
mod superimpose_style_sections {
use syntect::highlighting::Style as SyntectStyle;

use crate::bat_utils::terminal::to_ansi_color;
use crate::style::Style;
use crate::utils::bat::terminal::to_ansi_color;

pub fn superimpose_style_sections(
sections_1: &[(SyntectStyle, &str)],
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/list_syntax_themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::io::{self, Write};

use itertools::Itertools;

use crate::bat_utils::assets::HighlightingAssets;
use crate::options::theme::is_light_syntax_theme;
use crate::utils::bat::assets::HighlightingAssets;

#[cfg(not(tarpaulin_include))]
pub fn list_syntax_themes() -> std::io::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/show_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::io::Write;

use itertools::Itertools;

use crate::bat_utils::output::PagingMode;
use crate::cli;
use crate::config;
use crate::features::side_by_side::{Left, Right};
use crate::minusplus::*;
use crate::paint::BgFillMethod;
use crate::style;
use crate::utils::bat::output::PagingMode;

pub fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Result<()> {
// styles first
Expand Down
4 changes: 2 additions & 2 deletions src/subcommands/show_syntax_themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::io::{self, ErrorKind, Read, Write};

use structopt::StructOpt;

use crate::bat_utils::assets::HighlightingAssets;
use crate::bat_utils::output::{OutputType, PagingMode};
use crate::cli;
use crate::config;
use crate::delta;
use crate::options::theme::is_light_syntax_theme;
use crate::utils::bat::assets::HighlightingAssets;
use crate::utils::bat::output::{OutputType, PagingMode};

#[cfg(not(tarpaulin_include))]
pub fn show_syntax_themes() -> std::io::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/show_themes.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::io::{self, ErrorKind, Read};

use crate::bat_utils::output::{OutputType, PagingMode};
use crate::cli;
use crate::config;
use crate::delta;
use crate::git_config;
use crate::options::get::get_themes;
use crate::utils::bat::output::{OutputType, PagingMode};

pub fn show_themes(dark: bool, light: bool, computed_theme_is_light: bool) -> std::io::Result<()> {
use std::io::BufReader;
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/bat_utils/assets.rs → src/utils/bat/assets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Based on code from https://github.com/sharkdp/bat a1b9334a44a2c652f52dddaa83dbacba57372468
// See src/bat_utils/LICENSE
// See src/utils/bat/LICENSE

use std::fs::File;
use std::io::{self, BufReader, Write};
Expand All @@ -11,8 +11,8 @@ use syntect::dumps::{from_binary, from_reader};
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;

use crate::bat_utils::dirs::PROJECT_DIRS;
use crate::errors::*;
use crate::utils::bat::dirs::PROJECT_DIRS;

pub struct HighlightingAssets {
pub syntax_set: SyntaxSet,
Expand All @@ -25,11 +25,11 @@ impl HighlightingAssets {
}

fn get_integrated_syntaxset() -> SyntaxSet {
from_binary(include_bytes!("../../etc/assets/syntaxes.bin"))
from_binary(include_bytes!("../../../etc/assets/syntaxes.bin"))
}

fn get_integrated_themeset() -> ThemeSet {
from_binary(include_bytes!("../../etc/assets/themes.bin"))
from_binary(include_bytes!("../../../etc/assets/themes.bin"))
}

fn from_cache() -> Result<Self> {
Expand Down
2 changes: 1 addition & 1 deletion src/bat_utils/dirs.rs → src/utils/bat/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Based on code from https://github.com/sharkdp/bat e981e974076a926a38f124b7d8746de2ca5f0a28
// See src/bat_utils/LICENSE
// See src/utils/bat/LICENSE

use lazy_static::lazy_static;
use std::path::{Path, PathBuf};
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/bat_utils/output.rs → src/utils/bat/output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://github.com/sharkdp/bat a1b9334a44a2c652f52dddaa83dbacba57372468
// src/output.rs
// See src/bat_utils/LICENSE
// See src/utils/bat/LICENSE
use std::env;
use std::ffi::OsString;
use std::io::{self, Write};
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(not(tarpaulin_include))]
pub mod bat;
pub mod process;
pub mod syntect;
File renamed without changes.
File renamed without changes.

0 comments on commit a8dd6a9

Please sign in to comment.