如何获得当前焦点容器的布局(例如“splith”、“tabbed”)?我想最好的方法是解析swaymsg -t get_tree
using的 JSON 输出(下面的示例) jq
,但我不确定如何处理这些“n-debth 树”。
这可能是一个非常基本的问题,但这是我第一次处理 json
“附件”:示例 JSON 输出swaymsg -t get-tree
这是上述命令的输出,其中打开了 3 个终端,其中焦点终端的属性为“focused”:true。
{
"border": "none",
"current_border_width": 0,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [
3
],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 1,
"layout": "splith",
"marks": [],
"name": "root",
"nodes": [
{
"border": "none",
"current_border_width": 0,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [
2147483646
],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 2147483647,
"layout": "output",
"marks": [],
"name": "__i3",
"nodes": [
{
"border": "none",
"current_border_width": 0,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [],
"focused": false,
"fullscreen_mode": 1,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 2147483646,
"layout": "splith",
"marks": [],
"name": "__i3_scratch",
"nodes": [],
"orientation": "horizontal",
"percent": null,
"rect": {
"height": 720,
"width": 360,
"x": 0,
"y": 0
},
"sticky": false,
"type": "workspace",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
}
],
"orientation": "horizontal",
"percent": null,
"rect": {
"height": 720,
"width": 360,
"x": 0,
"y": 0
},
"sticky": false,
"type": "output",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
},
{
"active": true,
"adaptive_sync_status": "disabled",
"border": "none",
"current_border_width": 0,
"current_mode": {
"height": 1440,
"refresh": 60006,
"width": 720
},
"current_workspace": "1",
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"dpms": false,
"floating_nodes": [],
"focus": [
30,
32
],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 3,
"layout": "output",
"make": "<Unknown>",
"marks": [],
"max_render_time": 0,
"model": "<Unknown>",
"modes": [
{
"height": 1440,
"refresh": 60006,
"width": 720
}
],
"name": "DSI-1",
"nodes": [
{
"border": "none",
"current_border_width": 0,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [
36,
35
],
"focused": false,
"fullscreen_mode": 1,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 30,
"layout": "splith",
"marks": [],
"name": "1",
"nodes": [
{
"app_id": "foot",
"border": "pixel",
"current_border_width": 3,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 500,
"width": 700,
"x": 0,
"y": 0
},
"id": 35,
"idle_inhibitors": {
"application": "none",
"user": "none"
},
"inhibit_idle": false,
"layout": "none",
"marks": [],
"max_render_time": 0,
"name": "",
"nodes": [],
"orientation": "none",
"percent": 0.5,
"pid": 25044,
"rect": {
"height": 455,
"width": 180,
"x": 0,
"y": 25
},
"shell": "xdg_shell",
"sticky": false,
"type": "con",
"urgent": false,
"visible": true,
"window": null,
"window_rect": {
"height": 449,
"width": 174,
"x": 3,
"y": 3
}
},
{
"border": "none",
"current_border_width": 0,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [
34
],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 36,
"layout": "splitv",
"marks": [],
"name": null,
"nodes": [
{
"border": "none",
"current_border_width": 0,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [
27,
33
],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 34,
"layout": "splitv",
"marks": [],
"name": null,
"nodes": [
{
"app_id": "foot",
"border": "pixel",
"current_border_width": 3,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [],
"focused": true,
"fullscreen_mode": 0,
"geometry": {
"height": 500,
"width": 700,
"x": 0,
"y": 0
},
"id": 27,
"idle_inhibitors": {
"application": "none",
"user": "none"
},
"inhibit_idle": false,
"layout": "none",
"marks": [],
"max_render_time": 0,
"name": "",
"nodes": [],
"orientation": "none",
"percent": 0.5010989010989011,
"pid": 28522,
"rect": {
"height": 228,
"width": 180,
"x": 180,
"y": 25
},
"shell": "xdg_shell",
"sticky": false,
"type": "con",
"urgent": false,
"visible": true,
"window": null,
"window_rect": {
"height": 222,
"width": 174,
"x": 3,
"y": 3
}
},
{
"app_id": "foot",
"border": "pixel",
"current_border_width": 3,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 500,
"width": 700,
"x": 0,
"y": 0
},
"id": 33,
"idle_inhibitors": {
"application": "none",
"user": "none"
},
"inhibit_idle": false,
"layout": "none",
"marks": [],
"max_render_time": 0,
"name": "",
"nodes": [],
"orientation": "none",
"percent": 0.4989010989010989,
"pid": 23338,
"rect": {
"height": 227,
"width": 180,
"x": 180,
"y": 253
},
"shell": "xdg_shell",
"sticky": false,
"type": "con",
"urgent": false,
"visible": true,
"window": null,
"window_rect": {
"height": 221,
"width": 174,
"x": 3,
"y": 3
}
}
],
"orientation": "vertical",
"percent": 1,
"rect": {
"height": 455,
"width": 180,
"x": 180,
"y": 25
},
"sticky": false,
"type": "con",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
}
],
"orientation": "vertical",
"percent": 0.5,
"rect": {
"height": 455,
"width": 180,
"x": 180,
"y": 25
},
"sticky": false,
"type": "con",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
}
],
"num": 1,
"orientation": "horizontal",
"output": "DSI-1",
"percent": null,
"rect": {
"height": 455,
"width": 360,
"x": 0,
"y": 25
},
"representation": "H[foot V[V[foot foot]]]",
"sticky": false,
"type": "workspace",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
},
{
"border": "none",
"current_border_width": 0,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [
19
],
"focused": false,
"fullscreen_mode": 1,
"geometry": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"id": 32,
"layout": "splitv",
"marks": [],
"name": "4",
"nodes": [
{
"app_id": "foot",
"border": "pixel",
"current_border_width": 3,
"deco_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
},
"floating_nodes": [],
"focus": [],
"focused": false,
"fullscreen_mode": 0,
"geometry": {
"height": 500,
"width": 700,
"x": 0,
"y": 0
},
"id": 19,
"idle_inhibitors": {
"application": "none",
"user": "none"
},
"inhibit_idle": false,
"layout": "none",
"marks": [],
"max_render_time": 0,
"name": "",
"nodes": [],
"orientation": "none",
"percent": 1,
"pid": 3363,
"rect": {
"height": 455,
"width": 360,
"x": 0,
"y": 25
},
"shell": "xdg_shell",
"sticky": false,
"type": "con",
"urgent": false,
"visible": false,
"window": null,
"window_rect": {
"height": 455,
"width": 360,
"x": 0,
"y": 3
}
}
],
"num": 4,
"orientation": "vertical",
"output": "DSI-1",
"percent": null,
"rect": {
"height": 455,
"width": 360,
"x": 0,
"y": 25
},
"representation": "V[foot]",
"sticky": false,
"type": "workspace",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
}
],
"orientation": "none",
"percent": 1,
"primary": false,
"rect": {
"height": 720,
"width": 360,
"x": 0,
"y": 0
},
"scale": 2,
"scale_filter": "nearest",
"serial": "",
"sticky": false,
"transform": "normal",
"type": "output",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
}
],
"orientation": "horizontal",
"percent": null,
"rect": {
"height": 720,
"width": 360,
"x": 0,
"y": 0
},
"sticky": false,
"type": "root",
"urgent": false,
"window": null,
"window_rect": {
"height": 0,
"width": 0,
"x": 0,
"y": 0
}
}
答案1
您可以使用recurse
函数jq
来递归地遍历nodes
of root
、output
、workspace
和con
tainers (以及任何嵌套容器)。使用该select
函数,您可以仅过滤出焦点容器并获取键的值layout
。
swaymsg -t get_tree | jq 'recurse(.nodes[]) | select(.nodes[].focused == true).layout'