]> git.sur5r.net Git - i3/i3.github.io/blob - docs/4.13/user-contributed/conky-i3bar.html
save docs for 4.13
[i3/i3.github.io] / docs / 4.13 / user-contributed / conky-i3bar.html
1 ---
2 layout: default
3 title: Docs
4 group: Docs
5 ---
6 <div id="content" class="usergen">
7 <h1>User-contributed article: Using conky with i3bar</h1>
8
9 <p>
10 As explained in the i3 documentation, it’s possible to <a
11 href="http://i3wm.org/docs/userguide.html#_displaying_a_status_line">use an
12 external program to build the status information displayed
13 by i3bar</a>.
14 One of those programs is <a href="http://conky.sourceforge.net/">conky, a free,
15 light-weight system monitor</a>.
16 </p>
17
18 <p>
19 Since conky does not support the i3bar protocol by default, we could use the
20 plain text output format, but then we cannot have i3bar display colors. This
21 article shows how to configure conky to have colors with i3bar.
22 </p>
23
24 <p>
25 As explained in more detail in the <a
26 href="http://i3wm.org/docs/i3bar-protocol.html">i3bar protocol description</a>,
27 i3bar can receive input serialized as JSON, and this way has color support,
28 among other things (urgency flag, shortening description if more space is needed).
29 </p>
30
31 <h2>Example of colored status with conky</h2>
32
33 <p>
34 First of all we have to understand how i3bar expects JSON input:
35 We have to provide an "infinite array", each element on that array is a "status
36 line" that is displayed at a certain moment by i3bar.
37 </p>
38
39 <p>
40 Let's make a short example. 
41 We want to build a very simple status that displays the amunt of free space in
42 home folder and the percentage of used RAM.
43 Here's a sample JSON text for achieving our goal:
44 </p>
45
46 <pre><tt>{"version":1}
47 [
48  [].
49
50  [ { "full_text":"Home 84.0G Free", "color":"#ffffff" },
51    { "full_text":"RAM 32%" , "color":"#ffffff" } ],
52
53  [ { "full_text":"Home 84.0G Free", "color":"#ffffff" },
54    { "full_text":"RAM 34%" , "color":"#ffffff" } ],
55
56  [....],
57  [....],
58  ...</tt></pre>
59
60 <p>
61 The first line tells i3bar that we are using JSON input.
62 The second line is the opening of the "infinite array".
63 Each line after second is one "instance status" to be displayed in i3bar, 
64 </p>
65
66 <h2>Wrapper script</h2>
67
68 <p>
69 To obtain this output with conky we have to use it in combination with a short script.
70 The script will write down the "fixed" part of the output (first and second line), then it will call conky.
71 It will be conky’s duty to write each of the "instances".
72 </p>
73
74 <p>
75 The script is quite simple:
76 </p>
77
78 <pre><tt>#!/bin/sh
79
80 # Send the header so that i3bar knows we want to use JSON:
81 echo '{"version":1}'
82
83 # Begin the endless array.
84 echo '['
85
86 # We send an empty first array of blocks to make the loop simpler:
87 echo '[],'
88
89 # Now send blocks with information forever:
90 exec conky -c $HOME/.conkyrc</tt></pre>
91
92 <p>
93 Let's save this script as <tt>conky-i3bar</tt> in <tt>$HOME/bin</tt>.
94 In the i3 config file the <tt>bar</tt> config will be something like that
95 </p>
96
97 <pre><tt>bar {
98     status_command $HOME/bin/conky-i3bar
99 }</tt></pre>
100
101 <h2>conky configuration</h2>
102
103 <p>
104 Now we have to write a <tt>~/.conkyrc</tt> file in order to obtain the desired status:
105 </p>
106
107 <pre><tt>[{ "full_text":"Home 84.0G Free" , "color":"#ffffff" },
108  { "full_text":"RAM 32%" , "color":"#ffffff" }],</tt></pre>
109
110 <p>
111 Here's a sample conkyrc that updates every 2 seconds. Just to make things a litte bit more exciting 
112 we put a condition in the script in order to write the occupied RAM in red if the amount is more than 90%:
113 </p>
114
115 <pre><tt>out_to_x no
116 own_window no
117 out_to_console yes
118 background no
119 max_text_width 0
120
121 # Update interval in seconds
122 update_interval 2.0
123
124 # This is the number of times Conky will update before quitting.
125 # Set to zero to run forever.
126 total_run_times 0
127
128 # Shortens units to a single character (kiB->k, GiB->G, etc.). Default is off.
129 short_units yes
130
131 # How strict should if_up be when testing an interface for being up?
132 # The value is one of up, link or address, to check for the interface
133 # being solely up, being up and having link or being up, having link
134 # and an assigned IP address. 
135 if_up_strictness address
136
137 # Add spaces to keep things from moving about?  This only affects certain objects.
138 # use_spacer should have an argument of left, right, or none
139 use_spacer left
140
141 # Force UTF8? note that UTF8 support required XFT
142 override_utf8_locale no
143
144 # number of cpu samples to average
145 # set to 1 to disable averaging
146 cpu_avg_samples 2
147
148 # Stuff after 'TEXT' will be formatted on screen
149 TEXT
150
151 # JSON for i3bar
152 {% raw %}
153  [{ "full_text" : "Home ${fs_free /home} Free" , "color" : "\#ffffff" },
154   { "full_text" : "RAM ${memperc}%" , "color" :
155     ${if_match ${memperc}<90}"\#ffffff"${else}"\#ff0000"${endif} }],
156 {% endraw %}
157 </tt></pre>