filter repeat events

This commit is contained in:
atagen 2026-02-16 00:38:27 +11:00
parent f7d47d6c80
commit e3afb85f23

View file

@ -6,59 +6,58 @@ def main [] {
}
print "niri-s76-bridge started"
niri msg -j event-stream | lines | reduce --fold [] { |line, windows|
niri msg -j event-stream | lines | reduce --fold {focused: 0, windows: []} { |line, state|
let event = $line | from json
let event_type = $event | columns | first
mut windows = $state.windows
mut current = $state.focused
match $event_type {
"WindowsChanged" => {
mut acc = $windows
for w in $event.WindowsChanged.windows {
if $w.pid != null and $w.is_focused {
if $w.pid != null and $w.is_focused and $w.pid != $current {
notify-scheduler $w.pid
$current = $w.pid
}
if $w.pid != null {
let existing = $acc | where id == $w.id
let existing = $windows | where id == $w.id
if ($existing | is-empty) {
$acc = ($acc | append {id: $w.id, pid: $w.pid})
$windows = ($windows | append {id: $w.id, pid: $w.pid})
}
}
}
$acc
}
"WindowOpenedOrChanged" => {
let w = $event.WindowOpenedOrChanged.window
if $w.is_focused and $w.pid != null {
if $w.is_focused and $w.pid != null and $w.pid != $current {
notify-scheduler $w.pid
$current = $w.pid
}
if $w.pid != null {
let existing = $windows | where id == $w.id
let existing = $state.windows | where id == $w.id
if ($existing | is-empty) {
$windows | append {id: $w.id, pid: $w.pid}
} else {
$windows
$windows = $windows | append {id: $w.id, pid: $w.pid}
}
} else {
$windows
}
}
"WindowFocusChanged" => {
let focused_id = $event.WindowFocusChanged.id
if $focused_id != null {
let match = $windows | where id == $focused_id
let match = $state.windows | where id == $focused_id
if not ($match | is-empty) {
let focused_pid = $match.0.pid
if $focused_pid != null {
if $focused_pid != null and $focused_pid != $current {
notify-scheduler $focused_pid
$current = $focused_pid
}
}
}
$windows
}
"WindowClosed" => {
$windows | where id != $event.WindowClosed.id
$windows = $windows | where id != $event.WindowClosed.id
}
_ => $windows
_ => ()
}
{focused: $current, windows: $windows}
}
}