From 39405ba1ec3e574c6166f8028e821f627385a680 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 13 Jan 2018 10:36:56 -0800 Subject: [PATCH] Refactor spans and markup We now use mostly indexed spans into a source string. Indexed Spans can still be resolved to a string slice when needed. --- examples/markup.rs | 7 +- examples/mutation.rs | 2 +- src/printer.rs | 5 +- src/theme/style.rs | 6 + src/utils/lines/spans/chunk.rs | 2 + src/utils/lines/spans/chunk_iterator.rs | 79 ++--- src/utils/lines/spans/lines_iterator.rs | 24 +- src/utils/lines/spans/mod.rs | 20 +- src/utils/lines/spans/row.rs | 21 +- src/utils/lines/spans/segment.rs | 15 + src/utils/lines/spans/tests.rs | 385 ++++-------------------- src/utils/markup/.mod.rs.rustfmt | 89 ++++++ src/utils/markup/markdown.rs | 119 +++----- src/utils/markup/mod.rs | 187 ++---------- src/utils/mod.rs | 1 + src/utils/span.rs | 219 ++++++++++++++ src/views/text_view.rs | 146 +++------ 17 files changed, 569 insertions(+), 758 deletions(-) create mode 100644 src/utils/markup/.mod.rs.rustfmt create mode 100644 src/utils/span.rs diff --git a/examples/markup.rs b/examples/markup.rs index b2aabda..289184c 100644 --- a/examples/markup.rs +++ b/examples/markup.rs @@ -2,7 +2,7 @@ extern crate cursive; use cursive::Cursive; #[cfg(feature = "markdown")] -use cursive::utils::markup::MarkdownText; +use cursive::utils::markup::markdown; use cursive::views::{Dialog, TextView}; // Make sure you compile with the `markdown` feature! @@ -13,14 +13,13 @@ fn main() { let mut siv = Cursive::new(); #[cfg(feature = "markdown")] - let text = MarkdownText("Isn't *that* **cool**?"); + let text = markdown::parse("Isn't *that* **cool**?"); #[cfg(not(feature = "markdown"))] let text = "Rebuild with --features markdown ;)"; siv.add_layer( - Dialog::around(TextView::styled(text).unwrap()) - .button("Hell yeah!", |s| s.quit()), + Dialog::around(TextView::new(text)).button("Hell yeah!", |s| s.quit()), ); siv.run(); diff --git a/examples/mutation.rs b/examples/mutation.rs index e4d170d..e7049b4 100644 --- a/examples/mutation.rs +++ b/examples/mutation.rs @@ -14,7 +14,7 @@ fn show_popup(siv: &mut Cursive) { // Look for a view tagged "text". // We _know_ it's there, so unwrap it. s.call_on_id("text", |view: &mut TextView| { - let content = reverse(&view.get_content()); + let content = reverse(view.get_content().source()); view.set_content(content); }); }) diff --git a/src/printer.rs b/src/printer.rs index 0fb2722..65c1647 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -136,10 +136,13 @@ impl<'a> Printer<'a> { /// Call the given closure with a styled printer, /// that will apply the given style on prints. - pub fn with_style(&self, style: Style, f: F) + pub fn with_style(&self, style: T, f: F) where F: FnOnce(&Printer), + T: Into