From 603eac432c95a17d9e394dd963f30e3611740c47 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Thu, 23 Dec 2021 23:29:12 +0100 Subject: [PATCH] init --- .cargo/config.toml | 2 + .gitignore | 2 + Cargo.toml | 9 ++ README.md | 2 + plugin.yaml | 22 ++++ rustfmt.toml | 12 ++ src/main.rs | 111 +++++++++++++++++++ src/powerline.rs | 107 ++++++++++++++++++ src/util.rs | 271 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 538 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 plugin.yaml create mode 100644 rustfmt.toml create mode 100644 src/main.rs create mode 100644 src/powerline.rs create mode 100644 src/util.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..6b77899 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-wasi" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..93c0e87 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "zellij-compact-status" +version = "0.1.0" +authors = ["LordMZTE "] +edition = "2018" + +[dependencies] +owo-colors = "3.1.1" +zellij-tile = "0.23.0" diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea13d0e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# zellij-compact-status +This is a plugin for zellij that replaces the default status bar with a more compact (and hopefully fancy) one. diff --git a/plugin.yaml b/plugin.yaml new file mode 100644 index 0000000..3b22b41 --- /dev/null +++ b/plugin.yaml @@ -0,0 +1,22 @@ +--- +template: + direction: Horizontal + parts: + - direction: Vertical + borderless: true + split_size: + Fixed: 1 + run: + plugin: + location: "zellij:tab-bar" + - direction: Vertical + body: true + - direction: Vertical + borderless: true + split_size: + Fixed: 1 + run: + plugin: + location: "file:target/wasm32-wasi/debug/zellij-compact-status.wasm" +tabs: + - direction: Vertical diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..1059111 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,12 @@ +unstable_features = true +binop_separator = "Back" +format_code_in_doc_comments = true +format_macro_matchers = true +format_strings = true +imports_layout = "HorizontalVertical" +match_block_trailing_comma = true +merge_imports = true +normalize_comments = true +use_field_init_shorthand = true +use_try_shorthand = true +wrap_comments = true diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b2e3053 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,111 @@ +use powerline::Segment; +use zellij_tile::prelude::*; + +use crate::{powerline::Powerline, util::to_owo}; + +mod powerline; +mod util; + +// dracula background color +// config version will be used once bug is fixed upstream +const BG: PaletteColor = PaletteColor::Rgb((40, 42, 54)); +const ALT_BG: PaletteColor = PaletteColor::Rgb((68, 71, 90)); + +register_plugin!(State); + +#[derive(Default)] +struct State { + mode: ModeInfo, +} + +impl ZellijPlugin for State { + fn load(&mut self) { + set_selectable(false); + subscribe(&[EventType::ModeUpdate]); + } + + fn update(&mut self, event: Event) { + if let Event::ModeUpdate(mode) = event { + self.mode = mode; + } + } + + fn render(&mut self, _rows: usize, cols: usize) { + let pal = self.mode.palette; + let mut powerline = Powerline::new(to_owo(BG)); + + match self.mode.mode { + InputMode::Locked => { + powerline.segment(Segment::palette_colors("  ", pal.red, pal.black)) + }, + + InputMode::Normal => powerline + .segment(Segment::palette_colors("Normal", pal.black, pal.yellow)) + // TODO: use pal.bg once https://github.com/zellij-org/zellij/issues/971 is resolved + .segment(Segment::palette_colors(" |  ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors(" | ﴵ ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors(" | ﭕ ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.blue, ALT_BG)) + .segment(Segment::palette_colors("", pal.green, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.green, ALT_BG)) + .segment(Segment::palette_colors(" | ﭔ ", pal.green, ALT_BG)), + + InputMode::Pane => powerline + .segment(Segment::palette_colors("Pane", pal.black, pal.blue)) + .segment(Segment::palette_colors(" ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("p | 怜", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("n |  ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("d |   ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("r |   ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("x |  ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("f |  ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("z |  ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("c | 凜 ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.magenta, ALT_BG)), + + InputMode::Tab => powerline + .segment(Segment::palette_colors("Tab", pal.black, pal.cyan)) + .segment(Segment::palette_colors(" ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("⇋", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("n |  ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("x |  ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("r | 凜 ", pal.magenta, ALT_BG)) + // TODO: find out what sync even is + .segment(Segment::palette_colors("s | מּ ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.magenta, ALT_BG)), + + InputMode::Resize => powerline + .segment(Segment::palette_colors("Resize", pal.black, pal.red)) + .segment(Segment::palette_colors(" ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("+-", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.magenta, ALT_BG)), + + InputMode::Move => powerline + .segment(Segment::palette_colors("Move", pal.black, pal.gray)) + .segment(Segment::palette_colors(" ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("⇋/n | ⇋", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.magenta, ALT_BG)), + + InputMode::Scroll => powerline + .segment(Segment::palette_colors("Scroll", pal.black, pal.green)) + .segment(Segment::palette_colors("", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors("u/d | ﯕ ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.magenta, ALT_BG)), + + InputMode::Session => powerline + .segment(Segment::palette_colors("Session", pal.black, pal.red)) + .segment(Segment::palette_colors("d |  ", pal.magenta, ALT_BG)) + .segment(Segment::palette_colors(" |  ", pal.magenta, ALT_BG)), + + _ => &mut powerline, + }; + + println!("{}", powerline.render(cols)); + } +} diff --git a/src/powerline.rs b/src/powerline.rs new file mode 100644 index 0000000..b383c7a --- /dev/null +++ b/src/powerline.rs @@ -0,0 +1,107 @@ +use owo_colors::{DynColors, OwoColorize}; +use zellij_tile::prelude::*; + +use crate::util::to_owo; + +#[derive(Debug)] +pub struct Powerline { + segments: Vec, + separator: char, + background: DynColors, +} + +impl Powerline { + pub fn new(background: DynColors) -> Self { + Self::with_separator(background, '') + } + + pub fn with_separator(background: DynColors, separator: char) -> Self { + Self { + segments: vec![], + separator, + background, + } + } + + pub fn segment(&mut self, seg: Segment) -> &mut Self { + self.segments.push(seg); + self + } + + pub fn render(&self, max_width: usize) -> String { + let mut buf = String::new(); + let mut max_segs = 0; + let mut cur_len = 0; + + for seg in &self.segments { + if cur_len >= max_width { + max_segs -= 1; + break; + } + + // 2 for worst-case separator width + cur_len += seg.txt.len() + 2; + max_segs += 1; + } + + for (i, seg) in self.segments.iter().enumerate().take(max_segs) { + let pos = match i { + 0 => SegPos::First, + n if n == max_segs - 1 => SegPos::Last, + _ => SegPos::Middle, + }; + + if pos == SegPos::First { + buf.push_str(&' '.on_color(seg.bg).to_string()); + } else { + buf.push_str( + &self + .separator + .color(self.background) + .on_color(seg.bg) + .to_string(), + ); + } + + buf.push_str(&seg.txt.color(seg.fg).on_color(seg.bg).to_string()); + + if pos == SegPos::Last || (pos == SegPos::First && max_segs == 1) { + buf.push_str(&self.separator.color(seg.bg).to_string()); + } else { + buf.push_str( + &self + .separator + .color(seg.bg) + .on_color(self.background) + .to_string(), + ); + } + } + + buf + } +} + +#[derive(Debug)] +pub struct Segment { + pub fg: DynColors, + pub bg: DynColors, + pub txt: String, +} + +impl Segment { + pub fn palette_colors(txt: impl ToString, fg: PaletteColor, bg: PaletteColor) -> Self { + Self { + txt: txt.to_string(), + fg: to_owo(fg), + bg: to_owo(bg), + } + } +} + +#[derive(PartialEq, Eq)] +enum SegPos { + First, + Middle, + Last, +} diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..732d0e0 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,271 @@ +use owo_colors::{DynColors, XtermColors}; +use zellij_tile::prelude::PaletteColor; + +pub fn to_owo(color: PaletteColor) -> DynColors { + match color { + PaletteColor::Rgb((r, g, b)) => DynColors::Rgb(r, g, b), + PaletteColor::EightBit(n) => DynColors::Xterm(u8_to_xtermcolors(n)), + } +} + +pub fn u8_to_xtermcolors(n: u8) -> XtermColors { + // why isn't this in owo-colors?! + match n { + 0 => XtermColors::UserBlack, + 1 => XtermColors::UserRed, + 2 => XtermColors::UserGreen, + 3 => XtermColors::UserYellow, + 4 => XtermColors::UserBlue, + 5 => XtermColors::UserMagenta, + 6 => XtermColors::UserCyan, + 7 => XtermColors::UserWhite, + 8 => XtermColors::UserBrightBlack, + 9 => XtermColors::UserBrightRed, + 10 => XtermColors::UserBrightGreen, + 11 => XtermColors::UserBrightYellow, + 12 => XtermColors::UserBrightBlue, + 13 => XtermColors::UserBrightMagenta, + 14 => XtermColors::UserBrightCyan, + 15 => XtermColors::UserBrightWhite, + 16 => XtermColors::Black, + 17 => XtermColors::StratosBlue, + 18 => XtermColors::NavyBlue, + 19 => XtermColors::MidnightBlue, + 20 => XtermColors::DarkBlue, + 21 => XtermColors::Blue, + 22 => XtermColors::CamaroneGreen, + 23 => XtermColors::BlueStone, + 24 => XtermColors::OrientBlue, + 25 => XtermColors::EndeavourBlue, + 26 => XtermColors::ScienceBlue, + 27 => XtermColors::BlueRibbon, + 28 => XtermColors::JapaneseLaurel, + 29 => XtermColors::DeepSeaGreen, + 30 => XtermColors::Teal, + 31 => XtermColors::DeepCerulean, + 32 => XtermColors::LochmaraBlue, + 33 => XtermColors::AzureRadiance, + 34 => XtermColors::LightJapaneseLaurel, + 35 => XtermColors::Jade, + 36 => XtermColors::PersianGreen, + 37 => XtermColors::BondiBlue, + 38 => XtermColors::Cerulean, + 39 => XtermColors::LightAzureRadiance, + 40 => XtermColors::DarkGreen, + 41 => XtermColors::Malachite, + 42 => XtermColors::CaribbeanGreen, + 43 => XtermColors::LightCaribbeanGreen, + 44 => XtermColors::RobinEggBlue, + 45 => XtermColors::Aqua, + 46 => XtermColors::Green, + 47 => XtermColors::DarkSpringGreen, + 48 => XtermColors::SpringGreen, + 49 => XtermColors::LightSpringGreen, + 50 => XtermColors::BrightTurquoise, + 51 => XtermColors::Cyan, + 52 => XtermColors::Rosewood, + 53 => XtermColors::PompadourMagenta, + 54 => XtermColors::PigmentIndigo, + 55 => XtermColors::DarkPurple, + 56 => XtermColors::ElectricIndigo, + 57 => XtermColors::ElectricPurple, + 58 => XtermColors::VerdunGreen, + 59 => XtermColors::ScorpionOlive, + 60 => XtermColors::Lilac, + 61 => XtermColors::ScampiIndigo, + 62 => XtermColors::Indigo, + 63 => XtermColors::DarkCornflowerBlue, + 64 => XtermColors::DarkLimeade, + 65 => XtermColors::GladeGreen, + 66 => XtermColors::JuniperGreen, + 67 => XtermColors::HippieBlue, + 68 => XtermColors::HavelockBlue, + 69 => XtermColors::CornflowerBlue, + 70 => XtermColors::Limeade, + 71 => XtermColors::FernGreen, + 72 => XtermColors::SilverTree, + 73 => XtermColors::Tradewind, + 74 => XtermColors::ShakespeareBlue, + 75 => XtermColors::DarkMalibuBlue, + 76 => XtermColors::DarkBrightGreen, + 77 => XtermColors::DarkPastelGreen, + 78 => XtermColors::PastelGreen, + 79 => XtermColors::DownyTeal, + 80 => XtermColors::Viking, + 81 => XtermColors::MalibuBlue, + 82 => XtermColors::BrightGreen, + 83 => XtermColors::DarkScreaminGreen, + 84 => XtermColors::ScreaminGreen, + 85 => XtermColors::DarkAquamarine, + 86 => XtermColors::Aquamarine, + 87 => XtermColors::LightAquamarine, + 88 => XtermColors::Maroon, + 89 => XtermColors::DarkFreshEggplant, + 90 => XtermColors::LightFreshEggplant, + 91 => XtermColors::Purple, + 92 => XtermColors::ElectricViolet, + 93 => XtermColors::LightElectricViolet, + 94 => XtermColors::Brown, + 95 => XtermColors::CopperRose, + 96 => XtermColors::StrikemasterPurple, + 97 => XtermColors::DelugePurple, + 98 => XtermColors::DarkMediumPurple, + 99 => XtermColors::DarkHeliotropePurple, + 100 => XtermColors::Olive, + 101 => XtermColors::ClayCreekOlive, + 102 => XtermColors::DarkGray, + 103 => XtermColors::WildBlueYonder, + 104 => XtermColors::ChetwodeBlue, + 105 => XtermColors::SlateBlue, + 106 => XtermColors::LightLimeade, + 107 => XtermColors::ChelseaCucumber, + 108 => XtermColors::BayLeaf, + 109 => XtermColors::GulfStream, + 110 => XtermColors::PoloBlue, + 111 => XtermColors::LightMalibuBlue, + 112 => XtermColors::Pistachio, + 113 => XtermColors::LightPastelGreen, + 114 => XtermColors::DarkFeijoaGreen, + 115 => XtermColors::VistaBlue, + 116 => XtermColors::Bermuda, + 117 => XtermColors::DarkAnakiwaBlue, + 118 => XtermColors::ChartreuseGreen, + 119 => XtermColors::LightScreaminGreen, + 120 => XtermColors::DarkMintGreen, + 121 => XtermColors::MintGreen, + 122 => XtermColors::LighterAquamarine, + 123 => XtermColors::AnakiwaBlue, + 124 => XtermColors::BrightRed, + 125 => XtermColors::DarkFlirt, + 126 => XtermColors::Flirt, + 127 => XtermColors::LightFlirt, + 128 => XtermColors::DarkViolet, + 129 => XtermColors::BrightElectricViolet, + 130 => XtermColors::RoseofSharonOrange, + 131 => XtermColors::MatrixPink, + 132 => XtermColors::TapestryPink, + 133 => XtermColors::FuchsiaPink, + 134 => XtermColors::MediumPurple, + 135 => XtermColors::Heliotrope, + 136 => XtermColors::PirateGold, + 137 => XtermColors::MuesliOrange, + 138 => XtermColors::PharlapPink, + 139 => XtermColors::Bouquet, + 140 => XtermColors::Lavender, + 141 => XtermColors::LightHeliotrope, + 142 => XtermColors::BuddhaGold, + 143 => XtermColors::OliveGreen, + 144 => XtermColors::HillaryOlive, + 145 => XtermColors::SilverChalice, + 146 => XtermColors::WistfulLilac, + 147 => XtermColors::MelroseLilac, + 148 => XtermColors::RioGrandeGreen, + 149 => XtermColors::ConiferGreen, + 150 => XtermColors::Feijoa, + 151 => XtermColors::PixieGreen, + 152 => XtermColors::JungleMist, + 153 => XtermColors::LightAnakiwaBlue, + 154 => XtermColors::Lime, + 155 => XtermColors::GreenYellow, + 156 => XtermColors::LightMintGreen, + 157 => XtermColors::Celadon, + 158 => XtermColors::AeroBlue, + 159 => XtermColors::FrenchPassLightBlue, + 160 => XtermColors::GuardsmanRed, + 161 => XtermColors::RazzmatazzCerise, + 162 => XtermColors::MediumVioletRed, + 163 => XtermColors::HollywoodCerise, + 164 => XtermColors::DarkPurplePizzazz, + 165 => XtermColors::BrighterElectricViolet, + 166 => XtermColors::TennOrange, + 167 => XtermColors::RomanOrange, + 168 => XtermColors::CranberryPink, + 169 => XtermColors::HopbushPink, + 170 => XtermColors::Orchid, + 171 => XtermColors::LighterHeliotrope, + 172 => XtermColors::MangoTango, + 173 => XtermColors::Copperfield, + 174 => XtermColors::SeaPink, + 175 => XtermColors::CanCanPink, + 176 => XtermColors::LightOrchid, + 177 => XtermColors::BrightHeliotrope, + 178 => XtermColors::DarkCorn, + 179 => XtermColors::DarkTachaOrange, + 180 => XtermColors::TanBeige, + 181 => XtermColors::ClamShell, + 182 => XtermColors::ThistlePink, + 183 => XtermColors::Mauve, + 184 => XtermColors::Corn, + 185 => XtermColors::TachaOrange, + 186 => XtermColors::DecoOrange, + 187 => XtermColors::PaleGoldenrod, + 188 => XtermColors::AltoBeige, + 189 => XtermColors::FogPink, + 190 => XtermColors::ChartreuseYellow, + 191 => XtermColors::Canary, + 192 => XtermColors::Honeysuckle, + 193 => XtermColors::ReefPaleYellow, + 194 => XtermColors::SnowyMint, + 195 => XtermColors::OysterBay, + 196 => XtermColors::Red, + 197 => XtermColors::DarkRose, + 198 => XtermColors::Rose, + 199 => XtermColors::LightHollywoodCerise, + 200 => XtermColors::PurplePizzazz, + 201 => XtermColors::Fuchsia, + 202 => XtermColors::BlazeOrange, + 203 => XtermColors::BittersweetOrange, + 204 => XtermColors::WildWatermelon, + 205 => XtermColors::DarkHotPink, + 206 => XtermColors::HotPink, + 207 => XtermColors::PinkFlamingo, + 208 => XtermColors::FlushOrange, + 209 => XtermColors::Salmon, + 210 => XtermColors::VividTangerine, + 211 => XtermColors::PinkSalmon, + 212 => XtermColors::DarkLavenderRose, + 213 => XtermColors::BlushPink, + 214 => XtermColors::YellowSea, + 215 => XtermColors::TexasRose, + 216 => XtermColors::Tacao, + 217 => XtermColors::Sundown, + 218 => XtermColors::CottonCandy, + 219 => XtermColors::LavenderRose, + 220 => XtermColors::Gold, + 221 => XtermColors::Dandelion, + 222 => XtermColors::GrandisCaramel, + 223 => XtermColors::Caramel, + 224 => XtermColors::CosmosSalmon, + 225 => XtermColors::PinkLace, + 226 => XtermColors::Yellow, + 227 => XtermColors::LaserLemon, + 228 => XtermColors::DollyYellow, + 229 => XtermColors::PortafinoYellow, + 230 => XtermColors::Cumulus, + 231 => XtermColors::White, + 232 => XtermColors::DarkCodGray, + 233 => XtermColors::CodGray, + 234 => XtermColors::LightCodGray, + 235 => XtermColors::DarkMineShaft, + 236 => XtermColors::MineShaft, + 237 => XtermColors::LightMineShaft, + 238 => XtermColors::DarkTundora, + 239 => XtermColors::Tundora, + 240 => XtermColors::ScorpionGray, + 241 => XtermColors::DarkDoveGray, + 242 => XtermColors::DoveGray, + 243 => XtermColors::Boulder, + 244 => XtermColors::Gray, + 245 => XtermColors::LightGray, + 246 => XtermColors::DustyGray, + 247 => XtermColors::NobelGray, + 248 => XtermColors::DarkSilverChalice, + 249 => XtermColors::LightSilverChalice, + 250 => XtermColors::DarkSilver, + 251 => XtermColors::Silver, + 252 => XtermColors::DarkAlto, + 253 => XtermColors::Alto, + 254 => XtermColors::Mercury, + 255 => XtermColors::GalleryGray, + } +}