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 remaining = if lhs < self.remaining_width {
|
||||||
let sub = 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 {
|
} else {
|
||||||
self.remaining_width
|
self.remaining_width
|
||||||
};
|
};
|
||||||
|
|
52
src/state.rs
52
src/state.rs
|
@ -31,7 +31,7 @@ use crate::{
|
||||||
buffer::Buffer,
|
buffer::Buffer,
|
||||||
config::{Config, ThemeWatcher},
|
config::{Config, ThemeWatcher},
|
||||||
keybinds::{Key, Keybind},
|
keybinds::{Key, Keybind},
|
||||||
theme::StyleStore,
|
theme::{StyleStore, StyledString},
|
||||||
Cursor, Direction,
|
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 struct State {
|
||||||
pub styles: Arc<Mutex<StyleStore>>,
|
pub styles: Arc<Mutex<StyleStore>>,
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
@ -75,6 +114,7 @@ pub struct State {
|
||||||
pub redo_buffers: Vec<Buffer>,
|
pub redo_buffers: Vec<Buffer>,
|
||||||
pub cursor: Cursor,
|
pub cursor: Cursor,
|
||||||
pub file: Option<OsString>,
|
pub file: Option<OsString>,
|
||||||
|
pub window: Option<Window>,
|
||||||
pub mode: Mode,
|
pub mode: Mode,
|
||||||
pub submode: Option<Key>,
|
pub submode: Option<Key>,
|
||||||
pub last_saved: Rope,
|
pub last_saved: Rope,
|
||||||
|
@ -109,6 +149,7 @@ impl State {
|
||||||
quit: false,
|
quit: false,
|
||||||
error: None,
|
error: None,
|
||||||
theme_tx,
|
theme_tx,
|
||||||
|
window: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,6 +193,12 @@ impl State {
|
||||||
write!(out, "{:?}", submode)?;
|
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
|
// draw cursor
|
||||||
let cursor_pos = set_cursor_pos.unwrap_or_else(|| {
|
let cursor_pos = set_cursor_pos.unwrap_or_else(|| {
|
||||||
// calculate cursor position on buffer
|
// calculate cursor position on buffer
|
||||||
|
@ -181,6 +228,9 @@ impl State {
|
||||||
// reset the error from the last event
|
// reset the error from the last event
|
||||||
self.error = None;
|
self.error = None;
|
||||||
|
|
||||||
|
// close open window
|
||||||
|
self.window.take();
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
Event::Resize(cols, rows) => {
|
Event::Resize(cols, rows) => {
|
||||||
self.size = (cols as usize, rows as usize);
|
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;
|
let mut cursor = 0;
|
||||||
for (size, style) in self.styles.iter() {
|
for (size, style) in self.styles.iter() {
|
||||||
let end = cursor + size;
|
let end = cursor + size;
|
||||||
let range = cursor..end;
|
let range = cursor..end;
|
||||||
let sub = &self.content[range];
|
let sub = &self.content[range];
|
||||||
|
let mut style = style.clone();
|
||||||
|
style.apply(&base);
|
||||||
style.print_styled(out, sub)?;
|
style.print_styled(out, sub)?;
|
||||||
cursor = end;
|
cursor = end;
|
||||||
}
|
}
|
||||||
|
@ -153,6 +155,7 @@ impl StyledString {
|
||||||
&self,
|
&self,
|
||||||
out: &mut impl QueueableCommand,
|
out: &mut impl QueueableCommand,
|
||||||
clip: Range<usize>,
|
clip: Range<usize>,
|
||||||
|
base: Style,
|
||||||
) -> crossterm::Result<usize> {
|
) -> crossterm::Result<usize> {
|
||||||
let mut remaining = clip.end - clip.start;
|
let mut remaining = clip.end - clip.start;
|
||||||
let mut cursor = 0;
|
let mut cursor = 0;
|
||||||
|
@ -168,6 +171,8 @@ impl StyledString {
|
||||||
remaining -= clipped.end - clipped.start;
|
remaining -= clipped.end - clipped.start;
|
||||||
|
|
||||||
let sub = &self.content[clipped];
|
let sub = &self.content[clipped];
|
||||||
|
let mut style = style.clone();
|
||||||
|
style.apply(&base);
|
||||||
style.print_styled(out, sub)?;
|
style.print_styled(out, sub)?;
|
||||||
|
|
||||||
if cursor >= clip.end {
|
if cursor >= clip.end {
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
"ui.linenr" = "black"
|
"ui.linenr" = "black"
|
||||||
"ui.linenr.selected" = "white"
|
"ui.linenr.selected" = "white"
|
||||||
|
|
||||||
|
"ui.window" = { bg = "magenta" }
|
||||||
|
|
||||||
"error" = "red"
|
"error" = "red"
|
||||||
|
|
||||||
"comment" = { fg = "black", modifiers = ["italics"] }
|
"comment" = { fg = "black", modifiers = ["italics"] }
|
||||||
|
|
Loading…
Reference in New Issue