From 0c0b8d8f579d090a1fa09c546fc0f65a56e36923 Mon Sep 17 00:00:00 2001 From: Lukas Bergdoll Date: Sat, 8 Sep 2018 14:22:03 +0200 Subject: [PATCH] Generalize config Else indentation Serde rename Generalize config, rename and make style member --- README.md | 18 +++++------ src/config.rs | 50 ++++++++++++++++++----------- src/formatter.rs | 38 ++++------------------ src/main.rs | 22 ++++++------- tests/config.toml | 22 +++++++++++++ tests/inkscape-default.expected | 4 +-- tests/inkscape-with-config.expected | 4 +-- tests/lib.rs | 4 +-- tests/syntax.toml | 22 ------------- 9 files changed, 87 insertions(+), 97 deletions(-) create mode 100644 tests/config.toml delete mode 100644 tests/syntax.toml diff --git a/README.md b/README.md index 3c03a64..441516f 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Creating the config file can be done manually or with the help of tldr. tldr --seed-config The command should print the location where the command created the config file. -Example: `~/.config/tealdeer/syntax.toml` +Example: `~/.config/tealdeer/config.toml` The currently supported attributes are: @@ -103,14 +103,14 @@ The currently supported attributes are: The currently supported colors are: -- `Black` -- `Red` -- `Green` -- `Yellow` -- `Blue` -- `Purple` -- `Cyan` -- `White` +- `black` +- `red` +- `green` +- `yellow` +- `blue` +- `purple` +- `cyan` +- `white` Example customization: ![screenshot](screenshot-custom.png) diff --git a/src/config.rs b/src/config.rs index e2f35a0..c9070eb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,8 +9,9 @@ use xdg::BaseDirectories; use error::TealdeerError::{self, ConfigError}; -pub const SYNTAX_CONFIG_FILE_NAME: &'static str = "syntax.toml"; +pub const SYNTAX_CONFIG_FILE_NAME: &'static str = "config.toml"; +#[serde(rename_all = "lowercase")] #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub enum RawColor { Black, @@ -81,43 +82,56 @@ impl From for Style { } } +#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)] +struct RawStyleConfig { + pub highlight: RawStyle, + pub description: RawStyle, + pub example_text: RawStyle, + pub example_code: RawStyle, + pub example_variable: RawStyle, +} + + #[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)] struct RawConfig { - pub highlight_style: RawStyle, - pub description_style: RawStyle, - pub example_text_style: RawStyle, - pub example_code_style: RawStyle, - pub example_variable_style: RawStyle, + style: RawStyleConfig, } impl RawConfig { fn new() -> RawConfig { let mut raw_config = RawConfig::default(); - raw_config.highlight_style.foreground = Some(RawColor::Red); - raw_config.example_variable_style.underline = true; + raw_config.style.highlight.foreground = Some(RawColor::Red); + raw_config.style.example_variable.underline = true; raw_config } } // impl RawConfig +#[derive(Copy, Clone, Debug, PartialEq)] +pub struct StyleConfig { + pub highlight: Style, + pub description: Style, + pub example_text: Style, + pub example_code: Style, + pub example_variable: Style, +} + #[derive(Copy, Clone, Debug, PartialEq)] pub struct Config { - pub highlight_style: Style, - pub description_style: Style, - pub example_text_style: Style, - pub example_code_style: Style, - pub example_variable_style: Style, + pub style: StyleConfig, } impl From for Config { fn from(raw_config: RawConfig) -> Config { Config{ - highlight_style: raw_config.highlight_style.into(), - description_style: raw_config.description_style.into(), - example_text_style: raw_config.example_text_style.into(), - example_code_style: raw_config.example_code_style.into(), - example_variable_style: raw_config.example_variable_style.into(), + style: StyleConfig{ + highlight: raw_config.style.highlight.into(), + description: raw_config.style.description.into(), + example_text: raw_config.style.example_text.into(), + example_code: raw_config.style.example_code.into(), + example_variable: raw_config.style.example_variable.into(), + } } } } diff --git a/src/formatter.rs b/src/formatter.rs index 4192454..da6f606 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -1,6 +1,5 @@ //! Functions related to formatting and printing lines from a `Tokenizer`. -use std::fmt::Write; use std::io::BufRead; use ansi_term::{ANSIString, ANSIStrings}; @@ -18,12 +17,12 @@ fn highlight_command<'a>( let mut code_part_end_pos = 0; while let Some(command_start) = example_code[code_part_end_pos..].find(&command) { let code_part = &example_code[code_part_end_pos..code_part_end_pos + command_start]; - parts.push(config.example_code_style.paint(code_part)); - parts.push(config.highlight_style.paint(command)); + parts.push(config.style.example_code.paint(code_part)); + parts.push(config.style.highlight.paint(command)); code_part_end_pos += command_start + command.len(); } - parts.push(config.example_code_style.paint(&example_code[code_part_end_pos..])); + parts.push(config.style.example_code.paint(&example_code[code_part_end_pos..])); } /// Format and highlight code examples including variables in {{ curly braces }}. @@ -35,9 +34,8 @@ fn format_code(command: &str, text: &str, config: &Config) -> String { let example_variable = &between_variables[variable_start + 2..]; highlight_command(&command, &example_code, &config, &mut parts); - parts.push(config.example_variable_style.paint(example_variable)); - } - else { + parts.push(config.style.example_variable.paint(example_variable)); + } else { highlight_command(&command, &between_variables, &config, &mut parts); } } @@ -45,28 +43,6 @@ fn format_code(command: &str, text: &str, config: &Config) -> String { ANSIStrings(&parts).to_string() } -/// Format and highlight description text. -fn format_description(description: &str, config: &Config) -> String { - if let Some(first_space) = description.find(' ') { - let mut highlighted_description = String::new(); - write!( - highlighted_description, - "{}", - config.highlight_style.paint(&description[..first_space]) - ).unwrap(); - - write!( - highlighted_description, - "{}", - config.description_style.paint(&description[first_space..]) - ).unwrap(); - - return highlighted_description; - } - - String::from(description) -} - /// Print a token stream to an ANSI terminal. pub fn print_lines(tokenizer: &mut Tokenizer, config: &Config) where R: BufRead { let mut command = String::new(); @@ -81,8 +57,8 @@ pub fn print_lines(tokenizer: &mut Tokenizer, config: &Config) where R: Bu command = title; debug!("Detected command name: {}", &command); }, - LineType::Description(text) => println!(" {}", format_description(&text, &config)), - LineType::ExampleText(text) => println!(" {}", config.example_text_style.paint(text)), + LineType::Description(text) => println!(" {}", config.style.description.paint(text)), + LineType::ExampleText(text) => println!(" {}", config.style.example_text.paint(text)), LineType::ExampleCode(text) => println!(" {}", &format_code(&command, &text, &config)), LineType::Other(text) => debug!("Unknown line type: {:?}", text), } diff --git a/src/main.rs b/src/main.rs index a52be47..b34b5e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,15 +68,15 @@ Usage: Options: - -h --help Show this screen - -v --version Show version information - -l --list List all commands in the cache - -f --render Render a specific markdown file - -o --os Override the operating system [linux, osx, sunos] - -u --update Update the local cache - -c --clear-cache Clear the local cache - --display-config Show config directory path - --seed-config Create a basic config + -h --help Show this screen + -v --version Show version information + -l --list List all commands in the cache + -f --render Render a specific markdown file + -o --os Override the operating system [linux, osx, sunos] + -u --update Update the local cache + -c --clear-cache Clear the local cache + --config-path Show config directory path + --seed-config Create a basic config Examples: @@ -105,7 +105,7 @@ struct Args { flag_os: Option, flag_update: bool, flag_clear_cache: bool, - flag_display_config: bool, + flag_config_path: bool, flag_seed_config: bool, } @@ -274,7 +274,7 @@ fn main() { } // Show config file and path and exit - if args.flag_display_config { + if args.flag_config_path { match get_config_dir() { Ok(config_file_path) => { println!("Config directory path is: {}", config_file_path.to_str().unwrap()); diff --git a/tests/config.toml b/tests/config.toml new file mode 100644 index 0000000..5eaae22 --- /dev/null +++ b/tests/config.toml @@ -0,0 +1,22 @@ +[style.highlight] +foreground = "green" +underline = false +bold = false + +[style.description] +underline = false +bold = false + +[style.example_text] +foreground = "black" +background = "blue" +underline = false +bold = false + +[style.example_code] +underline = false +bold = false + +[style.example_variable] +underline = true +bold = false diff --git a/tests/inkscape-default.expected b/tests/inkscape-default.expected index adc88ca..3c8146d 100644 --- a/tests/inkscape-default.expected +++ b/tests/inkscape-default.expected @@ -1,6 +1,6 @@ - An SVG (Scalable Vector Graphics) editing program. - Use -z to not open the GUI and only process files in the console. + An SVG (Scalable Vector Graphics) editing program. + Use -z to not open the GUI and only process files in the console. Open an SVG file in the Inkscape GUI: diff --git a/tests/inkscape-with-config.expected b/tests/inkscape-with-config.expected index 8e1521f..fcd9271 100644 --- a/tests/inkscape-with-config.expected +++ b/tests/inkscape-with-config.expected @@ -1,6 +1,6 @@ - An SVG (Scalable Vector Graphics) editing program. - Use -z to not open the GUI and only process files in the console. + An SVG (Scalable Vector Graphics) editing program. + Use -z to not open the GUI and only process files in the console. Open an SVG file in the Inkscape GUI: diff --git a/tests/lib.rs b/tests/lib.rs index bdb33bf..390c4fd 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -116,11 +116,11 @@ fn test_correct_rendering_with_config() { // Setup syntax config file // TODO should be config::SYNTAX_CONFIG_FILE_NAME - let syntax_file_path = testenv.config_dir.path().join("syntax.toml"); + let syntax_file_path = testenv.config_dir.path().join("config.toml"); println!("Syntax config path: {:?}", &syntax_file_path); let mut syntax_config_file = File::create(&syntax_file_path).unwrap(); - syntax_config_file.write(include_str!("syntax.toml").as_bytes()).unwrap(); + syntax_config_file.write(include_str!("config.toml").as_bytes()).unwrap(); // Create input file let file_path = testenv.input_dir.path().join("inkscape-v2.md"); diff --git a/tests/syntax.toml b/tests/syntax.toml deleted file mode 100644 index 935c1fd..0000000 --- a/tests/syntax.toml +++ /dev/null @@ -1,22 +0,0 @@ -[highlight_style] -foreground = "Green" -underline = false -bold = false - -[description_style] -underline = false -bold = false - -[example_text_style] -foreground = "Black" -background = "Blue" -underline = false -bold = false - -[example_code_style] -underline = false -bold = false - -[example_variable_style] -underline = true -bold = false