From 7dc24a25ba148a9cd7c936e02cc03873ed6a467b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sun, 13 Dec 2020 13:35:30 +0900 Subject: [PATCH] Move ui modules under a ui:: namespace. --- helix-term/src/application.rs | 33 +++++-------------- helix-term/src/commands.rs | 2 +- helix-term/src/helix.log | 0 helix-term/src/main.rs | 3 +- .../src/{editor_view.rs => ui/editor.rs} | 22 ++++++------- helix-term/src/ui/mod.rs | 14 ++++++++ helix-term/src/{ => ui}/prompt.rs | 11 ++++--- 7 files changed, 42 insertions(+), 43 deletions(-) create mode 100644 helix-term/src/helix.log rename helix-term/src/{editor_view.rs => ui/editor.rs} (98%) create mode 100644 helix-term/src/ui/mod.rs rename helix-term/src/{ => ui}/prompt.rs (97%) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index c25871c7b..dc37612a2 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -2,9 +2,8 @@ use clap::ArgMatches as Args; use helix_view::{document::Mode, Document, Editor, Theme, View}; -use crate::compositor::{Component, Compositor, EventResult}; -use crate::editor_view::EditorView; -use crate::prompt::Prompt; +use crate::compositor::Compositor; +use crate::ui; use log::{debug, info}; @@ -19,18 +18,11 @@ use smol::prelude::*; use anyhow::Error; use crossterm::{ - cursor, - event::{read, Event, EventStream, KeyCode, KeyEvent}, - execute, queue, - terminal::{self, disable_raw_mode, enable_raw_mode}, + event::{Event, EventStream}, + execute, terminal, }; -use tui::{ - backend::CrosstermBackend, - buffer::Buffer as Surface, - layout::Rect, - style::{Color, Modifier, Style}, -}; +use tui::{backend::CrosstermBackend, layout::Rect}; type Terminal = crate::terminal::Terminal>; @@ -43,12 +35,6 @@ pub struct Application { language_server: helix_lsp::Client, } -// TODO: temp -#[inline(always)] -pub fn text_color() -> Style { - Style::default().fg(Color::Rgb(219, 191, 239)) // lilac -} - impl Application { pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result { let backend = CrosstermBackend::new(stdout()); @@ -61,14 +47,13 @@ impl Application { } let mut compositor = Compositor::new(); - compositor.push(Box::new(EditorView::new())); + compositor.push(Box::new(ui::EditorView::new())); let language_server = helix_lsp::Client::start(&executor, "rust-analyzer", &[]); let mut app = Self { editor, terminal, - // TODO; move to state compositor, executor, @@ -213,7 +198,7 @@ impl Application { } pub async fn run(&mut self) -> Result<(), Error> { - enable_raw_mode()?; + terminal::enable_raw_mode()?; let mut stdout = stdout(); @@ -223,7 +208,7 @@ impl Application { let hook = std::panic::take_hook(); std::panic::set_hook(Box::new(move |info| { execute!(std::io::stdout(), terminal::LeaveAlternateScreen); - disable_raw_mode(); + terminal::disable_raw_mode(); hook(info); })); @@ -234,7 +219,7 @@ impl Application { execute!(stdout, terminal::LeaveAlternateScreen)?; - disable_raw_mode()?; + terminal::disable_raw_mode()?; Ok(()) } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 04482ef74..b345d2e8b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -10,7 +10,7 @@ use helix_core::{ use once_cell::sync::Lazy; use crate::compositor::Compositor; -use crate::prompt::Prompt; +use crate::ui::Prompt; use helix_view::{ document::Mode, diff --git a/helix-term/src/helix.log b/helix-term/src/helix.log new file mode 100644 index 000000000..e69de29bb diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 63fbe52d7..f350b4c13 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -3,10 +3,9 @@ mod application; mod commands; mod compositor; -mod editor_view; mod keymap; -mod prompt; mod terminal; +mod ui; use application::Application; diff --git a/helix-term/src/editor_view.rs b/helix-term/src/ui/editor.rs similarity index 98% rename from helix-term/src/editor_view.rs rename to helix-term/src/ui/editor.rs index b778e79bd..ceb5a4429 100644 --- a/helix-term/src/editor_view.rs +++ b/helix-term/src/ui/editor.rs @@ -1,13 +1,16 @@ -use crate::application::text_color; use crate::commands; -use crate::compositor::{Component, Compositor, EventResult}; +use crate::compositor::{Component, Compositor, Context, EventResult}; use crate::keymap::{self, Keymaps}; +use crate::ui::text_color; + +use helix_core::{indent::TAB_WIDTH, syntax::HighlightEvent, Position, Range, State}; +use helix_view::{document::Mode, Document, Editor, Theme, View}; +use std::borrow::Cow; + use crossterm::{ cursor, event::{read, Event, EventStream, KeyCode, KeyEvent}, }; -use helix_view::{document::Mode, Document, Editor, Theme, View}; -use std::borrow::Cow; use tui::{ backend::CrosstermBackend, buffer::Buffer as Surface, @@ -15,8 +18,6 @@ use tui::{ style::{Color, Modifier, Style}, }; -use helix_core::{indent::TAB_WIDTH, syntax::HighlightEvent, Position, Range, State}; - pub struct EditorView { keymap: Keymaps, } @@ -212,6 +213,7 @@ impl EditorView { surface: &mut Surface, theme: &Theme, ) { + let text_color = text_color(); let mode = match view.doc.mode() { Mode::Insert => "INS", Mode::Normal => "NOR", @@ -222,23 +224,21 @@ impl EditorView { Rect::new(0, viewport.y, viewport.width, 1), theme.get("ui.statusline"), ); - surface.set_string(1, viewport.y, mode, text_color()); + surface.set_string(1, viewport.y, mode, text_color); if let Some(path) = view.doc.path() { - surface.set_string(6, viewport.y, path.to_string_lossy(), text_color()); + surface.set_string(6, viewport.y, path.to_string_lossy(), text_color); } surface.set_string( viewport.width - 10, viewport.y, format!("{}", view.doc.diagnostics.len()), - text_color(), + text_color, ); } } -use crate::compositor::Context; - impl Component for EditorView { fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { match event { diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs new file mode 100644 index 000000000..bc79e09cf --- /dev/null +++ b/helix-term/src/ui/mod.rs @@ -0,0 +1,14 @@ +mod editor; +mod prompt; + +pub use editor::EditorView; +pub use prompt::Prompt; + +pub use tui::layout::Rect; +pub use tui::style::{Color, Modifier, Style}; + +// TODO: temp +#[inline(always)] +pub fn text_color() -> Style { + Style::default().fg(Color::Rgb(219, 191, 239)) // lilac +} diff --git a/helix-term/src/prompt.rs b/helix-term/src/ui/prompt.rs similarity index 97% rename from helix-term/src/prompt.rs rename to helix-term/src/ui/prompt.rs index 7f473ebcc..071cac907 100644 --- a/helix-term/src/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -91,10 +91,11 @@ use tui::{ }; const BASE_WIDTH: u16 = 30; -use crate::application::text_color; +use crate::ui::text_color; impl Prompt { pub fn render_prompt(&self, area: Rect, surface: &mut Surface, theme: &Theme) { + let text_color = text_color(); // completion if !self.completion.is_empty() { // TODO: find out better way of clearing individual lines of the screen @@ -108,7 +109,7 @@ impl Prompt { 0, area.height - i as u16, " ".repeat(area.width as usize), - text_color(), + text_color, ); } surface.set_style( @@ -121,7 +122,7 @@ impl Prompt { { Style::default().bg(Color::Rgb(104, 060, 232)) } else { - text_color() + text_color }; surface.set_stringn( 1 + col * BASE_WIDTH, @@ -141,8 +142,8 @@ impl Prompt { } } // render buffer text - surface.set_string(1, area.height - 1, &self.prompt, text_color()); - surface.set_string(2, area.height - 1, &self.line, text_color()); + surface.set_string(1, area.height - 1, &self.prompt, text_color); + surface.set_string(2, area.height - 1, &self.line, text_color); } }