From 219c560c7c0ad9e3960298ec125d4e64637fe84b Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Fri, 2 Aug 2024 20:41:29 +0200 Subject: add editor theme and state lua bindings --- src/widgets/luaeditor/mod.rs | 161 ++++++++++++++++++++++--------------------- src/widgets/statusbar.rs | 13 ++-- 2 files changed, 91 insertions(+), 83 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs index c777190..620de4c 100644 --- a/src/widgets/luaeditor/mod.rs +++ b/src/widgets/luaeditor/mod.rs @@ -7,8 +7,8 @@ use ratatui::{ use tree_sitter_highlight::HighlightConfiguration; use crate::{ - config::GlobalConfig, - state::{editor::EditorState, window::Window, GlobalState}, + config::{theme::editor::bar::EditorBarTheme, GlobalConfig}, + state::{editor::{bar::EditorBarState, EditorState}, window::Window, GlobalState}, tuicursor::TuiCursor, }; use crate::{cursor::CursorMove, lua}; @@ -108,90 +108,95 @@ impl Widget for &mut LuaEditor { where Self: Sized, { - let state = GlobalState::instance(); - let buffer = &state.editor.buffer; - - let config = GlobalConfig::instance(); - let theme = &config.theme.editor; - - self.bar.render(area, buf); - let inner_area = self.bar.area(area); - - let text = buffer.as_string(); - let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config); - - let nr_width = (buffer.lines().len().to_string().len() + 1).max(4) as u16; - let mut text_area = inner_area; - text_area.x += nr_width; - - let mut span_area = text_area; - let mut current_line = 0; - - theme - .background - .get((), &lua::get()) - .unwrap_or_default() - .apply(Paragraph::default()) - .render(area, buf); - - theme - .cursor_line - .get((), &lua::get()) - .unwrap_or_default() - .apply(Paragraph::default()) - .render( - Rect::new( - text_area.x, - text_area.y + buffer.cursor().y() as u16, - text_area.width, - 1, - ), - buf, - ); - - for (hl, group) in highlights.iter() { - if *group == "\n" { - if current_line >= self.scroll { - span_area.y += 1; - span_area.x = text_area.x; + { + let state = GlobalState::instance(); + let buffer = &state.editor.buffer; + + let config = GlobalConfig::instance(); + let theme = &config.theme.editor; + + let inner_area = self.bar.area(area); + + let text = buffer.as_string(); + let highlights = treesitter::highlighter_split(text.as_bytes(), &self.highlight_config); + + let nr_width = (buffer.lines().len().to_string().len() + 1).max(4) as u16; + let mut text_area = inner_area; + text_area.x += nr_width; + + let mut span_area = text_area; + let mut current_line = 0; + + theme + .background + .get((), &lua::get()) + .unwrap_or_default() + .apply(Paragraph::default()) + .render(area, buf); + + theme + .cursor_line + .get((), &lua::get()) + .unwrap_or_default() + .apply(Paragraph::default()) + .render( + Rect::new( + text_area.x, + text_area.y + buffer.cursor().y() as u16, + text_area.width, + 1, + ), + buf, + ); + + for (hl, group) in highlights.iter() { + if *group == "\n" { + if current_line >= self.scroll { + span_area.y += 1; + span_area.x = text_area.x; + } + + current_line += 1; + } else if current_line >= self.scroll { + let group = group.replace('\t', " "); + let span = group.to_span(); + + if inner_area.contains(span_area.into()) { + theme.highlight.highlight(*hl, span).render(span_area, buf); + } + + span_area.x += group.len() as u16; } + } - current_line += 1; - } else if current_line >= self.scroll { - let group = group.replace('\t', " "); - let span = group.to_span(); + for i in self.scroll..buffer.lines().len() { + let mut nr_area = span_area; + nr_area.x = inner_area.x; + nr_area.width = nr_width; + nr_area.y = inner_area.y + (i - self.scroll) as u16; + nr_area.height = 1; - if inner_area.contains(span_area.into()) { - theme.highlight.highlight(*hl, span).render(span_area, buf); + if !inner_area.contains(nr_area.into()) { + break; } - span_area.x += group.len() as u16; + if buffer.cursor().y() == i { + theme + .active_line_number + .get((), &lua::get()) + .unwrap_or_default() + } else { + theme.line_number.get((), &lua::get()).unwrap_or_default() + } + .apply(((i + 1).to_string() + " ").to_line()) + .right_aligned() + .render(nr_area, buf); } } - for i in self.scroll..buffer.lines().len() { - let mut nr_area = span_area; - nr_area.x = inner_area.x; - nr_area.width = nr_width; - nr_area.y = inner_area.y + (i - self.scroll) as u16; - nr_area.height = 1; - - if !inner_area.contains(nr_area.into()) { - break; - } - - if buffer.cursor().y() == i { - theme - .active_line_number - .get((), &lua::get()) - .unwrap_or_default() - } else { - theme.line_number.get((), &lua::get()).unwrap_or_default() - } - .apply(((i + 1).to_string() + " ").to_line()) - .right_aligned() - .render(nr_area, buf); - } + EditorBarState::apply(&mut self.bar, (), &lua::get()); + EditorBarTheme::apply(&mut self.bar, (), &lua::get()); + self.bar.render(area, buf); } } diff --git a/src/widgets/statusbar.rs b/src/widgets/statusbar.rs index 61ce1a3..9375d95 100644 --- a/src/widgets/statusbar.rs +++ b/src/widgets/statusbar.rs @@ -1,4 +1,3 @@ - use ratatui::{ crossterm::event::{KeyCode, KeyEvent}, layout::{Alignment, Rect}, @@ -23,12 +22,12 @@ pub struct StatusBar { impl StatusBar { pub fn new() -> Self { Self { - left: String::new(), + left: "".to_string(), left_style: Style::default(), - middle: String::new(), + middle: " ".to_string(), middle_style: Style::default(), middle_alignment: Alignment::Center, - right: String::new(), + right: "".to_string(), right_style: Style::default(), input: None, cursor: 0, @@ -46,7 +45,11 @@ impl StatusBar { where S: AsRef, { - self.middle = text.as_ref().to_string(); + if text.as_ref().is_empty() { + self.middle = " ".to_string(); + } else { + self.middle = text.as_ref().to_string(); + } } pub fn set_right(&mut self, text: S) -- cgit v1.2.3-70-g09d2