Current window name in i3status

Given the very tiling nature of i3, the name of the current window can be hard to impossible to decipher, particularly those looooong-ass web page titles.

The following Python script gets said title - doesn’t choke when there is none - and echoes it to a one-line file that can be in turn read by i3status.

See ~/.config/i3/i3status.conf below.

~/.scripts/px-i3-window-name.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3

import os
import i3ipc
from html import escape

TMPFILE = os.path.expanduser("/tmp/.current_window_name.txt")

i3 = i3ipc.Connection()

thisId = 0

root = i3.get_tree()

def writeTmpFile(wname):
if wname:
wname = escape(wname)
else:
wname = 'Unnamed window'

with open(TMPFILE, 'w') as the_file:
the_file.write(wname)

def on_window(i3, e):
global thisId
if e.change == "focus":
thisId = e.container.id
writeTmpFile(e.container.name)
if e.change == "title":
if e.container.id == thisId:
writeTmpFile(e.container.name)

def on_workspace(i3, e):
if e.change == "focus":
writeTmpFile("Workspace " + e.current.name)

i3.on('window', on_window)
i3.on('workspace', on_workspace)

i3.main()

in ~/.config/i3/i3status.conf

~/.config/i3/i3status.conf
1
2
3
4
5
6
7
order += "read_file current_window_name"

read_file current_window_name {
format = "<span foreground='#ff8c00'>%content</span>"
format_bad = ""
path = "/tmp/.current_window_name.txt"
}

Remember to install i3ipc

1
python3 -m pip install i3ipc

NB if you do things the right way, you don’t have to remember all of that. Everything above is automatically installed on each and every of my new machines.

Stay tuned for more about just that - automatic deployment and maintaintance using a single, simple shell script instead of mastodons like Guix - and more.