Skip to content

Commit f419eb9

Browse files
committedDec 10, 2024·
feat: initial statusline config with messy implementation
1 parent 9379b37 commit f419eb9

File tree

21 files changed

+252
-65
lines changed

21 files changed

+252
-65
lines changed
 

‎Cargo.lock

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ tracing-appender = { version = "0.2.3" }
2424
tracing-subscriber = { version = "0.3.18" }
2525
mlua = { version = "0.10.1", features = ["luajit", "vendored", "serialize"] }
2626
serde = { version = "1.0.215", features = ["derive"] }
27+
parking_lot = { version = "0.12.3" }

‎glyph-config/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ tracing.workspace = true
1313
mlua.workspace = true
1414
serde.workspace = true
1515
tokio.workspace = true
16+
parking_lot.workspace = true
1617

1718
directories = "5.0.1"

‎glyph-config/src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use glyph_core::command::{Context, MappableCommand};
88
use glyph_core::editor::Mode;
99
use glyph_core::highlights::HighlightGroup;
1010
use glyph_runtime::keymap::{LuaKeymapOpts, LuaMappableCommand};
11-
use glyph_runtime::{RuntimeMessage, RuntimeQuery};
11+
use glyph_runtime::statusline::StatuslineConfig;
12+
use glyph_runtime::RuntimeMessage;
1213
use glyph_trie::Trie;
1314
use mlua::{Lua, LuaSerdeExt, Table, Value};
1415
use serde::Deserialize;
@@ -130,6 +131,7 @@ pub struct KeymapConfig<'cfg> {
130131
pub struct Config<'cfg> {
131132
cursor: CursorConfig,
132133
gutter: GutterConfig,
134+
pub statusline: StatuslineConfig,
133135
pub highlight_groups: HashMap<String, HighlightGroup>,
134136
pub keymaps: Trie<KeymapConfig<'cfg>>,
135137
}
@@ -156,8 +158,8 @@ impl<'cfg> Config<'cfg> {
156158

157159
let (highlight_groups, keymaps) = handle_setup_messages(setup_messages);
158160

159-
let glyph_mod = glyph_runtime::get_or_create_module(runtime, "glyph")?;
160-
let config = glyph_mod.get::<Table>("options")?;
161+
let glyph = glyph_runtime::get_or_create_module(runtime, "glyph")?;
162+
let config = glyph.get::<Table>("options")?;
161163

162164
let cursor = runtime.from_value::<CursorConfig>(config.get::<Value>("cursor")?)?;
163165
let gutter = runtime.from_value::<GutterConfig>(config.get::<Value>("gutter")?)?;
@@ -167,6 +169,8 @@ impl<'cfg> Config<'cfg> {
167169
gutter,
168170
highlight_groups,
169171
keymaps,
172+
// will be loaded when the editors starts up
173+
statusline: Default::default(),
170174
})
171175
}
172176

‎glyph-core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
tracing.workspace = true
88
thiserror.workspace = true
9+
parking_lot.workspace = true
910

1011
ropey = "1.6.1"
1112
slotmap = "1.0.7"

‎glyph-core/src/command.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use std::collections::BTreeMap;
22
use std::fmt::Debug;
3+
use std::sync::Arc;
4+
5+
use parking_lot::RwLock;
36

47
use crate::cursor::Cursor;
58
use crate::editor::Editor;
69
use crate::window::WindowId;
710

811
#[derive(Debug)]
912
pub struct Context<'ctx> {
10-
pub editor: &'ctx mut Editor,
13+
pub editor: Arc<RwLock<Editor>>,
1114
pub cursors: &'ctx mut BTreeMap<WindowId, Cursor>,
1215
}
1316

