mod line;
mod modestatus;
mod tab;
use std::cmp::{max, min};
use std::convert::TryInto;
use zellij_tile::prelude::*;
use crate::line::tab_line;
use crate::modestatus::modestatus;
use crate::tab::tab_style;
#[derive(Debug, Default)]
pub struct LinePart {
part: String,
len: usize,
tab_index: Option<usize>,
}
#[derive(Default)]
struct State {
tabs: Vec<TabInfo>,
active_tab_idx: usize,
mode_info: ModeInfo,
mouse_click_pos: usize,
should_change_tab: bool,
}
register_plugin!(State);
impl ZellijPlugin for State {
fn load(&mut self) {
set_selectable(false);
subscribe(&[
EventType::TabUpdate,
EventType::ModeUpdate,
EventType::Mouse,
]);
}
fn update(&mut self, event: Event) -> bool {
let mut should_render = false;
match event {
Event::ModeUpdate(mode_info) => {
if self.mode_info != mode_info {
should_render = true;
}
self.mode_info = mode_info
}
Event::TabUpdate(tabs) => {
if let Some(active_tab_index) = tabs.iter().position(|t| t.active) {
// tabs are indexed starting from 1 so we need to add 1
let active_tab_idx = active_tab_index + 1;
if self.active_tab_idx != active_tab_idx || self.tabs != tabs {
should_render = true;
}
self.active_tab_idx = active_tab_idx;
self.tabs = tabs;
} else {
eprintln!("Could not find active tab.");
}
}
Event::Mouse(me) => match me {
Mouse::LeftClick(_, col) => {
if self.mouse_click_pos != col {
should_render = true;
self.should_change_tab = true;
}
self.mouse_click_pos = col;
}
Mouse::ScrollUp(_) => {
should_render = true;
switch_tab_to(min(self.active_tab_idx + 1, self.tabs.len()) as u32);
}
Mouse::ScrollDown(_) => {
should_render = true;
switch_tab_to(max(self.active_tab_idx.saturating_sub(1), 1) as u32);
}
_ => {}
},
_ => {
eprintln!("Got unrecognized event: {:?}", event);
}
};
should_render
}
fn render(&mut self, _rows: usize, cols: usize) {
if self.tabs.is_empty() {
return;
}
let mut all_tabs: Vec<LinePart> = vec![];
let mut active_tab_index = 0;
let mut is_alternate_tab = false;
for t in &mut self.tabs {
let mut tabname = t.name.clone();
if t.active && self.mode_info.mode == InputMode::RenameTab {
if tabname.is_empty() {
tabname = String::from("Enter name...");
}
active_tab_index = t.position;
} else if t.active {
active_tab_index = t.position;
}
let tab = tab_style(tabname, t, self.mode_info.style.colors);
is_alternate_tab = !is_alternate_tab;
all_tabs.push(tab);
}
let tab_line = tab_line(
all_tabs,
active_tab_index,
cols.saturating_sub(1),
self.mode_info.style.colors,
self.mode_info.mode,
);
let mut s = String::new();
let mut len_cnt = 0;
for bar_part in tab_line {
s = format!("{}{}", s, &bar_part.part);
if self.should_change_tab
&& self.mouse_click_pos >= len_cnt
&& self.mouse_click_pos < len_cnt + bar_part.len
&& bar_part.tab_index.is_some()
{
// Tabs are indexed starting from 1, therefore we need add 1 to tab_index.
let tab_index: u32 = bar_part.tab_index.unwrap().try_into().unwrap();
switch_tab_to(tab_index + 1);
}
len_cnt += bar_part.len;
}
let background = match self.mode_info.style.colors.theme_hue {
ThemeHue::Dark => self.mode_info.style.colors.black,
ThemeHue::Light => self.mode_info.style.colors.white,
};
s = format!("{}{}", s, modestatus(&self.mode_info, cols));
match background {
PaletteColor::Rgb((r, g, b)) => {
print!("{}\u{1b}[48;2;{};{};{}m\u{1b}[0K", s, r, g, b);
}
PaletteColor::EightBit(color) => {
print!("{}\u{1b}[48;5;{}m\u{1b}[0K", s, color);
}
}
self.should_change_tab = false;
}
}