Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed Oct 8, 2023
1 parent 6331706 commit 01addfc
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 71 deletions.
5 changes: 5 additions & 0 deletions examples/readline/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ fn main() -> Result<()> {
)
.build()?,
TextEditorBuilder::new()
.style(
ContentStyleBuilder::new()
.foreground_color(Color::DarkYellow)
.build(),
)
.label_style(
ContentStyleBuilder::new()
.foreground_color(Color::DarkGreen)
Expand Down
8 changes: 8 additions & 0 deletions src/editor/builder/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ use crate::{

pub struct TextEditorBuilder {
label: String,
style: ContentStyle,
label_style: ContentStyle,
}

impl TextEditorBuilder {
pub fn new() -> Self {

Check warning on line 15 in src/editor/builder/text_editor.rs

View workflow job for this annotation

GitHub Actions / test

you should consider adding a `Default` implementation for `TextEditorBuilder`
Self {
label: String::from("❯❯ "),
style: ContentStyle::new(),
label_style: ContentStyle::new(),
}
}

pub fn style(mut self, style: ContentStyle) -> Self {
self.style = style;
self
}

pub fn label<T: AsRef<str>>(mut self, label: T) -> Self {
self.label = label.as_ref().to_string();
self
Expand All @@ -31,6 +38,7 @@ impl TextEditorBuilder {
pub fn build(self) -> Result<Box<TextEditor>> {
Ok(Box::new(TextEditor {
textbuffer: TextBuffer::new(),
style: self.style,
label: Graphemes::new_with_style(self.label, self.label_style),
}))
}
Expand Down
12 changes: 8 additions & 4 deletions src/editor/text_editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
grapheme::{matrixify, Grapheme, Graphemes},
crossterm::{
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers},
style::ContentStyle,
},
grapheme::{matrixify, Graphemes},
pane::Pane,
text_buffer::TextBuffer,
};
Expand All @@ -10,14 +13,15 @@ use super::Editor;
pub struct TextEditor {
pub textbuffer: TextBuffer,

pub style: ContentStyle,
pub label: Graphemes,
}

impl Editor for TextEditor {
fn gen_pane(&self, width: u16) -> Pane {
let mut buf = Graphemes::default();
buf.append(&mut self.label.clone());
buf.append(&mut self.textbuffer.buf.clone());
buf.append(&mut self.textbuffer.graphemes(self.style));

Pane::new(
matrixify(width as usize, buf),
Expand Down Expand Up @@ -73,7 +77,7 @@ impl Editor for TextEditor {
modifiers: KeyModifiers::SHIFT,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => self.textbuffer.insert(Grapheme::new(*ch)),
}) => self.textbuffer.insert(*ch),

_ => [TextBuffer::new(), TextBuffer::new()],
};
Expand Down
Loading

0 comments on commit 01addfc

Please sign in to comment.