From 8695415fbfe927250f68e93793660e3c4e4a70b4 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Sun, 13 Dec 2020 12:23:50 +0900 Subject: wip: Move to new rendering structure. --- helix-term/src/prompt.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 6 deletions(-) (limited to 'helix-term/src/prompt.rs') diff --git a/helix-term/src/prompt.rs b/helix-term/src/prompt.rs index 689eac66..4747c9f5 100644 --- a/helix-term/src/prompt.rs +++ b/helix-term/src/prompt.rs @@ -1,9 +1,7 @@ -use crate::{ - application::Renderer, - compositor::{Component, Context, EventResult}, -}; +use crate::compositor::{Component, Context, EventResult}; use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; use helix_view::Editor; +use helix_view::Theme; use std::string::String; pub struct Prompt { @@ -85,6 +83,68 @@ impl Prompt { } } +use tui::{ + buffer::Buffer as Surface, + layout::Rect, + style::{Color, Modifier, Style}, +}; + +const BASE_WIDTH: u16 = 30; +use crate::application::text_color; + +impl Prompt { + pub fn render_prompt(&self, area: Rect, surface: &mut Surface, theme: &Theme) { + // completion + if !self.completion.is_empty() { + // TODO: find out better way of clearing individual lines of the screen + let mut row = 0; + let mut col = 0; + let max_col = area.width / BASE_WIDTH; + let col_height = ((self.completion.len() as u16 + max_col - 1) / max_col); + + for i in (3..col_height + 3) { + surface.set_string( + 0, + area.height - i as u16, + " ".repeat(area.width as usize), + text_color(), + ); + } + surface.set_style( + Rect::new(0, area.height - col_height - 2, area.width, col_height), + theme.get("ui.statusline"), + ); + for (i, command) in self.completion.iter().enumerate() { + let color = if self.completion_selection_index.is_some() + && i == self.completion_selection_index.unwrap() + { + Style::default().bg(Color::Rgb(104, 060, 232)) + } else { + text_color() + }; + surface.set_stringn( + 1 + col * BASE_WIDTH, + area.height - col_height - 2 + row, + &command, + BASE_WIDTH as usize - 1, + color, + ); + row += 1; + if row > col_height - 1 { + row = 0; + col += 1; + } + if col > max_col { + break; + } + } + } + // 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()); + } +} + impl Component for Prompt { fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult { let event = match event { @@ -137,7 +197,7 @@ impl Component for Prompt { EventResult::Consumed(None) } - fn render(&mut self, renderer: &mut Renderer, cx: &mut Context) { - renderer.render_prompt(self, &cx.editor.theme) + fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) { + self.render_prompt(area, surface, &cx.editor.theme) } } -- cgit v1.2.3-70-g09d2