- Notifications
You must be signed in to change notification settings - Fork184
The hacker-friendly status_command for Sway and i3
License
vivien/i3blocks
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The hacker-friendlystatus_command
for Sway and i3
i3blocks is a feed generator for text based status bars.It executes your command lines and generates a status line from their output.Commands are scheduled at configured time intervals, upon signal reception or on clicks.
The generated line is meant to be displayed by thei3 window manager through its i3bar component, as an alternative to i3status.This program naturally works out of the box with thesway Wayland compositor since it is a drop-in replacement for i3.
i3blocks is meant to be highly flexible but intuitive.No library package is required, just output what your status bar expects, from your favorite programming language and your preferred format.
[click]full_text=Click me!command=echo"Got clicked with button $button"color=#F79494# Guess the weather hourly[weather]command=curl -Ss'https://wttr.in?0&T&Q' | cut -c 16- | head -2 | xargs echointerval=3600color=#A4C2F4# Query my default IP address only on startup[ip]command=hostname -i | awk'{ print "IP:" $1 }'interval=oncecolor=#91E78B# Update time every 5 seconds[time]command=date +%Tinterval=5
i3blocks is already packaged for:
Archlinux:i3blocks in the community repository andi3blocks-git in the AUR
Gentoo:x11-misc/i3blocks
Debian:i3blocks
Ubuntu:i3blocks
Fedora (COPR):i3blocks
Void Linux:i3blocks
Or can be installed from source with:
git clone https://github.com/vivien/i3blockscd i3blocks./autogen.sh./configuremakemake install
In your i3 configuration file, define i3blocks as thestatus line command of a new bar block:
bar { status_command i3blocks}
Important | The project’s repository does not include default scripts anymore. |
For the lazy, you can start fromour collection of scripts:
git clone https://github.com/vivien/i3blocks-contrib ~/.config/i3blockscd !$cp config.example config
For the picky, you can start a configuration file in one of the following preferred paths:
$XDG_CONFIG_HOME/i3blocks/config
(or~/.config/i3blocks/config
);~/.i3blocks.conf
;$XDG_CONFIG_DIRS/i3blocks/config
(or/etc/xdg/i3blocks/config
);/etc/i3blocks.conf
;or any other path that you will specify using the
-c
option.
Note | By default/etc is prefixed by/usr/local when you installed from source. |
Use the example above or dig in the configuration details below.
Now restart i3 withi3-msg restart
to apply your changes.
The configuration file uses a simplified INI file format:
# Properties not preceded by a section are considered global# and merged into every section declarations.foo=bar[block1]baz=qux# This is a comment[block2]quux= quuz
In this example,block2 contains afoo property equal to"bar" and aquux property equal to" quuz" (including the leading space).Everything after the equal sign will be part of the value, thus inline comments won’t be stripped out.
At runtime, these properties are simply variables, that are passed along to the status bar program when printing is necessary.However on startup, i3blocks checks some optional properties to eventually setup the scheduling of a command.
If a block specifies a command, then all of its properties are passed as environment variables at execution, which means that thefoo=bar property will be available from a shell script with$foo
.The output of the command is used to update the values of these variables.The values are reset to default (as defined in the configuration file) before the update, so that blocks get a consistent behavior at each execution.
Note | Each line from a block output must be terminated with a newline. |
In order to use i3blocks with i3, its status bar commandi3bar expects specific keys.To know how to customize the blocks of your status line, you must refer to thei3bar protocol.
Note | full_text is the only mandatory key, the block will be skipped if this key is absent or empty. |
Unless overriden, the section name of the block defines thename key.
Below are examples of static blocks interacting withi3bar.
[simple]full_text=This is a looong white on red textshort_text=Short white on red textbackground=#FF0000color=#FFFFFF# Block with a fixed width[aligned]full_text=Here.min_width=100align=center# Fancy text with multiple colors and shapes[funky]full_text=<spanforeground="red"size="x-large">Roses</span> and <i><spancolor="#EE37B8">violets</span></i>!markup=pango
These are some special properties checked by i3blocks on startup.These will be considered as simple variables at runtime.
The optionalcommand property specifies a command line to be executed withsh -c
.The command can be relative to the configuration file where it is defined.If the command outputs some text, it is used to update the block.
An exit code of 0 means success.A special exit code of33 will set theurgent i3bar key to true.Any other exit code will raise an error.
[pacman]full_text=c ·command=echo"· ${full_text~~}"color=#FFFF00
The optionalinterval property specifies when the command must be scheduled.
A positive value represents the number of seconds to wait between exectutions.
# Print seconds since 1970-01-01[epoch]command=date +%sinterval=1
A value of0 (or undefined) means the command is not timed whatsoever and will not be executed on startup.This is useful to trigger the command only on user input (e.g. signal or click), not before.
# Restart i3 on click[restart]full_text=Restartcommand=i3-msg -q restart#interval=0
The interval valueonce (or-1) will schedule the command only on startup.This tells i3blocks not to schedule the command again on a time basis.But events such as signals and clicks will execute the command again of course.
# Fetch the public IP address only on startup[public-ip]command=wget -qO - icanhazip.cominterval=once
The interval valuerepeat (or-2) will respawn the command as soon as it terminates.This is convenient for blocking programs which exit as soon as the awaited event arises.
Note | clicks are not supported with this value, since such commands are unlikely to expect data on their standard input. |
# Print the last command entered in Bash[history]command=inotifywait -qq -e close_write ~/.bash_history; tail -1 ~/.bash_historyinterval=repeat
The interval valuepersist (or-3) expects the command to be an infinite loop.Each line of the output will trigger an update of the block.
[window]command=xtitle -sinterval=persist
Blocks can be scheduled upon reception of a real-time signal (think prioritized and queueable).The range of available signal numbers is1 toN, whereSIGRTMIN+N = SIGRTMAX.(Note: there are 31 real-time signals in Linux.)
[caps-lock]command=xset -q | grep Caps | awk'{ print $2, $3, $4 }'interval=oncesignal=10
This example block above will be scheduled once i3blocks handles theSIGRTMIN+10 signal.This can be sent directly from an i3 binding on Caps Lock release with the following configuration:
bindsym --release Caps_Lock exec pkill -SIGRTMIN+10 i3blocks
There are several formats supported to specify which variables i3blocks must update.Some favor simplicity over flexibility but thus can be limited.
When undefined, a raw format is assumed.Each line of the output corresponds to an i3bar key, in the order of definition found in thei3bar protocol:
the 1st line updates thefull_text;
the 2nd line updates theshort_text;
the 3rd line updates thecolor;
the 4th line updates thebackground.
Excess lines are considered an error.Below is an example of a simple battery script.
#!/bin/bashBAT=$(acpi -b| grep -E -o'[0-9][0-9]?%')# Full and short textsecho"Battery:$BAT"echo"BAT:$BAT"# Set urgent flag below 5% or use orange below 20%[${BAT%?}-le 5 ]&&exit 33[${BAT%?}-le 20 ]&&echo"#FF8000"exit 0
[battery]command=battery.shinterval=10
Thejson format can update any variable.
[counter]_count=0command=printf'{"full_text":"Counter: %s", "_count":%d}\n' $_count $((_count + 1))format=jsoninterval=1
When you click on a block, data such as the button number and coordinates are merged into the block variables.
Note | name andinstance are the two keys used by i3bar to identify a block. |
The data sent on click is detailed in thei3bar protocol.
If the block command isn’t already spawned, it is executed again.
# Print click data[clickme]align=centerfull_text=Click me!min_width=Button=?x=?y=?command=echo"Button=$button x=$x y=$y"
If the value of the block’s interval ispersist, then the data is written on the command standard input, one line per click.What gets written depends on the block’s format.The raw format only gets the click button.The JSON format gets all block variables.
[click-loop]full_text=Click me!command=while read button; do echo "Got click $button"; doneinterval=persist[click-loop-json]full_text=Click me!command=ruby -r json -n -e'$_ = JSON.parse($_)' -e'$_["full_text"] = "Click %s at (%d,%d)" % $_.slice("button", "x", "y").values' -e'puts JSON.dump($_)' -e'STDOUT.flush'interval=persistformat=json
Frequently Asked Questions and Troubleshooting.
What is a blocklet?
A blocklet is the configuration of a single block, part of the status line.There are plenty listed in theblocklets page.
Can I use my own variables?
Yes, any variable defined in the block is exported as is to the environment of its command.The
foo=bar
property can be accessed with$foo
from a shell script,ENV["foo"]
from Ruby, and so on.The IEEE and The Open Group state that"The name space of environment variable names containing lowercase letters is reserved for applications.".i3bar suggests to prefix your own keys with an underscore (
_
), but it might be more intuitive to use uppercase environment variables, so it is your call to define your own naming convention.Why
$foo
doesn’t work from the configuration file?i3blocks does not do string interpolation of any sort.The definitions found in the configuration file are just raw strings, this means that
bar=$baz
defines abar variable equal to literally$baz
(a dollar sign followed by "baz").String interpolation does work in thecommand property though, since it is interpreted by a shell which has access to the environment variables.
How can I simulate a button?
This is pretty straightforward actually.Just make sure not to override thefull_text, for example:
[calc-button]full_text=Calculatorcommand=gnome-calculator >/dev/null
Can a block start a GUI application?
Sure.And if you do not wish your command to block until the application is closed, ask i3 to start it for you with
i3-msg -q exec myapp
.Why Pango isn’t working?
The Pango markup requires a Pango font.Make sure you configuredi3bar to use a Pango font.For example:
font pango:Inconsolata, Icons 12
Why is
printf
not working?As of i3blocks 1.5, each line from a block output are expected to be terminated with a newline, e.g. with
printf "…\n"
from a shell script.Why is the output from my persistent block not displayed?
Make sure to flush stdout, for example:
[ruby-loop]full_text=Click mecommand=ruby -p -e'$_.prepend("Got button ")' -e'STDOUT.flush'interval=persist
Can I use a time interval below 1 second?
No, the time unit for interval is the second.
But even though I wouldn’t recommend it, you can still update faster than that with loops:
[nano1]command=sleep .5; date +%Ninterval=repeat[nano2]command=while sleep .5; do date +%N; doneinterval=persist
Can I change the block separator?
Not with i3blocks itself, separators are drawn by i3bar.You can change theseparator_symbol in thei3bar configuration.
Alternatively, you can define static blocks as custom separators in your i3blocks configuration.In the example below, we use the"\xe3\x80\x89" UTF-8 character:
# Define the custom separator in global properties for boilerplatefull_text=〉align=centercolor=#666666separator=falseseparator_block_width=7[time]instance=laTZ=America/Los_Angelescommand=date +%Tinterval=5[separator][time]instance=ncTZ=Pacific/Noumeacommand=date +%Tinterval=5[separator][time]instance=mtlTZ=America/Montrealcommand=date +%Tinterval=5
The log level can be increased with the-v
option.
If your window manager (and thus this program) is run via systemd, you can inspect the program outputs withjournalctl -t <identifier> -f
.You may also use this in conjonction with running the program manually withsystemd-cat -t <identifier> ./i3blocks
.
Alternatively you can redirect the standard output and error streams from the program invokation with:
bar { status_command 2>/tmp/i3blocks.err /path/to/i3blocks -vvv -c /path/to/config | tee /tmp/i3blocks.out}
And inspect the log withtail -f /tmp/i3blocks.err
.
See themanpage for details about the command line options and i3blocks usage.
About
The hacker-friendly status_command for Sway and i3