Skip to content

Commit

Permalink
feat: add args
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed Dec 7, 2023
1 parent 876975e commit 1558121
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 17 deletions.
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ env_logger = "0.10.0"
syntect = { version = "5.1.0", default-features = false, features = [
"parsing",
"default-syntaxes",
"default-themes",
"regex-fancy",
"plist-load",
] }
clap = { version = "4.4.8", features = ["derive", "color", "env", "suggestions", "wrap_help"] }
clap = { version = "4.4.8", features = [
"derive",
"color",
"env",
"suggestions",
"wrap_help",
] }
thiserror = "1.0.50"
lewp-css = "0.2.0"
14 changes: 11 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ use std::path::PathBuf;

use clap::Parser;

use crate::error::CodeScreenshotError;

pub fn get_args() -> AppArgs {
AppArgs::parse()
}

#[derive(Parser)]
#[clap(author, version, about)]
pub struct AppArgs {
#[clap(long, short, help = "File to take screenshot", default_value = "false")]
pub print: bool,
#[clap(long, short, help = "File to take screenshot")]
pub file: Option<PathBuf>,
#[clap(long, short, help = "Theme for highlight")]
Expand All @@ -14,13 +22,13 @@ pub struct AppArgs {
pub lines: Option<Range<u32>>,
}

fn parse_range(s: &str) -> Result<Range<u32>, String> {
fn parse_range(s: &str) -> Result<Range<u32>, CodeScreenshotError> {
let Some(other) = s.chars().find(|c| !c.is_numeric()) else {
return Err("The format for range are start..end".to_string());
return Err(CodeScreenshotError::InvalidFormat("range", "start..end"));
};

let Some((start_str, end_str)) = s.split_once(&other.to_string()) else {
return Err("The format for range are start..end".to_string());
return Err(CodeScreenshotError::InvalidFormat("range", "start..end"));
};

let (start, end) = (
Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use thiserror::Error;


#[derive(Error, Debug)]
pub enum CodeScreenshotError {
#[error("The range is invalid")]
InvalidRange,
#[error("The expected format for {0} is {1}")]
InvalidFormat(&'static str, &'static str),
}
Empty file added src/highlight.rs
Empty file.
34 changes: 21 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use cli::get_args;
use syntect::easy::HighlightLines;
use syntect::highlighting::{ScopeSelectors, Style, Theme, ThemeItem, ThemeSet};
use syntect::highlighting::{Style, ThemeSet};
use syntect::parsing::SyntaxSet;
use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings};
use theme::load_themes;

mod cli;
mod error;
mod highlight;
mod theme;

fn main() {
let args = get_args();
load_themes();
// Load these once at the start of your program
let ps = SyntaxSet::load_defaults_newlines();
let themes = ThemeSet::default();

let syntax = ps.find_syntax_by_extension("rs").unwrap();
let mut h = HighlightLines::new(syntax, &theme);
let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
for line in LinesWithEndings::from(s) {
let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ps).unwrap();
ranges.iter().for_each(|(style, content)| {
println!("Style: {style:?}\nContent: '{content}'");
// let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
// print!("{}", escaped);
});
}
// let themes = ThemeSet::default();
//
// let syntax = ps.find_syntax_by_extension("rs").unwrap();
// let mut h = HighlightLines::new(syntax, &theme);
// let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
// for line in LinesWithEndings::from(s) {
// let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ps).unwrap();
// ranges.iter().for_each(|(style, content)| {
// println!("Style: {style:?}\nContent: '{content}'");
// // let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
// // print!("{}", escaped);
// });
// }
}
75 changes: 75 additions & 0 deletions src/theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::fs::{self, read_dir};
use std::path::PathBuf;

use lewp_css::domain::{CssRuleType, StyleRule};
use lewp_css::Stylesheet;
use syntect::highlighting::{Theme, ThemeSet, ThemeSettings};

fn load_from_css(file: &PathBuf) -> (String, Theme) {
let file_name = file.file_name().unwrap().to_str().unwrap().to_string();
let file_name = file_name.split(".").next().unwrap().to_string();
let content = fs::read_to_string(file).unwrap();
let style = Stylesheet::parse(&content).unwrap();
let style = style.rules.0.iter();
let theme = Theme {
name: Some(file_name.clone()),
author: None,
settings: ThemeSettings {
foreground: style.find_map(|s| {
if let CssRuleType::Style(StyleRule {selectors, property_declarations}) = s.rule_type() {
} else {
None
}
}),
background: todo!(),
caret: todo!(),
line_highlight: todo!(),
misspelling: todo!(),
minimap_border: todo!(),
accent: todo!(),
popup_css: todo!(),
phantom_css: todo!(),
bracket_contents_foreground: todo!(),
bracket_contents_options: todo!(),
brackets_foreground: todo!(),
brackets_background: todo!(),
brackets_options: todo!(),
tags_foreground: todo!(),
tags_options: todo!(),
highlight: todo!(),
find_highlight: todo!(),
find_highlight_foreground: todo!(),
gutter: todo!(),
gutter_foreground: todo!(),
selection: todo!(),
selection_foreground: todo!(),
selection_border: todo!(),
inactive_selection: todo!(),
inactive_selection_foreground: todo!(),
guide: todo!(),
active_guide: todo!(),
stack_guide: todo!(),
shadow: todo!(),
},
scopes: todo!(),
};

(file_name, theme)
}

pub fn load_themes() {
let mut themes = ThemeSet::load_defaults();
// themes.add_from_folder("./assets").unwrap_or_default();

let dir_content = read_dir("./assets").unwrap();

for file in dir_content {
let Ok(file) = file else {
continue;
};
let (theme_name, theme) = load_from_css(&file.path());
themes.themes.insert(theme_name, theme);
}

syntect::dumps::dump_to_file(&themes, "./assets/themes.themedump").unwrap();
}

0 comments on commit 1558121

Please sign in to comment.