doti3/bar/src/workspace_apps.php

140 lines
2.9 KiB
PHP
Executable file

#!/usr/bin/env php
<?php
$windowKey = $argv[1];
$blockButton = $argv[2];
require __DIR__.'/base/block.php';
function getWorkspaces(): array
{
$output = shell_exec('i3-msg -t get_workspaces');
return json_decode($output, true);
}
function getVisibleWorkspaces(): array
{
$workspaces = [];
foreach (getWorkspaces() as $wp) {
if (true === $wp['visible']) {
$workspaces[] = $wp;
}
}
return $workspaces;
}
function getTree(): array
{
$output = shell_exec('i3-msg -t get_tree');
return json_decode($output, true);
}
function getVisibleWorkspacesNodes(array $tree, array $workspaces): array
{
$nodes = [];
$num = $tree['num'] ?? null;
$output = $tree['output'] ?? null;
foreach ($workspaces as $workspace) {
if ($num === $workspace['num']) {
$nodes = array_merge(
$nodes,
$tree['nodes']
);
$nodes = array_merge(
$nodes,
$tree['floating_nodes']
);
} elseif ($output === '__i3' && isset($tree['window'])) {
$nodes[] = $tree;
}
}
foreach ($tree as $node) {
if (is_array($node)) {
$nodes = array_merge(
$nodes,
getVisibleWorkspacesNodes($node, $workspaces)
);
}
}
foreach ($nodes as $k => $n) {
if (!isset($n['window'])) {
unset($nodes[$k]);
foreach ($n['nodes'] as $node) {
$nodes[] = $node;
}
foreach ($n['floating_nodes'] as $node) {
$nodes[] = $node;
}
}
}
return $nodes;
}
$node = getVisibleWorkspacesNodes(getTree(), getVisibleWorkspaces())[$windowKey] ?? null;
if (!$node) {
die(0);
}
$title = $node['window_properties']['title'];
$instance = $node['window_properties']['instance'];
$window = $node['window'];
$focused = $node['focused'];
$urgent = $node['urgent'];
$scratchpad = $node['output'] === '__i3';
if (mb_strlen($title) > 25) {
$title = mb_substr($title, 0, 22).'…';
}
$foreground = '#9cb7d1';
$background = '#222222';
if ($focused) {
$foreground = '#07c0d4';
$background = '#333333';
} elseif ($scratchpad) {
$foreground = '#bababa';
$background = '#222222';
}
if ($urgent) {
if (date('s') % 2) {
$foreground = '#ffffff';
$background = '#87af15';
} else {
$foreground = '#ffffff';
$background = '#07c0d4';
}
}
$fullText = pspan(mb_strtoupper($title), $foreground, $background).' ';
if ($blockButton) {
shell_exec(sprintf('i3-msg "[id=%s] focus"', $window));
}
echo block(
'workspace_apps_'.$windowKey,
[
'full_text' => $fullText,
'color' => color('normal'),
'separator' => false,
'separator_block_width' => 0,
'min_width' => 1,
'align' => 'left',
]
);