1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 23:21:08 +03:00

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 <wez@wezfurlong.org>

* Fmt
This commit is contained in:
Chester Liu 2021-07-24 12:54:35 +08:00 committed by GitHub
parent dcbbda7702
commit a7722273b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 2 deletions

View File

@ -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<WindowLayout>,
raw_flags: Option<String>,
},
}
fn parse_pane_id(pair: Pair<Rule>) -> anyhow::Result<TmuxPaneId> {
@ -143,6 +156,29 @@ fn parse_guard(mut pairs: Pairs<Rule>) -> 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<Rule>) -> Option<WindowLayout> {
match pair.as_rule() {
Rule::window_layout => {
let mut pairs = pair.into_inner();
let layout_id_option = pairs.next()?.as_str().parse::<String>().ok();
let width_option = pairs.next()?.as_str().parse::<u64>().ok();
let height_option = pairs.next()?.as_str().parse::<u64>().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<Event> {
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<Event> {
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()

View File

@ -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 }