Json

sway 獲取目前焦點容器的佈局

  • December 25, 2021

我如何獲得目前焦點容器的佈局(例如’splith’,’tabbed’)?我想最好的方法是解析swaymsg -t get_treeusing的 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
 }
}

您可以使用recurseof 函式jq遞歸地遍歷nodesof rootoutputworkspacecontainers(以及任何嵌套容器)。使用該select功能,您可以僅過濾掉焦點容器並獲取layout鍵的值。

swaymsg -t get_tree | jq 'recurse(.nodes[]) | select(.nodes[].focused == true).layout'

引用自:https://unix.stackexchange.com/questions/683903