Skip to content

Commit

Permalink
feat: new Manager component for better style extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Oct 17, 2023
1 parent 04a09b9 commit 29ae9e0
Show file tree
Hide file tree
Showing 24 changed files with 313 additions and 184 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod executor;
mod help;
mod input;
mod logs;
mod manager;
mod root;
mod select;
mod signals;
Expand Down
17 changes: 0 additions & 17 deletions app/src/manager/folder.rs

This file was deleted.

51 changes: 0 additions & 51 deletions app/src/manager/layout.rs

This file was deleted.

7 changes: 0 additions & 7 deletions app/src/manager/mod.rs

This file was deleted.

14 changes: 5 additions & 9 deletions app/src/root.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::Ctx;

use plugin::components;
use ratatui::{buffer::Buffer, layout::{Constraint, Direction, Layout, Rect}, widgets::Widget};
use tracing::error;

use super::{input, manager, select, tasks, which};
use super::{input, select, tasks, which};
use crate::help;

pub(super) struct Root<'a> {
Expand All @@ -21,13 +21,9 @@ impl<'a> Widget for Root<'a> {
.constraints([Constraint::Length(1), Constraint::Min(0), Constraint::Length(1)])
.split(area);

if let Err(e) = plugin::Header.render(chunks[0], buf) {
error!("{:?}", e);
}
manager::Layout::new(self.cx).render(chunks[1], buf);
if let Err(e) = plugin::Status.render(chunks[2], buf) {
error!("{:?}", e);
}
components::Header::new(self.cx).render(chunks[0], buf);
components::Manager::new(self.cx).render(chunks[1], buf);
components::Status::new(self.cx).render(chunks[2], buf);

if self.cx.tasks.visible {
tasks::Layout::new(self.cx).render(area, buf);
Expand Down
1 change: 1 addition & 0 deletions plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ core = { path = "../core" }
shared = { path = "../shared" }

# External dependencies
ansi-to-tui = "^3"
anyhow = "^1"
mlua = { version = "^0", features = [ "luajit52", "vendored", "serialize" ] }
ratatui = "^0"
Expand Down
25 changes: 25 additions & 0 deletions plugin/preset/components/manager.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Manager = {}

function Manager:render(area)
local chunks = ui.Layout()
:direction(ui.Direction.HORIZONTAL)
:constraints({
ui.Constraint.Ratio(MANAGER.layout.parent, MANAGER.layout.all),
ui.Constraint.Ratio(MANAGER.layout.current, MANAGER.layout.all),
ui.Constraint.Ratio(MANAGER.layout.preview, MANAGER.layout.all),
})
:split(area)

return {
-- Parent
table.unpack(Folder:render(chunks[1]:padding(ui.Padding.x(1)), { kind = Folder.Kind.Parent })),
-- Current
table.unpack(Folder:render(chunks[2], { kind = Folder.Kind.Current })),
-- Preview
ui.Base(chunks[3]:padding(ui.Padding.x(1)), "Preview"),

-- Borders
ui.Bar(chunks[1], ui.Position.RIGHT):symbol(THEME.manager.border_symbol):style(THEME.manager.border_style),
ui.Bar(chunks[3], ui.Position.LEFT):symbol(THEME.manager.border_symbol):style(THEME.manager.border_style),
}
end
11 changes: 11 additions & 0 deletions plugin/preset/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ ui = {
BOTTOM = 3,
LEFT = 4,
},

Padding = setmetatable({
left = function(left) return ui.Padding.new(left, 0, 0, 0) end,
right = function(right) return ui.Padding.new(0, right, 0, 0) end,
top = function(top) return ui.Padding.new(0, 0, top, 0) end,
bottom = function(bottom) return ui.Padding.new(0, 0, 0, bottom) end,
x = function(x) return ui.Padding.new(x, x, 0, 0) end,
y = function(y) return ui.Padding.new(0, 0, y, y) end,
}, {
__call = function(...) return ui.Padding.new(...) end,
}),
}

function ui.highlight_ranges(s, ranges)
Expand Down
76 changes: 0 additions & 76 deletions plugin/src/components.rs

This file was deleted.

43 changes: 43 additions & 0 deletions plugin/src/components/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use mlua::{FromLua, Lua, Table, UserData, Value};
use ratatui::widgets::Widget;

use crate::{layout::Rect, GLOBALS, LUA};

#[derive(Clone)]
pub struct Base {
area: ratatui::layout::Rect,

name: String,
}

impl Base {
pub(crate) fn install() -> mlua::Result<()> {
let ui: Table = GLOBALS.get("ui")?;
ui.set(
"Base",
LUA.create_function(|_, (area, name): (Rect, String)| Ok(Self { area: area.0, name }))?,
)
}

pub fn render(self, cx: &core::Ctx, buf: &mut ratatui::buffer::Buffer) {
match self.name.as_ref() {
"Preview" => super::Preview::new(cx).render(self.area, buf),
_ => {}
}
}
}

impl<'lua> FromLua<'lua> for Base {
fn from_lua(value: Value<'lua>, _: &'lua Lua) -> mlua::Result<Self> {
match value {
Value::UserData(ud) => Ok(ud.borrow::<Self>()?.clone()),
_ => Err(mlua::Error::FromLuaConversionError {
from: value.type_name(),
to: "Base",
message: Some("expected a Base".to_string()),
}),
}
}
}

impl UserData for Base {}
39 changes: 39 additions & 0 deletions plugin/src/components/components.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use mlua::{AnyUserData, Table};
use shared::RoCell;

use super::Base;
use crate::{layout::{Bar, Gauge, List, Paragraph}, GLOBALS};

pub(super) static COMP_FOLDER: RoCell<Table> = RoCell::new();
pub(super) static COMP_HEADER: RoCell<Table> = RoCell::new();
pub(super) static COMP_MANAGER: RoCell<Table> = RoCell::new();
pub(super) static COMP_STATUS: RoCell<Table> = RoCell::new();

pub fn init() -> mlua::Result<()> {
COMP_FOLDER.init(GLOBALS.get("Folder")?);
COMP_HEADER.init(GLOBALS.get("Header")?);
COMP_MANAGER.init(GLOBALS.get("Manager")?);
COMP_STATUS.init(GLOBALS.get("Status")?);
Ok(())
}

pub(super) fn layout(
values: Vec<AnyUserData>,
cx: &core::Ctx,
buf: &mut ratatui::prelude::Buffer,
) -> mlua::Result<()> {
for value in values {
if let Ok(c) = value.take::<Paragraph>() {
c.render(buf)
} else if let Ok(c) = value.take::<List>() {
c.render(buf)
} else if let Ok(c) = value.take::<Bar>() {
c.render(buf)
} else if let Ok(c) = value.take::<Base>() {
c.render(cx, buf)
} else if let Ok(c) = value.take::<Gauge>() {
c.render(buf)
}
}
Ok(())
}
35 changes: 35 additions & 0 deletions plugin/src/components/folder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use mlua::TableExt;
use ratatui::widgets::Widget;
use tracing::error;

use super::{layout, COMP_FOLDER};
use crate::{layout::Rect, LUA};

pub struct Folder<'a> {
cx: &'a core::Ctx,
kind: u8,
}

