From 6bc6ceac12836114207f9a61aaa854f22b4eb609 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 26 Oct 2022 16:34:53 -0600 Subject: [PATCH] Syntax highlighting example --- examples/syntax/Cargo.toml | 15 ++++ examples/syntax/src/main.rs | 159 ++++++++++++++++++++++++++++++++++++ syntax.sh | 1 + 3 files changed, 175 insertions(+) create mode 100644 examples/syntax/Cargo.toml create mode 100644 examples/syntax/src/main.rs create mode 100755 syntax.sh diff --git a/examples/syntax/Cargo.toml b/examples/syntax/Cargo.toml new file mode 100644 index 0000000..259291f --- /dev/null +++ b/examples/syntax/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "syntax" +version = "0.1.0" +authors = ["Jeremy Soller "] +edition = "2021" +license = "MIT OR Apache-2.0" +publish = false + +[dependencies] +cosmic-text = { path = "../../" } +env_logger = "0.9" +fontdb = "0.9" +log = "0.4" +orbclient = "0.3.35" +syntect = "5.0" diff --git a/examples/syntax/src/main.rs b/examples/syntax/src/main.rs new file mode 100644 index 0000000..82e930b --- /dev/null +++ b/examples/syntax/src/main.rs @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use cosmic_text::{Attrs, Color, Family, FontSystem, Style, SwashCache, + TextAction, TextBuffer, TextBufferLine, TextMetrics, Weight}; +use orbclient::{EventOption, Renderer, Window, WindowFlag}; +use std::{env, fs, process, thread, time::{Duration, Instant}}; + +fn main() { + env_logger::init(); + + let font_system = FontSystem::new(); + + let text = if let Some(arg) = env::args().nth(1) { + fs::read_to_string(&arg).expect("failed to open file") + } else { + "Markdown can have **bold**, *italic*, and `code` styles".to_string() + }; + + let mut window = Window::new_flags( + -1, + -1, + 1024, + 768, + &format!("COSMIC TEXT - {}", font_system.locale), + &[WindowFlag::Resizable], + ) + .unwrap(); + + let attrs = Attrs::new() + .monospaced(true) + .family(Family::Monospace); + let mut buffer = TextBuffer::new( + &font_system, + attrs, + TextMetrics::new(14, 20) + ); + + buffer.set_size( + window.width() as i32, + window.height() as i32 + ); + + { + use syntect::easy::HighlightLines; + use syntect::parsing::SyntaxSet; + use syntect::highlighting::{ThemeSet, FontStyle, Style as SyntectStyle}; + use syntect::util::LinesWithEndings; + + // Load these once at the start of your program + let ps = SyntaxSet::load_defaults_newlines(); + let ts = ThemeSet::load_defaults(); + + let syntax = ps.find_syntax_by_extension("rs").unwrap(); + let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]); + for (line_i, highlight_line) in LinesWithEndings::from(&text).enumerate() { // LinesWithEndings enables use of newlines mode + while line_i >= buffer.lines.len() { + buffer.lines.push(TextBufferLine::new(String::new(), attrs)); + } + + let ranges: Vec<(SyntectStyle, &str)> = h.highlight_line(highlight_line, &ps).unwrap(); + + let line = &mut buffer.lines[line_i]; + for (style, string) in ranges.iter() { + let string_trim = match string.lines().next() { + Some(some) => some, + None => continue, + }; + + let start = line.text.len(); + line.text.push_str(string_trim); + let end = line.text.len(); + line.attrs_list.add_span( + start, + end, + attrs + .color(Color::rgba( + style.foreground.r, + style.foreground.g, + style.foreground.b, + style.foreground.a, + )) + //TODO: background + .style(if style.font_style.contains(FontStyle::ITALIC) { + Style::Italic + } else { + Style::Normal + }) + .weight(if style.font_style.contains(FontStyle::BOLD) { + Weight::BOLD + } else { + Weight::NORMAL + }) + //TODO: underline + ); + line.reset(); + } + } + } + + let mut swash_cache = SwashCache::new(&font_system); + + //TODO: make window not async? + let mut mouse_x = -1; + let mut mouse_y = -1; + let mut mouse_left = false; + loop { + let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34); + let font_color = orbclient::Color::rgb(0xFF, 0xFF, 0xFF); + + if buffer.cursor_moved { + buffer.shape_until_cursor(); + buffer.cursor_moved = false; + } else { + buffer.shape_until_scroll(); + } + + if buffer.redraw { + let instant = Instant::now(); + + window.set(bg_color); + + buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| { + window.rect(x, y, w, h, orbclient::Color { data: color }); + }); + + window.sync(); + + buffer.redraw = false; + + let duration = instant.elapsed(); + log::debug!("redraw: {:?}", duration); + } + + for event in window.events() { + match event.to_option() { + EventOption::Mouse(mouse) => { + mouse_x = mouse.x; + mouse_y = mouse.y; + if mouse_left { + buffer.action(TextAction::Drag { x: mouse_x, y: mouse_y }); + } + }, + EventOption::Button(button) => { + mouse_left = button.left; + if mouse_left { + buffer.action(TextAction::Click { x: mouse_x, y: mouse_y }); + } + }, + EventOption::Resize(resize) => { + buffer.set_size(resize.width as i32, resize.height as i32); + }, + EventOption::Quit(_) => process::exit(0), + _ => (), + } + } + + thread::sleep(Duration::from_millis(1)); + } +} diff --git a/syntax.sh b/syntax.sh new file mode 100755 index 0000000..fe8621e --- /dev/null +++ b/syntax.sh @@ -0,0 +1 @@ +RUST_LOG=cosmic_text=debug cargo run --release --package syntax -- "$@"