保存和恢复i3工作区布局的简单而灵活的解决方案

i3-resurrect的Python项目详细描述


i3复活

保存和恢复i3工作区的简单而灵活的解决方案

构建状态coverage statusgithub issuesgithub pull requestspypi-version

目录

简介

i3 resurrect是一个可以保存和恢复布局并运行的程序 i3工作区中的程序。

通过使用i3ipc从 工作区树并将其写入json文件。

通过在工作区中查找每个进程并编写 cmdline(用于启动程序的命令)和cwd(当前工作 目录)到json文件。

在恢复程序时,python的子进程模块用于启动保存的 具有正确工作目录的程序。

恢复布局时,i3的内置功能布局恢复功能是 使用。这将创建占位符窗口,每个窗口都将"吞咽"任何新的 与指定条件(窗口类、实例、标题等)匹配的窗口。

xdool用于使i3将现有窗口视为新窗口。 这对于按窗口标题匹配是必需的,因为标题必须匹配 当窗口第一次出现时,程序通常只在 窗口已创建 (请参见此处的 有关详细信息)。

背景

这个项目是由python和bash脚本混合而成的。 为了能够快速地保存和加载工作区而编写的。

我讨厌重新启动电脑,因为它会破坏我打开的所有东西 (往往很多)。

为了解决这个问题,我尽量让自己 使一切恢复到重新启动前的状态。

我很快就发现了i3保存树ode>实用程序和i3的附加布局 命令,但这些对我自己没多大用处 保存后手动自定义布局并重新启动所有程序 恢复布局时手动操作。

我的解决方案是创建一个脚本,从 I3保存所需的树,并使用 i3ipcwmctrl,和 psutilpython库以获取 在保存的工作区中启动程序所需的命令。

自从我决定公开发布这个,我已经提高了 编写大量代码,去掉了那些讨厌的bash部分。 现在所有的代码都是python,不再需要i3 save tree了 在python中重新实现。

开始

要求

  • python 3.6
  • xdool

安装

来自使用yay的aur(推荐给arch linux用户)

yay -S i3-resurrect-git

来自PYPI(推荐给其他人)

pip3 install --user --upgrade i3-resurrect

确保~/.local/bin位于PATH环境变量中。

手册

获取源代码

git clone git@github.com:JonnyHaystack/i3-resurrect.git

使用pip在本地安装

cd i3-resurrect
pip3 install --user .

用法

命令行

完整的命令行文档:

Usage: i3_resurrect.py save [OPTIONS]

  Save an i3 workspace's layout and running programs to a file.

Options:
  -w, --workspace TEXT       The workspace to save.
                             [default: current workspace]
  -d, --directory DIRECTORY  The directory to save the workspace to.
                             [default: ~/.i3/i3-resurrect]
  -s, --swallow TEXT         The swallow criteria to use.
                             [options: class,instance,title,window_role]
                             [default: class,instance]
  --layout-only              Only save layout.
  --programs-only            Only save running programs.


Usage: i3_resurrect.py restore [OPTIONS]

  Restore i3 workspace layout and programs.

Options:
  -w, --workspace TEXT       The workspace to restore.
                             [default: current workspace]
  -d, --directory DIRECTORY  The directory to restore the workspace from.
                             [default: ~/.i3/i3-resurrect]
  --layout-only              Only restore layout.
  --programs-only            Only restore running programs.

基本用法,仅匹配窗口类/实例:

# Save workspace '1'
i3-resurrect save -w 1

# Restore workspace '1'
i3-resurrect restore -w 1

通过匹配标题还原更精确的布局:

# Save workspace '1'
i3-resurrect save -w 1 --swallow=class,instance,title

# Restore workspace '1' programs
i3-resurrect restore -w 1 --programs-only

# Apply workspace '1' layout
i3-resurrect restore -w 1 --layout-only

按标题匹配窗口时,必须在布局之前还原程序, 因为标题通常在窗口首次出现时不匹配。

恢复布局时,i3 resurrect使用xdool取消映射并重新映射 工作区上的窗口,它使i3将它们视为新窗口,因此它们将 被占位符窗口吞噬。

i3中的示例配置

一个没有窗口标题匹配的非常基本的设置:

set $i3_resurrect i3-resurrect

