From ed510cffbc77815e04bc7d48eaa21959247627a5 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sat, 7 Mar 2015 18:30:18 -0800 Subject: [PATCH] WIP more line wrapping options. This commit just adds code to allow non-wrapping behavior. --- src/term_ui/formatter.rs | 80 +++++++++++++++++++++++++++++++--------- src/term_ui/mod.rs | 6 +-- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/term_ui/formatter.rs b/src/term_ui/formatter.rs index c14a7e1..0d85da5 100644 --- a/src/term_ui/formatter.rs +++ b/src/term_ui/formatter.rs @@ -3,13 +3,20 @@ use std::cmp::max; use string_utils::{is_line_ending}; use formatter::{LineFormatter, RoundingBehavior}; +pub enum WrapType { + NoWrap, + CharWrap(usize), + WordWrap(usize), +} + //=================================================================== // LineFormatter implementation for terminals/consoles. //=================================================================== pub struct ConsoleLineFormatter { pub tab_width: u8, - pub wrap_width: usize, + pub wrap_type: WrapType, + pub maintain_indent: bool, } @@ -17,7 +24,23 @@ impl ConsoleLineFormatter { pub fn new(tab_width: u8) -> ConsoleLineFormatter { ConsoleLineFormatter { tab_width: tab_width, - wrap_width: 40, + wrap_type: WrapType::CharWrap(40), + maintain_indent: false, + } + } + + pub fn set_wrap_width(&mut self, width: usize) { + match self.wrap_type { + WrapType::NoWrap => { + }, + + WrapType::CharWrap(ref mut w) => { + *w = width; + }, + + WrapType::WordWrap(ref mut w) => { + *w = width; + }, } } @@ -118,22 +141,45 @@ where T: Iterator type Item = (&'a str, (usize, usize), usize); fn next(&mut self) -> Option<(&'a str, (usize, usize), usize)> { - if let Some(g) = self.grapheme_iter.next() { - let width = grapheme_vis_width_at_vis_pos(g, self.pos.1, self.f.tab_width as usize); + match self.f.wrap_type { + WrapType::NoWrap => { + if let Some(g) = self.grapheme_iter.next() { + let width = grapheme_vis_width_at_vis_pos(g, self.pos.1, self.f.tab_width as usize); + + let pos = self.pos; + self.pos = (self.pos.0, self.pos.1 + width); + return Some((g, pos, width)); + } + else { + return None; + } + }, + + WrapType::CharWrap(wrap_width) => { + if let Some(g) = self.grapheme_iter.next() { + let width = grapheme_vis_width_at_vis_pos(g, self.pos.1, self.f.tab_width as usize); + + if (self.pos.1 + width) > wrap_width { + let pos = (self.pos.0 + self.f.single_line_height(), 0); + self.pos = (self.pos.0 + self.f.single_line_height(), width); + return Some((g, pos, width)); + } + else { + let pos = self.pos; + self.pos = (self.pos.0, self.pos.1 + width); + return Some((g, pos, width)); + } + } + else { + return None; + } + }, + + WrapType::WordWrap(_) => { + // TODO + return None; + }, - if (self.pos.1 + width) > self.f.wrap_width { - let pos = (self.pos.0 + self.f.single_line_height(), 0); - self.pos = (self.pos.0 + self.f.single_line_height(), width); - return Some((g, pos, width)); - } - else { - let pos = self.pos; - self.pos = (self.pos.0, self.pos.1 + width); - return Some((g, pos, width)); - } - } - else { - return None; } } } diff --git a/src/term_ui/mod.rs b/src/term_ui/mod.rs index 01a2c92..11d87bc 100644 --- a/src/term_ui/mod.rs +++ b/src/term_ui/mod.rs @@ -93,12 +93,12 @@ impl TermUI { let mut resize: Option<(usize, usize)> = None; self.editor.update_dim(self.height-1, self.width); - self.editor.formatter.wrap_width = self.width as usize; + self.editor.formatter.set_wrap_width(self.width as usize); loop { // Draw the editor to screen self.editor.update_view_dim(); - self.editor.formatter.wrap_width = self.editor.view_dim.1; + self.editor.formatter.set_wrap_width(self.editor.view_dim.1); self.rb.clear(); self.draw_editor(&self.editor, (0, 0), (self.height-1, self.width-1)); self.rb.present(); @@ -230,7 +230,7 @@ impl TermUI { loop { // Draw the editor to screen self.editor.update_view_dim(); - self.editor.formatter.wrap_width = self.editor.view_dim.1; + self.editor.formatter.set_wrap_width(self.editor.view_dim.1); self.rb.clear(); self.draw_editor(&self.editor, (0, 0), (self.height-1, self.width-1)); for i in 0..self.width {