Window drawing
This commit is contained in:
parent
cce42ef240
commit
4dd6307c12
|
@ -71,7 +71,7 @@ impl<'a> LineBuf<'a> {
|
|||
|
||||
let remaining = if lhs < self.remaining_width {
|
||||
let sub = lhs..self.remaining_width;
|
||||
self.styled.print_sub(&mut self.buf, sub)?
|
||||
self.styled.print_sub(&mut self.buf, sub, line_style)?
|
||||
} else {
|
||||
self.remaining_width
|
||||
};
|
||||
|
|
52
src/state.rs
52
src/state.rs
|
@ -31,7 +31,7 @@ use crate::{
|
|||
buffer::Buffer,
|
||||
config::{Config, ThemeWatcher},
|
||||
keybinds::{Key, Keybind},
|
||||
theme::StyleStore,
|
||||
theme::{StyleStore, StyledString},
|
||||
Cursor, Direction,
|
||||
};
|
||||
|
||||
|
@ -67,6 +67,45 @@ impl Mode {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Rect {
|
||||
pub position: (usize, usize),
|
||||
pub size: (usize, usize),
|
||||
}
|
||||
|
||||
pub struct Window {
|
||||
pub rect: Rect,
|
||||
pub contents: Vec<StyledString>,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
pub fn draw(
|
||||
&self,
|
||||
out: &mut impl QueueableCommand,
|
||||
styles: &mut StyleStore,
|
||||
) -> crossterm::Result<()> {
|
||||
let x = self.rect.position.0 as u16;
|
||||
let base = *styles.get_scope("ui.window");
|
||||
let width = self.rect.size.0;
|
||||
let clip = 0..width;
|
||||
let padding: String = std::iter::repeat(' ').take(width).collect();
|
||||
for row in 0..self.rect.size.1 {
|
||||
let y = self.rect.position.1 as u16 + row as u16;
|
||||
out.queue(cursor::MoveTo(x, y))?;
|
||||
|
||||
let remaining = if let Some(line) = self.contents.get(row) {
|
||||
line.print_sub(out, clip.clone(), base)?
|
||||
} else {
|
||||
width
|
||||
};
|
||||
|
||||
let padding = &padding[0..remaining];
|
||||
base.print_styled(out, padding)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub styles: Arc<Mutex<StyleStore>>,
|
||||
pub config: Config,
|
||||
|
@ -75,6 +114,7 @@ pub struct State {
|
|||
pub redo_buffers: Vec<Buffer>,
|
||||
pub cursor: Cursor,
|
||||
pub file: Option<OsString>,
|
||||
pub window: Option<Window>,
|
||||
pub mode: Mode,
|
||||
pub submode: Option<Key>,
|
||||
pub last_saved: Rope,
|
||||
|
@ -109,6 +149,7 @@ impl State {
|
|||
quit: false,
|
||||
error: None,
|
||||
theme_tx,
|
||||
window: None,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -152,6 +193,12 @@ impl State {
|
|||
write!(out, "{:?}", submode)?;
|
||||
}
|
||||
|
||||
// draw open window
|
||||
if let Some(window) = self.window.as_ref() {
|
||||
let mut styles = self.styles.lock();
|
||||
window.draw(out, &mut styles)?;
|
||||
}
|
||||
|
||||
// draw cursor
|
||||
let cursor_pos = set_cursor_pos.unwrap_or_else(|| {
|
||||
// calculate cursor position on buffer
|
||||
|
@ -181,6 +228,9 @@ impl State {
|
|||
// reset the error from the last event
|
||||
self.error = None;
|
||||
|
||||
// close open window
|
||||
self.window.take();
|
||||
|
||||
match event {
|
||||
Event::Resize(cols, rows) => {
|
||||
self.size = (cols as usize, rows as usize);
|
||||
|
|
|
@ -136,12 +136,14 @@ impl StyledString {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn print(&self, out: &mut impl QueueableCommand) -> crossterm::Result<()> {
|
||||
pub fn print(&self, out: &mut impl QueueableCommand, base: Style) -> crossterm::Result<()> {
|
||||
let mut cursor = 0;
|
||||
for (size, style) in self.styles.iter() {
|
||||
let end = cursor + size;
|
||||
let range = cursor..end;
|
||||
let sub = &self.content[range];
|
||||
let mut style = style.clone();
|
||||
style.apply(&base);
|
||||
style.print_styled(out, sub)?;
|
||||
cursor = end;
|
||||
}
|
||||
|
@ -153,6 +155,7 @@ impl StyledString {
|
|||
&self,
|
||||
out: &mut impl QueueableCommand,
|
||||
clip: Range<usize>,
|
||||
base: Style,
|
||||
) -> crossterm::Result<usize> {
|
||||
let mut remaining = clip.end - clip.start;
|
||||
let mut cursor = 0;
|
||||
|
@ -168,6 +171,8 @@ impl StyledString {
|
|||
remaining -= clipped.end - clipped.start;
|
||||
|
||||
let sub = &self.content[clipped];
|
||||
let mut style = style.clone();
|
||||
style.apply(&base);
|
||||
style.print_styled(out, sub)?;
|
||||
|
||||
if cursor >= clip.end {
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
"ui.linenr" = "black"
|
||||
"ui.linenr.selected" = "white"
|
||||
|
||||
"ui.window" = { bg = "magenta" }
|
||||
|
||||
"error" = "red"
|
||||
|
||||
"comment" = { fg = "black", modifiers = ["italics"] }
|
||||
|
|
Loading…
Reference in New Issue