From a7722273b0c569f1fc2d01b2e0fc981ef68daa47 Mon Sep 17 00:00:00 2001 From: Chester Liu Date: Sat, 24 Jul 2021 12:54:35 +0800 Subject: [PATCH] tmux-cc: add support for layout-change (#972) * tmux-cc: add support for layout-change * Test for client-session-changed * Tmux 3 * Handle visible_layout * Apply suggestions from code review Co-authored-by: Wez Furlong * Fmt --- tmux-cc/src/lib.rs | 82 +++++++++++++++++++++++++++++++++++++++++++ tmux-cc/src/tmux.pest | 7 ++-- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/tmux-cc/src/lib.rs b/tmux-cc/src/lib.rs index f3ffea2b8..46bc4fb21 100644 --- a/tmux-cc/src/lib.rs +++ b/tmux-cc/src/lib.rs @@ -23,6 +23,13 @@ pub struct Guarded { pub output: String, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct WindowLayout { + pub layout_id: String, + pub width: u64, + pub height: u64, +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum Event { Begin { @@ -82,6 +89,12 @@ pub enum Event { window: TmuxWindowId, name: String, }, + LayoutChange { + window: TmuxWindowId, + layout: WindowLayout, + visible_layout: Option, + raw_flags: Option, + }, } fn parse_pane_id(pair: Pair) -> anyhow::Result { @@ -143,6 +156,29 @@ fn parse_guard(mut pairs: Pairs) -> anyhow::Result<(i64, u64, i64)> { Ok((timestamp, number, flags)) } +/// Parses a window_layout line, for example "b25d,80x24,0,0,0" +fn parse_window_layout(pair: Pair) -> Option { + match pair.as_rule() { + Rule::window_layout => { + let mut pairs = pair.into_inner(); + let layout_id_option = pairs.next()?.as_str().parse::().ok(); + let width_option = pairs.next()?.as_str().parse::().ok(); + let height_option = pairs.next()?.as_str().parse::().ok(); + if let (Some(layout_id), Some(width), Some(height)) = + (layout_id_option, width_option, height_option) + { + return Some(WindowLayout { + layout_id, + width, + height, + }); + } + return None; + } + _ => None, + } +} + fn parse_line(line: &str) -> anyhow::Result { let mut pairs = parser::TmuxParser::parse(Rule::line_entire, line)?; let pair = pairs.next().ok_or_else(|| anyhow::anyhow!("no pairs!?"))?; @@ -238,11 +274,25 @@ fn parse_line(line: &str) -> anyhow::Result { let window = parse_window_id(pairs.next().unwrap())?; Ok(Event::SessionWindowChanged { session, window }) } + Rule::layout_change => { + let mut pairs = pair.into_inner(); + let window = parse_window_id(pairs.next().unwrap())?; + let layout = pairs.next().and_then(parse_window_layout).unwrap(); + let visible_layout = pairs.next().and_then(parse_window_layout); + let raw_flags = pairs.next().map(|r| r.as_str().to_owned()); + Ok(Event::LayoutChange { + window, + layout, + visible_layout, + raw_flags, + }) + } Rule::pane_id | Rule::word | Rule::client_name | Rule::window_id | Rule::session_id + | Rule::window_layout | Rule::any_text | Rule::line | Rule::line_entire @@ -596,6 +646,9 @@ here %window-add @1 %sessions-changed %session-changed $1 1 +%client-session-changed /dev/pts/5 $1 home +%layout-change @1 b25d,80x24,0,0,0 +%layout-change @1 cafd,120x29,0,0,0 cafd,120x29,0,0,0 * %output %1 \\033[1m\\033[7m%\\033[27m\\033[1m\\033[0m \\015 \\015 %output %1 \\033kwez@cube-localdomain:~\\033\\134\\033]2;wez@cube-localdomain:~\\033\\134 %output %1 \\033]7;file://cube-localdomain/home/wez\\033\\134 @@ -623,6 +676,35 @@ here session: 1, name: "1".to_owned(), }, + Event::ClientSessionChanged { + client_name: "/dev/pts/5".to_owned(), + session: 1, + session_name: "home".to_owned() + }, + Event::LayoutChange { + window: 1, + layout: WindowLayout { + layout_id: "b25d".to_owned(), + width: 80, + height: 24 + }, + visible_layout: None, + raw_flags: None + }, + Event::LayoutChange { + window: 1, + layout: WindowLayout { + layout_id: "cafd".to_owned(), + width: 120, + height: 29 + }, + visible_layout: Some(WindowLayout { + layout_id: "cafd".to_owned(), + width: 120, + height: 29 + }), + raw_flags: Some("*".to_owned()) + }, Event::Output { pane: 1, text: "\x1b[1m\x1b[7m%\x1b[27m\x1b[1m\x1b[0m \r \r".to_owned() diff --git a/tmux-cc/src/tmux.pest b/tmux-cc/src/tmux.pest index 108f05034..a6e428eb9 100644 --- a/tmux-cc/src/tmux.pest +++ b/tmux-cc/src/tmux.pest @@ -1,11 +1,12 @@ number = { ASCII_DIGIT+ } any_text = { ANY* } -word = { ASCII_ALPHANUMERIC+ } +word = { (ASCII_ALPHANUMERIC | "/")+ } pane_id = { "%" ~ number } window_id = { "@" ~ number } session_id = { "$" ~ number } client_name = { word } +window_layout = { word ~ "," ~ number ~ "x" ~ number ~ "," ~ number ~ "," ~ number ~ "," ~ number } begin = { "%begin " ~ number ~ " " ~ number ~ " " ~ number } end = { "%end " ~ number ~ " " ~ number ~ " " ~ number } @@ -23,6 +24,7 @@ window_renamed = { "%window-renamed " ~ window_id ~ " " ~ any_text } session_changed = { "%session-changed " ~ session_id ~ " " ~ any_text } session_renamed = { "%session-renamed " ~ any_text } session_window_changed = { "%session-window-changed " ~ session_id ~ " " ~ window_id } +layout_change = { "%layout-change " ~ window_id ~ " " ~ (window_layout ~ " " ~ window_layout ~ " " ~any_text | window_layout) } line = _{ ( client_session_changed | @@ -39,7 +41,8 @@ line = _{ ( window_add | window_close | window_pane_changed | - window_renamed + window_renamed | + layout_change ) } line_entire = _{ SOI ~ line ~ EOI }