Improve syntax example, remove markdown example

This commit is contained in:
Jeremy Soller 2022-10-26 17:23:40 -06:00
parent 6bc6ceac12
commit 9bbc0ba6c5
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
4 changed files with 33 additions and 162 deletions

View file

@ -1,15 +0,0 @@
[package]
name = "markdown"
version = "0.1.0"
authors = ["Jeremy Soller <jeremy@system76.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
publish = false
[dependencies]
cosmic-text = { path = "../../" }
env_logger = "0.9"
fontdb = "0.9"
log = "0.4"
minimad = "0.9"
orbclient = "0.3.35"

View file

@ -1,134 +0,0 @@
// 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 md = 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 text = minimad::parse_text(&md);
let mut window = Window::new_flags(
-1,
-1,
1024,
768,
&format!("COSMIC TEXT - {}", font_system.locale),
&[WindowFlag::Resizable],
)
.unwrap();
let attrs = cosmic_text::Attrs::new();
let mut buffer = TextBuffer::new(
&font_system,
attrs,
TextMetrics::new(32, 44)
);
buffer.set_size(
window.width() as i32,
window.height() as i32
);
for (line_i, md_line) in text.lines.iter().enumerate() {
while line_i >= buffer.lines.len() {
buffer.lines.push(TextBufferLine::new(String::new(), attrs));
}
let composite = match md_line {
minimad::Line::Normal(composite) => composite,
_ => {
//TODO: other md_line types
continue
}
};
let line = &mut buffer.lines[line_i];
//TODO: use composite style
for compound in composite.compounds.iter() {
let start = line.text.len();
line.text.push_str(compound.src);
let end = line.text.len();
line.attrs_list.add_span(
start,
end,
attrs
.monospaced(compound.code)
.family(if compound.code { Family::Monospace } else { Family::SansSerif })
.style(if compound.italic { Style::Italic } else { Style::Normal })
.weight(if compound.bold { Weight::BOLD } else { Weight::NORMAL })
);
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));
}
}

View file

@ -13,7 +13,7 @@ fn main() {
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()
String::new()
};
let mut window = Window::new_flags(
@ -51,7 +51,10 @@ fn main() {
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 (name, _) in ts.themes.iter() {
println!("{}", name);
}
let mut h = HighlightLines::new(syntax, &ts.themes["base16-eighties.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));
@ -104,7 +107,7 @@ fn main() {
let mut mouse_y = -1;
let mut mouse_left = false;
loop {
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let bg_color = orbclient::Color::rgb(0x2d, 0x2d, 0x2d);
let font_color = orbclient::Color::rgb(0xFF, 0xFF, 0xFF);
if buffer.cursor_moved {
@ -133,22 +136,40 @@ fn main() {
for event in window.events() {
match event.to_option() {
EventOption::Mouse(mouse) => {
mouse_x = mouse.x;
mouse_y = mouse.y;
EventOption::Key(event) => match event.scancode {
orbclient::K_LEFT if event.pressed => buffer.action(TextAction::Left),
orbclient::K_RIGHT if event.pressed => buffer.action(TextAction::Right),
orbclient::K_UP if event.pressed => buffer.action(TextAction::Up),
orbclient::K_DOWN if event.pressed => buffer.action(TextAction::Down),
orbclient::K_HOME if event.pressed => buffer.action(TextAction::Home),
orbclient::K_END if event.pressed => buffer.action(TextAction::End),
orbclient::K_PGUP if event.pressed => buffer.action(TextAction::PageUp),
orbclient::K_PGDN if event.pressed => buffer.action(TextAction::PageDown),
_ => (),
},
EventOption::Mouse(event) => {
mouse_x = event.x;
mouse_y = event.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::Button(event) => {
if event.left != mouse_left {
mouse_left = event.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::Resize(event) => {
buffer.set_size(event.width as i32, event.height as i32);
},
EventOption::Scroll(event) => {
buffer.action(TextAction::Scroll {
lines: -event.y * 3,
});
}
EventOption::Quit(_) => process::exit(0),
_ => (),
}

View file

@ -1 +0,0 @@
RUST_LOG=cosmic_text=debug cargo run --release --package markdown -- "$@"