Skip to content

Commit

Permalink
plugins can now render on help message and greeting message
Browse files Browse the repository at this point in the history
  • Loading branch information
curlpipe committed Sep 26, 2024
1 parent ed75651 commit 00ae87d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
4 changes: 2 additions & 2 deletions plugins/pomodoro.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Define our pomodoro state
pomodoro = {
-- Configuration values
work_time = 1,
rest_time = 1,
work_time = 25,
rest_time = 5,
-- Plug-in state
current = "none",
started = os.date("*t"),
Expand Down
50 changes: 41 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::VERSION;
use crate::editor::Editor;
use crate::error::{OxError, Result};
use crate::ui::{Feedback, Terminal};
use crate::ui::Feedback;
use crossterm::{
event::{KeyCode as KCode, KeyModifiers as KMod, MediaKeyCode, ModifierKeyCode},
style::{Color, SetForegroundColor as Fg},
Expand All @@ -11,7 +11,6 @@ use kaolinite::utils::{filetype, get_absolute_path, get_file_ext, get_file_name,
use kaolinite::{Document, Loc};
use mlua::prelude::*;
use std::collections::HashMap;
use std::io::Write;
use std::{cell::RefCell, rc::Rc};
use synoptic::{from_extension, Highlighter};

Expand Down Expand Up @@ -385,13 +384,32 @@ impl Default for GreetingMessage {

impl GreetingMessage {
/// Take the configuration information and render the greeting message
pub fn render(&self, colors: &Colors) -> Result<String> {
pub fn render(&self, lua: &Lua, colors: &Colors) -> Result<String> {
let highlight = Fg(colors.highlight.to_color()?).to_string();
let editor_fg = Fg(colors.editor_fg.to_color()?).to_string();
let mut result = self.format.clone();
result = result.replace("{version}", &VERSION).to_string();
result = result.replace("{highlight_start}", &highlight).to_string();
result = result.replace("{highlight_end}", &editor_fg).to_string();
// Find functions to call and substitute in
let mut searcher = Searcher::new(r"\{[A-Za-z_][A-Za-z0-9_]*\}");
while let Some(m) = searcher.lfind(&result) {
let name = m
.text
.chars()
.skip(1)
.take(m.text.chars().count().saturating_sub(2))
.collect::<String>();
if let Ok(func) = lua.globals().get::<String, LuaFunction>(name) {
if let Ok(r) = func.call::<(), LuaString>(()) {
result = result.replace(&m.text, r.to_str().unwrap_or(""));
} else {
break;
}
} else {
break;
}
}
Ok(result)
}
}
Expand Down Expand Up @@ -429,19 +447,33 @@ impl Default for HelpMessage {

impl HelpMessage {
/// Take the configuration information and render the help message
pub fn render(&self, term: &mut Terminal, w: usize, h: usize, colors: &Colors) -> Result<()> {
pub fn render(&self, lua: &Lua, colors: &Colors) -> Result<Vec<String>> {
let highlight = Fg(colors.highlight.to_color()?).to_string();
let editor_fg = Fg(colors.editor_fg.to_color()?).to_string();
let mut result = self.format.clone();
result = result.replace("{version}", &VERSION).to_string();
result = result.replace("{highlight_start}", &highlight).to_string();
result = result.replace("{highlight_end}", &editor_fg).to_string();
let message: Vec<&str> = result.split('\n').collect();
for (c, line) in message.iter().enumerate().take(h.saturating_sub(h / 4)) {
term.goto(w.saturating_sub(30), h / 4 + c + 1)?;
write!(term.stdout, "{line}")?;
// Find functions to call and substitute in
let mut searcher = Searcher::new(r"\{[A-Za-z_][A-Za-z0-9_]*\}");
while let Some(m) = searcher.lfind(&result) {
let name = m
.text
.chars()
.skip(1)
.take(m.text.chars().count().saturating_sub(2))
.collect::<String>();
if let Ok(func) = lua.globals().get::<String, LuaFunction>(name) {
if let Ok(r) = func.call::<(), LuaString>(()) {
result = result.replace(&m.text, r.to_str().unwrap_or(""));
} else {
break;
}
} else {
break;
}
}
Ok(())
Ok(result.split('\n').map(|l| l.to_string()).collect())
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ impl Editor {
self.render_status_line(&lua, w, h)?;
// Render greeting or help message if applicable
if self.greet {
self.render_greeting(w, h)?;
self.render_greeting(lua, w, h)?;
} else if self.config.help_message.borrow().enabled {
self.render_help_message(w, h)?;
self.render_help_message(lua, w, h)?;
}
// Render feedback line
self.render_feedback_line(w, h)?;
Expand Down Expand Up @@ -498,18 +498,20 @@ impl Editor {
}

/// Render the greeting message
fn render_help_message(&mut self, w: usize, h: usize) -> Result<()> {
fn render_help_message(&mut self, lua: &Lua, w: usize, h: usize) -> Result<()> {
let colors = self.config.colors.borrow();
self.config
.help_message
.borrow()
.render(&mut self.terminal, w, h, &colors)
let message = self.config.help_message.borrow().render(lua, &colors)?;
for (c, line) in message.iter().enumerate().take(h.saturating_sub(h / 4)) {
self.terminal.goto(w.saturating_sub(30), h / 4 + c + 1)?;
write!(self.terminal.stdout, "{line}")?;
}
Ok(())
}

/// Render the help message
fn render_greeting(&mut self, w: usize, h: usize) -> Result<()> {
fn render_greeting(&mut self, lua: &Lua, w: usize, h: usize) -> Result<()> {
let colors = self.config.colors.borrow();
let greeting = self.config.greeting_message.borrow().render(&colors)?;
let greeting = self.config.greeting_message.borrow().render(lua, &colors)?;
let message: Vec<&str> = greeting.split('\n').collect();
for (c, line) in message.iter().enumerate().take(h.saturating_sub(h / 4)) {
self.terminal.goto(4, h / 4 + c + 1)?;
Expand Down

0 comments on commit 00ae87d

Please sign in to comment.