Skip to content

Commit

Permalink
Merge branch 'main' into feature/pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
dj95 committed Mar 13, 2024
2 parents f6d9f8f + ded1809 commit 3cc941e
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.direnv/
.zjstatus.log
69 changes: 69 additions & 0 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bench = false

[features]
bench = []
tracing = []

[dependencies]
zellij-tile = "0.40.0"
Expand All @@ -25,9 +26,13 @@ uuid = { version = "1.6.1", features = ["v4"] }
lazy_static = "1.4.0"
cached = { version = "0.46.1", features = ["wasm"] }
console = "0.15.8"
tracing-subscriber = "0.3.18"
tracing = "0.1.40"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false, features = ["html_reports"] }
criterion = { version = "0.5.1", default-features = false, features = [
"html_reports",
] }

[[bench]]
name = "benches"
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bench:
| xargs -I{} wasmtime --dir $PWD/target::target {} --bench --color=always

build:
cargo build
cargo build --features tracing

run: build
zellij -l ./plugin-dev-workspace.kdl -s zjstatus-dev
Expand Down
58 changes: 56 additions & 2 deletions src/bin/zjstatus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use widgets::{
tabs::TabsWidget,
widget::Widget,
};

use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

use zellij_tile::prelude::*;

use chrono::Local;
use std::{collections::BTreeMap, sync::Arc, usize};
use std::{collections::BTreeMap, fs::File, sync::Arc, usize};
use uuid::Uuid;

use zjstatus::{
Expand All @@ -31,8 +34,25 @@ struct State {
#[cfg(not(test))]
register_plugin!(State);

#[cfg(feature = "tracing")]
fn init_tracing() {
let file = File::create(".zjstatus.log");
let file = match file {
Ok(file) => file,
Err(error) => panic!("Error: {:?}", error),
};
let debug_log = tracing_subscriber::fmt::layer().with_writer(Arc::new(file));

tracing_subscriber::registry().with(debug_log).init();

tracing::info!("tracing initialized");
}

impl ZellijPlugin for State {
fn load(&mut self, configuration: BTreeMap<String, String>) {
#[cfg(feature = "tracing")]
init_tracing();

// we need the ReadApplicationState permission to receive the ModeUpdate and TabUpdate
// events
// we need the RunCommands permission to run "cargo test" in a floating window
Expand All @@ -41,6 +61,7 @@ impl ZellijPlugin for State {
PermissionType::ChangeApplicationState,
PermissionType::RunCommands,
]);

subscribe(&[
EventType::Mouse,
EventType::ModeUpdate,
Expand Down Expand Up @@ -89,11 +110,16 @@ impl ZellijPlugin for State {
should_render
}

#[tracing::instrument(skip_all, fields(event_type))]
fn update(&mut self, event: Event) -> bool {
let mut should_render = false;
match event {
Event::Mouse(mouse_info) => {
tracing::Span::current().record("event_type", "Event::Mouse");
tracing::debug!(mouse = ?mouse_info);

if !self.got_permissions {
tracing::info!("no permissions");
return false;
}

Expand All @@ -104,7 +130,11 @@ impl ZellijPlugin for State {
);
}
Event::ModeUpdate(mode_info) => {
tracing::Span::current().record("event_type", "Event::ModeUpdate");
tracing::debug!(mode = ?mode_info.mode);

if !self.got_permissions {
tracing::info!("no permissions");
return false;
}

Expand All @@ -113,7 +143,11 @@ impl ZellijPlugin for State {
should_render = true;
}
Event::PaneUpdate(pane_info) => {
tracing::Span::current().record("event_type", "Event::PaneUpdate");
tracing::debug!(pane_count = ?pane_info.panes.len());

if !self.got_permissions {
tracing::info!("no permissions");
return false;
}

Expand All @@ -130,12 +164,23 @@ impl ZellijPlugin for State {

should_render = true;
}
Event::PermissionRequestResult(_result) => {
Event::PermissionRequestResult(result) => {
tracing::Span::current().record("event_type", "Event::PermissionRequestResult");
tracing::debug!(result = ?result);
set_selectable(false);
self.got_permissions = true;
}
Event::RunCommandResult(exit_code, stdout, stderr, context) => {
tracing::Span::current().record("event_type", "Event::RunCommandResult");
tracing::debug!(
exit_code = ?exit_code,
stdout = ?String::from_utf8(stdout.clone()),
stderr = ?String::from_utf8(stderr.clone()),
context = ?context
);

if !self.got_permissions {
tracing::info!("no permissions");
return false;
}
self.state.cache_mask = UpdateEventMask::Command as u8;
Expand Down Expand Up @@ -163,7 +208,10 @@ impl ZellijPlugin for State {
}
}
Event::SessionUpdate(session_info, _) => {
tracing::Span::current().record("event_type", "Event::SessionUpdate");

if !self.got_permissions {
tracing::info!("no permissions");
return false;
}

Expand All @@ -185,7 +233,11 @@ impl ZellijPlugin for State {
should_render = true;
}
Event::TabUpdate(tab_info) => {
tracing::Span::current().record("event_type", "Event::TabUpdate");
tracing::debug!(tab_count = ?tab_info.len());

if !self.got_permissions {
tracing::info!("no permissions");
return false;
}

Expand Down Expand Up @@ -235,5 +287,7 @@ fn register_widgets(configuration: &BTreeMap<String, String>) -> BTreeMap<String
);
widget_map.insert("tabs".to_owned(), Arc::new(TabsWidget::new(configuration)));

tracing::debug!("registered widgets: {:?}", widget_map.keys());

widget_map
}
3 changes: 2 additions & 1 deletion src/border.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use crate::render::FormattedPart;

const DEFAULT_CHAR: &str = "─";

#[derive(Default, PartialEq)]
#[derive(Default, PartialEq, Debug)]
pub enum BorderPosition {
#[default]
Top,
Bottom,
}

#[derive(Debug)]
pub struct BorderConfig {
pub enabled: bool,
pub char: String,
Expand Down
6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn event_mask_from_widget_name(name: &str) -> u8 {
}
}

#[derive(Default)]
#[derive(Default, Debug)]
pub struct ModuleConfig {
pub left_parts_config: String,
pub left_parts: Vec<FormattedPart>,
Expand Down Expand Up @@ -326,6 +326,7 @@ impl ModuleConfig {
)
}

#[tracing::instrument(skip_all)]
fn get_spacer_left(&self, output_left: &str, output_center: &str, cols: usize) -> String {
let text_count = console::measure_text_width(output_left)
+ (console::measure_text_width(output_center) as f32 / 2.0).floor() as usize;
Expand All @@ -336,9 +337,11 @@ impl ModuleConfig {
// count of 0 on tab creation
let space_count = center_pos.saturating_sub(text_count);

tracing::debug!("space_count: {:?}", space_count);
self.format_space.format_string(&" ".repeat(space_count))
}

#[tracing::instrument(skip_all)]
fn get_spacer_right(&self, output_right: &str, output_center: &str, cols: usize) -> String {
let text_count = console::measure_text_width(output_right)
+ (console::measure_text_width(output_center) as f32 / 2.0).ceil() as usize;
Expand All @@ -349,6 +352,7 @@ impl ModuleConfig {
// count of 0 on tab creation
let space_count = center_pos.saturating_sub(text_count);

tracing::debug!("space_count: {:?}", space_count);
self.format_space.format_string(&" ".repeat(space_count))
}

Expand Down
Loading

0 comments on commit 3cc941e

Please sign in to comment.