# Save workspace mode.
mode "save" {
  bindsym 1 exec $i3_resurrect save -w 1
  bindsym 2 exec $i3_resurrect save -w 2
  bindsym 3 exec $i3_resurrect save -w 3
  bindsym 4 exec $i3_resurrect save -w 4
  bindsym 5 exec $i3_resurrect save -w 5
  bindsym 6 exec $i3_resurrect save -w 6
  bindsym 7 exec $i3_resurrect save -w 7
  bindsym 8 exec $i3_resurrect save -w 8
  bindsym 9 exec $i3_resurrect save -w 9
  bindsym 0 exec $i3_resurrect save -w 0

  # Back to normal: Enter, Escape, or s
  bindsym Return mode "default"
  bindsym Escape mode "default"
  bindsym s mode "default"
  bindsym $mod+s mode "default"
}

bindsym $mod+s mode "save"

# Restore workspace mode.
mode "restore" {
  bindsym 1 exec $i3_resurrect restore -w 1
  bindsym 2 exec $i3_resurrect restore -w 2
  bindsym 3 exec $i3_resurrect restore -w 3
  bindsym 4 exec $i3_resurrect restore -w 4
  bindsym 5 exec $i3_resurrect restore -w 5
  bindsym 6 exec $i3_resurrect restore -w 6
  bindsym 7 exec $i3_resurrect restore -w 7
  bindsym 8 exec $i3_resurrect restore -w 8
  bindsym 9 exec $i3_resurrect restore -w 9
  bindsym 0 exec $i3_resurrect restore -w 0

  # Back to normal: Enter, Escape, or n
  bindsym Return mode "default"
  bindsym Escape mode "default"
  bindsym n mode "default"
  bindsym $mod+n mode "default"
}

bindsym $mod+n mode "restore"

一个更高级的设置,其中窗口与标题匹配:

set $i3_resurrect i3-resurrect

# Save workspace mode.
mode "save" {
  bindsym 1 exec "$i3_resurrect save -w 1 --swallow=class,instance,title"
  bindsym 2 exec "$i3_resurrect save -w 2 --swallow=class,instance,title"
  bindsym 3 exec "$i3_resurrect save -w 3 --swallow=class,instance,title"
  bindsym 4 exec "$i3_resurrect save -w 4 --swallow=class,instance,title"
  bindsym 5 exec "$i3_resurrect save -w 5 --swallow=class,instance,title"
  bindsym 6 exec "$i3_resurrect save -w 6 --swallow=class,instance,title"
  bindsym 7 exec "$i3_resurrect save -w 7 --swallow=class,instance,title"
  bindsym 8 exec "$i3_resurrect save -w 8 --swallow=class,instance,title"
  bindsym 9 exec "$i3_resurrect save -w 9 --swallow=class,instance,title"
  bindsym 0 exec "$i3_resurrect save -w 10 --swallow=class,instance,title"

  # Back to normal: Enter, Escape, or s
  bindsym Return mode "default"
  bindsym Escape mode "default"
  bindsym s mode "default"
  bindsym $mod+s mode "default"
}

bindsym $mod+s mode "save"

# Restore workspace mode.
mode "restore" {
  bindsym 1 exec "$i3_resurrect restore -w 1 --programs-only"
  bindsym 2 exec "$i3_resurrect restore -w 2 --programs-only"
  bindsym 3 exec "$i3_resurrect restore -w 3 --programs-only"
  bindsym 4 exec "$i3_resurrect restore -w 4 --programs-only"
  bindsym 5 exec "$i3_resurrect restore -w 5 --programs-only"
  bindsym 6 exec "$i3_resurrect restore -w 6 --programs-only"
  bindsym 7 exec "$i3_resurrect restore -w 7 --programs-only"
  bindsym 8 exec "$i3_resurrect restore -w 8 --programs-only"
  bindsym 9 exec "$i3_resurrect restore -w 9 --programs-only"
  bindsym 0 exec "$i3_resurrect restore -w 10 --programs-only"

  bindsym $mod+1 exec "$i3_resurrect restore -w 1 --layout-only"
  bindsym $mod+2 exec "$i3_resurrect restore -w 2 --layout-only"
  bindsym $mod+3 exec "$i3_resurrect restore -w 3 --layout-only"
  bindsym $mod+4 exec "$i3_resurrect restore -w 4 --layout-only"
  bindsym $mod+5 exec "$i3_resurrect restore -w 5 --layout-only"
  bindsym $mod+6 exec "$i3_resurrect restore -w 6 --layout-only"
  bindsym $mod+7 exec "$i3_resurrect restore -w 7 --layout-only"
  bindsym $mod+8 exec "$i3_resurrect restore -w 8 --layout-only"
  bindsym $mod+9 exec "$i3_resurrect restore -w 9 --layout-only"
  bindsym $mod+0 exec "$i3_resurrect restore -w 10 --layout-only"

  # Back to normal: Enter, Escape, or n
  bindsym Return mode "default"
  bindsym Escape mode "default"
  bindsym n mode "default"
  bindsym $mod+n mode "default"
}