@@ -61,14 +64,16 @@ impl Debug for MappableCommand {
6164

6265
fn move_left(ctx: &mut Context) {
6366
{
64-
let tab = ctx.editor.focused_tab();
67+
let editor = ctx.editor.read();
68+
let tab = editor.focused_tab();
6569
let window = tab.tree.focus();
6670
let window = tab.tree.window(window);
6771
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
6872
cursor.move_left();
6973
}
7074

71-
let tab = ctx.editor.focused_tab_mut();
75+
let mut editor = ctx.editor.write();
76+
let tab = editor.focused_tab_mut();
7277
let window = tab.tree.focus();
7378
let window = tab.tree.window_mut(window);
7479
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
@@ -80,15 +85,17 @@ fn move_left(ctx: &mut Context) {
8085

8186
fn move_down(ctx: &mut Context) {
8287
{
83-
let tab = ctx.editor.focused_tab();
88+
let editor = ctx.editor.read();
89+
let tab = editor.focused_tab();
8490
let window = tab.tree.focus();
8591
let window = tab.tree.window(window);
86-
let document = ctx.editor.document(&window.document);
92+
let document = editor.document(&window.document);
8793
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
8894
cursor.move_down(document);
8995
}
9096

91-
let tab = ctx.editor.focused_tab_mut();
97+
let mut editor = ctx.editor.write();
98+
let tab = editor.focused_tab_mut();
9299
let window = tab.tree.focus();
93100
let window = tab.tree.window_mut(window);
94101
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
@@ -100,14 +107,16 @@ fn move_down(ctx: &mut Context) {
100107

101108
fn move_up(ctx: &mut Context) {
102109
{
103-
let tab = ctx.editor.focused_tab();
110+
let editor = ctx.editor.read();
111+
let tab = editor.focused_tab();
104112
let window = tab.tree.focus();
105113
let window = tab.tree.window(window);
106114
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
107115
cursor.move_up();
108116
}
109117

110-
let tab = ctx.editor.focused_tab_mut();
118+
let mut editor = ctx.editor.write();
119+
let tab = editor.focused_tab_mut();
111120
let window = tab.tree.focus();
112121
let window = tab.tree.window_mut(window);
113122
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
@@ -119,15 +128,17 @@ fn move_up(ctx: &mut Context) {
119128

120129
pub fn move_right(ctx: &mut Context) {
121130
{
122-
let tab = ctx.editor.focused_tab();
131+
let editor = ctx.editor.read();
132+
let tab = editor.focused_tab();
123133
let window = tab.tree.focus();
124134
let window = tab.tree.window(window);
125-
let document = ctx.editor.document(&window.document);
135+
let document = editor.document(&window.document);
126136
let cursor = ctx.cursors.get_mut(&window.id).unwrap();
127137
cursor.move_right(document);
128138
}
129139

130-
let tab = ctx.editor.focused_tab_mut();
140+
let mut editor = ctx.editor.write();
141+
let tab = editor.focused_tab_mut();
131142
let window = tab.tree.focus();
132143
let window = tab.tree.window_mut(window);
133144
let cursor = ctx.cursors.get_mut(&window.id).unwrap();

‎glyph-runtime/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ thiserror.workspace = true
1010
mlua.workspace = true
1111
serde.workspace = true
1212
tokio.workspace = true
13+
parking_lot.workspace = true

‎glyph-runtime/src/editor.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
use std::sync::Arc;
2+
13
use mlua::{Lua, Table};
2-
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
4+
use parking_lot::RwLock;
5+
use tokio::sync::mpsc::UnboundedSender;
36

47
use crate::error::Result;
5-
use crate::{RuntimeMessage, RuntimeQuery};
8+
use crate::{GlyphContext, RuntimeMessage};
69

710
pub fn setup_editor_api(
811
lua: &Lua,
912
core: &Table,
10-
runtime_sender: UnboundedSender<RuntimeMessage<'static>>,
13+
_runtime_sender: UnboundedSender<RuntimeMessage<'static>>,
14+
context: Arc<RwLock<GlyphContext>>,
1115
) -> Result<()> {
1216
core.set(
1317
"get_editor_mode",
14-
lua.create_function(move |_: &Lua, _: ()| get_editor_mode(runtime_sender.clone()))?,
18+
lua.create_function(move |_: &Lua, _: ()| get_editor_mode(context.clone()))?,
1519
)?;
1620

1721
Ok(())
1822
}
1923

20-
fn get_editor_mode(runtime_sender: UnboundedSender<RuntimeMessage<'_>>) -> mlua::Result<String> {
21-
todo!();
24+
fn get_editor_mode(context: Arc<RwLock<GlyphContext>>) -> mlua::Result<String> {
25+
Ok(context.read().editor.read().mode().to_string())
2226
}

‎glyph-runtime/src/lib.rs

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
mod colors;
1+
pub mod colors;
22
pub mod editor;
33
pub mod error;
44
pub mod keymap;
5+
pub mod statusline;
56

67
use std::path::{Path, PathBuf};
8+
use std::sync::Arc;
79

810
use colors::setup_colors_api;
911
use editor::setup_editor_api;
1012
use error::{Error, Result};
11-
use glyph_core::editor::Mode;
13+
use glyph_core::editor::{Editor, Mode};
1214
use glyph_core::highlights::HighlightGroup;
1315
use keymap::{setup_keymap_api, LuaKeymap};
14-
use mlua::{Lua, Table, Value};
16+
use mlua::{FromLua, Function, Lua, Table, Value};
17+
use parking_lot::RwLock;
18+
use statusline::StatuslineConfig;
1519
use tokio::sync::mpsc::UnboundedSender;
20+
use tokio::sync::oneshot::Sender;
1621

1722
#[derive(Debug)]
1823
pub enum RuntimeQuery {
19-
EditorMode(UnboundedSender<Mode>),
24+
EditorMode(Sender<Mode>),
2025
}
2126

2227
#[derive(Debug)]
@@ -34,7 +39,6 @@ pub fn setup_lua_runtime(config_dir: &Path, runtime_sender: UnboundedSender<Runt
3439
let core = lua.create_table()?;
3540
setup_colors_api(&lua, &core, runtime_sender.clone())?;
3641
setup_keymap_api(&lua, &core, runtime_sender.clone())?;
37-
setup_editor_api(&lua, &core, runtime_sender.clone())?;
3842
glyph.set("_core", core)?;
3943

4044
let package = globals.get::<Table>("package")?;
@@ -64,6 +68,29 @@ pub fn setup_lua_runtime(config_dir: &Path, runtime_sender: UnboundedSender<Runt
6468
Ok(lua)
6569
}
6670

71+
#[derive(Debug)]
72+
pub struct GlyphContext {
73+
pub editor: Arc<RwLock<Editor>>,
74+
}
75+
76+
pub fn setup_post_startup_apis(
77+
lua: &Lua,
78+
runtime_sender: UnboundedSender<RuntimeMessage<'static>>,
79+
context: GlyphContext,
80+
) -> Result<StatuslineConfig> {
81+
let context = Arc::new(RwLock::new(context));
82+
let glyph = get_or_create_module(lua, "glyph")?;
83+
let core = glyph.get::<Table>("_core")?;
84+
85+
setup_editor_api(lua, &core, runtime_sender.clone(), context)?;
86+
87+
// setup all of the things that needed to wait until editor startup on lua runtime
88+
glyph.get::<Function>("_startup")?.call::<()>(())?;
89+
let options = glyph.get::<Table>("options")?;
90+
91+
Ok(StatuslineConfig::from_lua(options.get::<Value>("statusline")?, lua)?)
92+
}
93+
6794
pub fn get_or_create_module(lua: &Lua, name: &str) -> Result<Table> {
6895
let globals = lua.globals();
6996
let package = globals.get::<Table>("package")?;

‎glyph-runtime/src/statusline.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use mlua::{FromLua, Function, Lua, LuaSerdeExt, Number, Table, Value};
2+
3+
use crate::colors::LuaHighlightGroup;
4+
5+
#[derive(Debug, Default)]
6+
pub struct StatuslineConfig {
7+
pub left: Vec<StatuslineSection>,
8+
pub right: Vec<StatuslineSection>,
9+
}
10+
11+
#[derive(Debug)]
12+
pub enum StatuslineStyle {
13+
HighlightGroup(LuaHighlightGroup),
14+
Named(String),
15+
}
16+
17+
#[derive(Debug)]
18+
pub struct StatuslineSection {
19+
pub content: StatuslineContent,
20+
pub style: StatuslineStyle,
21+
}
22+
23+
#[derive(Debug)]
24+
pub enum StatuslineContent {
25+
Immediate(String),
26+
Dynamic(Function),
27+
}
28+
29+
impl FromLua for StatuslineConfig {
30+
fn from_lua(value: Value, lua: &Lua) -> mlua::Result<Self> {
31+
let Value::Table(statusline) = value else {
32+
return Err(mlua::Error::runtime("statusline must be a table"));
33+
};
34+
35+
let left_val = statusline.get::<Table>("left")?;
36+
let right_val = statusline.get::<Table>("right")?;
37+
38+
let mut left = vec![];
39+
let mut right = vec![];
40+
41+
for pair in left_val.pairs::<Number, Value>() {
42+
let (_, value) = pair?;
43+
let section = StatuslineSection::from_lua(value, lua)?;
44+
left.push(section);
45+
}
46+
47+
for pair in right_val.pairs::<Number, Value>() {
48+
let (_, value) = pair?;
49+
let section = StatuslineSection::from_lua(value, lua)?;
50+
right.push(section);
51+
}
52+
53+
Ok(StatuslineConfig { left, right })
54+
}
55+
}
56+
57+
impl FromLua for StatuslineSection {
58+
fn from_lua(value: Value, lua: &Lua) -> mlua::Result<Self> {
59+
let Value::Table(table) = value else {
60+
return Err(mlua::Error::runtime("statusline section must be a table"));
61+
};
62+
63+
let content = StatuslineContent::from_lua(table.get::<Value>("content")?, lua)?;
64+
let style = StatuslineStyle::from_lua(table.get::<Value>("style")?, lua)?;
65+
66+
Ok(StatuslineSection { content, style })
67+
}
68+
}
69+
70+
impl FromLua for StatuslineContent {
71+
fn from_lua(value: Value, _: &Lua) -> mlua::Result<Self> {
72+
match value {
73+
Value::String(inner) => Ok(StatuslineContent::Immediate(inner.to_string_lossy())),
74+
Value::Function(inner) => Ok(StatuslineContent::Dynamic(inner)),
75+
_ => Err(mlua::Error::runtime(
76+
"statusline content can only be a function or string",
77+
)),
78+
}
79+
}
80+
}
81+
82+
impl FromLua for StatuslineStyle {
83+
fn from_lua(value: Value, lua: &Lua) -> mlua::Result<Self> {
84+
match value {
85+
Value::Table(_) => Ok(StatuslineStyle::HighlightGroup(lua.from_value(value)?)),
86+
Value::String(inner) => Ok(StatuslineStyle::Named(inner.to_string_lossy())),
87+
_ => Err(mlua::Error::runtime(
88+
"statusline style can only be a string or highlight group",
89+
)),
90+
}
91+
}
92+
}

‎glyph-term/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ glyph-config.workspace = true
1010
tracing.workspace = true
1111
tracing-appender.workspace = true
1212
tracing-subscriber.workspace = true
13+
parking_lot.workspace = true
1314

1415
crossterm = { version = "0.28.1", features = ["event-stream"] }
1516
slotmap = "1.0.7"

‎glyph-term/src/layers/editor_layer.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,18 @@ impl EditorLayer {
105105
config: GlyphConfig,
106106
) {
107107
let cursor = cursors.get(&window.id).unwrap();
108-
let line_drawer = get_line_drawer(&config);
108+
let line_drawer = get_line_drawer(config);
109109
line_drawer.draw_line_numbers(area, document, window, cursor, buffer, config);
110110
}
111111

112112
pub fn draw_statusline(&self, buffer: &mut Buffer, ctx: &mut Context, area: Rect) {
113-
let tab = ctx.editor.focused_tab();
113+
let editor = ctx.editor.read();
114+
let tab = editor.focused_tab();
114115
let focused_window = tab.tree.focus();
115116
let window = tab.tree.window(focused_window);
116117

117118
let cursor = ctx.cursors.get(&window.id).unwrap();
118-
let editor_mode = ctx.editor.mode().to_string().to_uppercase();
119+
let editor_mode = ctx.editor.read().mode().to_string().to_uppercase();
119120
let cursor = cursor.to_string();
120121

121122
let padding = area.width - (editor_mode.len() + cursor.len()) as u16;
@@ -132,17 +133,17 @@ impl EditorLayer {
132133
config: GlyphConfig,
133134
) -> Result<Option<EventResult>, std::io::Error> {
134135
match key_event.code {
135-
KeyCode::Char(_) => match ctx.editor.mode() {
136+
KeyCode::Char(_) => match ctx.editor.read().mode() {
136137
Mode::Normal => {}
137138
Mode::Insert => {}
138139
},
139140
_ => todo!(),
140141
}
141142
if let KeyCode::Char(ch) = key_event.code {
142143
if let Some(result) = config.keymaps.find_word(ch.to_string()) {
143-
if result.data.mode == ctx.editor.mode() {
144+
if result.data.mode == ctx.editor.read().mode() {
144145
let mut context = CmdContext {
145-
editor: ctx.editor,
146+
editor: ctx.editor.clone(),
146147
cursors: ctx.cursors,
147148
};
148149
result.data.command.run(&mut context);
@@ -156,24 +157,26 @@ impl EditorLayer {
156157

157158
impl RenderLayer for EditorLayer {
158159
fn draw(&self, buffer: &mut Buffer, ctx: &mut Context, config: GlyphConfig) {
159-
let mut area = ctx.editor.area();
160+
let mut area = ctx.editor.read().area();
160161
let mut statusline_area = area.split_bottom(2);
161162
let _commandline_area = statusline_area.split_bottom(1);
162163

163164
self.draw_statusline(buffer, ctx, statusline_area);
164165

165-
for (window, _) in ctx.editor.focused_tab().tree.windows() {
166-
let document = ctx.editor.document(&window.document);
166+
let editor = ctx.editor.read();
167+
for (window, _) in editor.focused_tab().tree.windows() {
168+
let document = editor.document(&window.document);
167169
self.draw_window(area, document, window, buffer, ctx.highlighter, ctx.cursors, config);
168170
}
169171
}
170172

171173
fn cursor(&self, ctx: &mut Context, config: GlyphConfig) -> (Option<Point>, CursorKind) {
172-
let tab = ctx.editor.focused_tab();
174+
let editor = ctx.editor.read();
175+
let tab = editor.focused_tab();
173176
let focused_window = tab.tree.focus();
174177
let window = tab.tree.window(focused_window);
175178
let cursor = ctx.cursors.get(&window.id).unwrap();
176-
let document = ctx.editor.document(&window.document);
179+
let document = editor.document(&window.document);
177180
let gutter_size = calculate_gutter_size(document, config);
178181

179182
let point = Point {

‎glyph-term/src/renderer.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::BTreeMap;
2+
use std::sync::Arc;
23

34
use crossterm::event::Event;
45
use glyph_config::GlyphConfig;
@@ -7,6 +8,7 @@ use glyph_core::editor::{Editor, EventResult};
78
use glyph_core::rect::Point;
89
use glyph_core::syntax::Highlighter;
910
use glyph_core::window::WindowId;
11+
use parking_lot::RwLock;
1012

1113
use crate::backend::CursorKind;
1214
use crate::buffer::Buffer;
@@ -32,7 +34,7 @@ pub trait RenderLayer {
3234

3335
#[derive(Debug)]
3436
pub struct Context<'ctx> {
35-
pub editor: &'ctx mut Editor,
37+
pub editor: Arc<RwLock<Editor>>,
3638
pub highlighter: &'ctx mut Highlighter,
3739
pub cursors: &'ctx mut BTreeMap<WindowId, Cursor>,
3840
}

‎glyph-term/src/ui/line_number.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl LineNumberDrawer for LineDrawer {
146146
}
147147
}
148148

149-
pub fn get_line_drawer(config: &GlyphConfig) -> LineDrawer {
149+
pub fn get_line_drawer(config: GlyphConfig) -> LineDrawer {
150150
match config.gutter().line_numbers {
151151
LineNumbersConfig::Absolute => LineDrawer::Absolute(AbsoluteLineDrawer),
152152
LineNumbersConfig::Relative => LineDrawer::Relative(RelativeLineDrawer),

‎glyph/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ tracing.workspace = true
1414
tracing-appender.workspace = true
1515
tracing-subscriber.workspace = true
1616
mlua.workspace = true
17+
parking_lot.workspace = true
1718

1819
crossterm = { version = "0.28.1", features = ["event-stream"] }
1920
futures = { version = "0.3.31" }

‎glyph/src/glyph.rs

+30-9
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
11
use std::collections::BTreeMap;
22
use std::io::{self, Stdout};
33
use std::path::PathBuf;
4+
use std::sync::Arc;
45

56
use crossterm::event::{Event, KeyCode, KeyEvent};
67
use futures::{Stream, StreamExt};
7-
use glyph_config::GlyphConfig;
8+
use glyph_config::Config;
89
use glyph_core::cursor::Cursor;
910
use glyph_core::editor::{Editor, EventResult, OpenAction};
1011
use glyph_core::rect::Point;
1112
use glyph_core::syntax::Highlighter;
1213
use glyph_core::window::WindowId;
14+
use glyph_runtime::{setup_post_startup_apis, GlyphContext, RuntimeMessage};
1315
use glyph_term::backend::{Backend, CrosstermBackend};
1416
use glyph_term::layers::editor_layer::EditorLayer;
1517
use glyph_term::renderer::{Context, Renderer};
1618
use glyph_term::terminal::Terminal;
19+
use mlua::Lua;
20+
use parking_lot::RwLock;
21+
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
1722

1823
#[derive(Debug)]
1924
pub struct Glyph<'a, B>
2025
where
2126
B: Backend,
2227
{
2328
terminal: Terminal<B>,
24-
editor: Editor,
29+
editor: Arc<RwLock<Editor>>,
2530
cursors: BTreeMap<WindowId, Cursor>,
2631
highlighter: Highlighter,
2732
renderer: Renderer,
28-
config: GlyphConfig<'a>,
33+
config: &'a mut Config<'a>,
34+
runtime: Lua,
2935
}
3036

3137
impl<'a, B> Glyph<'a, B>
3238
where
3339
B: Backend,
3440
{
35-
pub fn new(backend: B, config: GlyphConfig) -> Glyph<B> {
41+
pub fn new(
42+
backend: B,
43+
runtime: Lua,
44+
runtime_sender: UnboundedSender<RuntimeMessage<'static>>,
45+
_runtime_receiver: UnboundedReceiver<RuntimeMessage<'static>>,
46+
config: &'a mut Config<'a>,
47+
) -> glyph_runtime::error::Result<Glyph<'a, B>> {
3648
let file = std::env::args().nth(1);
3749

3850
let mut editor = Editor::new(backend.area().expect("couldn't get terminal size"));
@@ -56,14 +68,23 @@ where
5668
let cursor = Cursor::default();
5769
cursors.insert(window, cursor);
5870

59-
Glyph {
71+
let editor = Arc::new(RwLock::new(editor));
72+
73+
let glyph_context = GlyphContext { editor: editor.clone() };
74+
75+
let statusline = setup_post_startup_apis(&runtime, runtime_sender, glyph_context)?;
76+
println!("{statusline:?}");
77+
config.statusline = statusline;
78+
79+
Ok(Glyph {
6080
editor,
6181
terminal,
6282
renderer,
6383
highlighter,
6484
config,
6585
cursors,
66-
}
86+
runtime,
87+
})
6788
}
6889

6990
// TODO: make an actual error type
@@ -91,7 +112,7 @@ where
91112
S: Stream<Item = io::Result<Event>> + Unpin,
92113
{
93114
loop {
94-
if self.editor.should_close() {
115+
if self.editor.read().should_close() {
95116
break Ok(());
96117
}
97118

@@ -112,7 +133,7 @@ where
112133

113134
fn handle_event(&mut self, event: Event) -> Result<Option<EventResult>, io::Error> {
114135
let mut context = Context {
115-
editor: &mut self.editor,
136+
editor: self.editor.clone(),
116137
highlighter: &mut self.highlighter,
117138
cursors: &mut self.cursors,
118139
};
@@ -121,7 +142,7 @@ where
121142

122143
fn draw_frame(&mut self) -> Result<(), std::io::Error> {
123144
let mut context = Context {
124-
editor: &mut self.editor,
145+
editor: self.editor.clone(),
125146
highlighter: &mut self.highlighter,
126147
cursors: &mut self.cursors,
127148
};

‎glyph/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1818
let (runtime_sender, mut runtime_receiver) = unbounded_channel();
1919
let runtime = glyph_runtime::setup_lua_runtime(DIRS.get().unwrap().config(), runtime_sender.clone())?;
2020

21-
let config = glyph_config::Config::load(&runtime, runtime_sender.clone(), &mut runtime_receiver)?;
21+
let mut config = glyph_config::Config::load(&runtime, runtime_sender.clone(), &mut runtime_receiver)?;
2222

2323
let backend = CrosstermBackend::new(stdout());
2424

25-
let mut glyph = Glyph::new(backend, &config);
25+
let mut glyph = Glyph::new(backend, runtime, runtime_sender, runtime_receiver, &mut config)?;
2626
glyph.run(&mut EventStream::new()).await?;
2727

2828
Ok(())

‎runtime/api/colors.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
--- @type glyph
22
local glyph = require("glyph")
33

4-
--- @class glyph.colors.hl_group_opts
4+
--- @class glyph.colors.hl_group
55
--- @field fg? string
66
--- @field bg? string
77
--- @field bold? boolean
88

99
--- @class glyph.api.colors
10-
--- @field set_hl_group fun(name: string, opts: glyph.colors.hl_group_opts)
10+
--- @field set_hl_group fun(name: string, opts: glyph.colors.hl_group)
1111
local M = {}
1212

1313
--- @param name string
14-
--- @param opts glyph.colors.hl_group_opts
14+
--- @param opts glyph.colors.hl_group
1515
function M.set_hl_group(name, opts)
1616
opts = opts or {}
1717

‎runtime/defaults/options/init.lua

-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
--- @type glyph
2-
local glyph = require("glyph")
3-
41
--- @class glyph.defaults.options
52
--- @field cursor glyph.options.cursor
63
--- @field gutter glyph.options.gutter
@@ -17,13 +14,4 @@ M.gutter = {
1714
sign_column = "all",
1815
}
1916

20-
M.statusline = {
21-
left = {
22-
{ content = glyph.api.get_editor_mode(), style = {} },
23-
},
24-
right = {},
25-
}
26-
27-
print(glyph.api.get_editor_mode())
28-
2917
return M

‎runtime/defaults/statusline.lua

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--- @type glyph
2+
local glyph = require("glyph")
3+
4+
glyph.options.statusline = {
5+
left = {
6+
{ content = glyph.api.get_editor_mode, style = "some_group" },
7+
{ content = "some_string", style = { fg = "#ffffff", bg = "#ffffff" } },
8+
},
9+
right = {},
10+
}

‎runtime/init.lua

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
--- @field api glyph.api
44
--- @field options glyph.options
55
--- @field _core glyph.core
6+
--- @field _startup function
67

78
--- @class glyph.core
89
--- @field set_keymap_command fun(mode: "n" | "i" | "c" | "v", keys: string, command: string, opts?: KeymapOpts)
910
--- @field set_keymap_function fun(mode: "n" | "i" | "c" | "v", keys: string, command: function, opts?: KeymapOpts)
10-
--- @field set_hl_group fun(name: string, opts: glyph.colors.hl_group_opts)
11+
--- @field set_hl_group fun(name: string, opts: glyph.colors.hl_group)
1112
--- @field get_editor_mode fun(): string
1213

1314
--- @class glyph.options
1415
--- @field cursor glyph.options.cursor
1516
--- @field gutter glyph.options.gutter
17+
--- @field statusline glyph.options.statusline
1618

1719
--- @class glyph.options.cursor
1820
--- @field style "block" | "steady_bar"
@@ -23,10 +25,22 @@
2325
--- @field line_numbers "absolute" | "relative" | "relative_numbered"
2426
--- @field sign_column "all" | "git" | "lsp"
2527

28+
--- @class glyph.options.statusline
29+
--- @field left glyph.options.statusline.section[]
30+
--- @field right glyph.options.statusline.section[]
31+
32+
--- @class glyph.options.statusline.section
33+
--- @field content string|fun(): string
34+
--- @field style string|glyph.colors.hl_group
35+
2636
--- @type glyph
2737
local glyph = require("glyph")
2838

2939
glyph.u = require("utils")
3040
glyph.api = require("api")
3141

42+
glyph._startup = function()
43+
require("defaults.statusline")
44+
end
45+
3246
require("defaults")

0 commit comments

Comments
 (0)
Please sign in to comment.