impl<'a> Folder<'a> {
#[inline]
pub fn parent(cx: &'a core::Ctx) -> Self { Self { cx, kind: 0 } }

#[inline]
pub fn current(cx: &'a core::Ctx) -> Self { Self { cx, kind: 1 } }

#[inline]
pub fn preview(cx: &'a core::Ctx) -> Self { Self { cx, kind: 2 } }
}

impl<'a> Widget for Folder<'a> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || {
let args = LUA.create_table()?;
args.set("kind", self.kind)?;
layout(COMP_FOLDER.call_method::<_, _>("render", (Rect(area), args))?, self.cx, buf)
};
if let Err(e) = f() {
error!("{:?}", e);
}
}
}
24 changes: 24 additions & 0 deletions plugin/src/components/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use mlua::TableExt;
use ratatui::widgets::Widget;
use tracing::error;

use super::{layout, COMP_HEADER};
use crate::layout::Rect;

pub struct Header<'a> {
cx: &'a core::Ctx,
}

impl<'a> Header<'a> {
#[inline]
pub fn new(cx: &'a core::Ctx) -> Self { Self { cx } }
}

impl<'a> Widget for Header<'a> {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
let mut f = || layout(COMP_HEADER.call_method::<_, _>("render", Rect(area))?, self.cx, buf);
if let Err(e) = f() {
error!("{:?}", e);
}
}
}
Loading

0 comments on commit 29ae9e0

Please sign in to comment.