bindsym $mod+n mode "restore"

使用第二种配置的示例:

第二种配置使用示例

配置

配置文件应该位于~/.config/i3 resurrect/config.json。 首次运行i3 resurrect时将创建默认配置文件。

窗口命令映射

如果窗口中的进程cmdline与 必须运行命令才能启动该程序,可以添加一个显式窗口 配置文件中的命令映射。

例如,gnome终端的进程是gnome终端服务器,但是我们需要 使用命令gnome terminal启动它。为了让它工作,你会 配置文件中的以下内容:

yay -S i3-resurrect-git
0

另一个用例示例如下:

  • 一个应用程序的单个实例有多个窗口
  • 还原时,您只希望启动程序的一个实例 应用程序主窗口的每个实例

在这个场景中,您可以创建一个默认映射 应用程序的窗口类没有命令,另一个窗口类设置命令 如果它也与某个标题匹配:

yay -S i3-resurrect-git
1 提示: 如果需要查找窗口的类/实例,请键入xprop grep wm_class 在终端中,然后单击所需窗口。

端子

对于终端模拟器窗口,我们必须从 第一个子进程(通常这是您的shell)而不是窗口的根 处理(终端模拟器)。

i3 resurrect通过允许您指定终端列表 配置文件中的模拟器窗口类。

例如,如果您同时使用alacrity和gnome终端,并且希望它们 要正确还原工作目录,请将以下内容放入 您的配置文件:

yay -S i3-resurrect-git
2

默认配置中包含一些示例。如果你想让我补充 更多命令映射或终端到默认配置,请打开一个问题 对于它。

每窗口吞咽标准

也可以在每个窗口的基础上配置swallow条件 将覆盖由--swallow命令行参数设置的条件。

示例用例:

  • 我通常希望在swallow标准中包含窗口标题 准确还原布局
  • 在我使用的其他程序中,ario(一个mpd客户端)总是有 在窗口标题中播放歌曲
  • 这使得按窗口标题匹配布局不方便,所以我想 ario始终只与窗口类/实例匹配

这可以通过在配置文件中放入以下内容来实现:

yay -S i3-resurrect-git
3

贡献

有关我们的行为准则以及向我们提交请求的过程的详细信息,请阅读contribution.md

版本控制

我们使用semver进行版本控制。有关可用的版本,请参见此存储库上的标记

使用

构建
  • 单击-用于创建命令行界面
  • i3ipc-用于获取/构建工作区树
  • wmctrl-用于获取使用i3ipc检索的窗口的PID
  • psutil-用于获取每个进程的命令行和cwd
  • xdool-用于取消映射和重新映射窗口

贡献者

另请参见参与此项目的贡献者列表。

致谢

相关项目

对于那些感兴趣的人,我用来快速启动和运行的其他优秀软件包括:

许可证

此项目在GNU GPL版本3下获得许可-有关详细信息,请参见许可文件

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在mybatis中捕获SQLException   java Spring XML自动连线记录器不确定目标类名   JavaSpringDataJPA:使用联接表进行排序和分页   JAVA循环的lang.NullPointerException数组   带Jetty和空密码的java双向SSL   当对象为类型时,java在tableview上显示图像   如何在GWT中从javascript到java获取返回类型“any”?(泛型类型传递)   java从ListView Android中的微调器获取选定值   java缓冲图像中较小的图像被裁剪,我如何解决这个问题?   java Spring MVC 3.1:使用SimpleUrlHandlerMapping和通用基本控制器时的问题   java在每次调用时生成唯一的随机数   java libGDX:在屏幕上移动多边形   java TextView未在应用程序中居中,但在match_约束中居中   在python中重复运行同一java函数的最佳方式是什么?