You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用jq获取i3中活动容器的父容器布局?

用jq获取i3窗口管理器中活动容器的父容器布局

我想用jq工具查找i3窗口管理器中当前活动容器的父容器布局。i3-msg -t get_tree会返回包含.layout和.focused属性的递归容器结构,需求很明确:不要.focused == true的容器自身的.layout,而是要它的父节点的.layout。

示例JSON结构如下(实际数据可能嵌套在多层.nodes[]下):

{
  "focused": false,
  "layout": "splitv",  // 需要返回的值
  "nodes": [
    {
      "focused": true,  // 查找的依据
      "layout": "splith",  // 不需要返回的值
      "nodes": []
    }
  ]
}

最初的尝试

我一开始参考递归搜索键值的方法,执行了以下命令:

$ i3-msg -t get_tree | jq -r '.. | select(.focused?) | .layout'
splith

但结果返回的是活动容器自身的布局,不符合需求。如果jq有假设的parent()函数,我会写成类似.. | select(.focused?) | parent().parent().layout的语句,从当前层级定位到父容器。

后续尝试及错误

之后我尝试参考相关方法解决,但遇到了多个错误,推测是..运算符遍历到了非对象类型(比如数字)导致的:

$ i3-msg -t get_tree | jq -r '.. | select(.nodes[] | select(.focused?)) | .layout'
jq: error (at <stdin>:1): Cannot index number with string "nodes"
$ i3-msg -t get_tree | jq -r '.. | select(.nodes[]? | select(.focused?)) | .layout'
jq: error (at <stdin>:1): Cannot index number with string "nodes"
$ i3-msg -t get_tree | jq -r '.. | select(.nodes?[] | select(.focused?)) | .layout'
jq: error (at <stdin>:1): Cannot iterate over null (null)
$ i3-msg -t get_tree | jq -r '.. | select(.nodes? | select(.focused?)) | .layout'
# 无输出

我已经导出了i3-msg -t get_tree的完整数据,方便未安装i3的用户测试。


内容的提问来源于stack exchange,提问作者Dominik Kaszewski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 12:42:44