Generalize config

Else indentation
Serde rename
Generalize config, rename and make style member
This commit is contained in:
Lukas Bergdoll 2018-09-08 14:22:03 +02:00 committed by Danilo Bargen
commit 0c0b8d8f57
9 changed files with 87 additions and 97 deletions

View file

@ -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)

View file

@ -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<RawStyle> 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<RawConfig> 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(),
}
}
}
}

View file

@ -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<R>(tokenizer: &mut Tokenizer<R>, config: &Config) where R: BufRead {
let mut command = String::new();
@ -81,8 +57,8 @@ pub fn print_lines<R>(tokenizer: &mut Tokenizer<R>, 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),
}

View file

@ -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 <file> Render a specific markdown file
-o --os <type> 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 <file> Render a specific markdown file
-o --os <type> 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<OsType>,
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());

22
tests/config.toml Normal file
View file

@ -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

View file

@ -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:

View file

@ -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:

View file

@ -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");

View file

@ -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