Debian Codegraph @

vicious/docs/source/format.rst

raw
1 .. _format-func:
2
3 Format Functions
4 ================
5
6 You can use a function instead of a string as the format parameter.
7 Then you are able to check the value returned by the widget type
8 and change it or perform some action. You can change the color of
9 the battery widget when it goes below a certain point, hide widgets
10 when they return a certain value or maybe use ``string.format`` for padding.
11
12 Do not confuse this with just coloring the widget, in those cases
13 standard Pango markup can be inserted into the format string.
14
15 The format function will get the widget as its first argument, table with
16 the values otherwise inserted into the format string as its second argument,
17 and will return the text/data to be used for the widget.
18
19 Examples
20 --------
21
22 Hide mpd widget when no song is playing
23 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24
25 .. code-block:: lua
26
27 mpdwidget = wibox.widget.textbox()
28 vicious.register(
29 mpdwidget,
30 vicious.widgets.mpd,
31 function (widget, args)
32 if args["{state}"] == "Stop" then
33 return ''
34 else
35 return ('<span color="white">MPD:</span> %s - %s'):format(
36 args["{Artist}"], args["{Title}"])
37 end
38 end)
39
40 Use string.format for padding
41 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42
43 .. code-block:: lua
44
45 uptimewidget = wibox.widget.textbox()
46 vicious.register(uptimewidget, vicious.widgets.uptime,
47 function (widget, args)
48 return ("Uptime: %02d %02d:%02d "):format(
49 args[1], args[2], args[3])
50 end, 61)
51
52 When it comes to padding it is also useful to mention how a widget
53 can be configured to have a fixed width. You can set a fixed width on
54 your textbox widgets by changing their ``width`` field (by default width
55 is automatically adapted to text width). The following code forces
56 a fixed width of 50 px to the uptime widget, and aligns its text to the right:
57
58 .. code-block:: lua
59
60 uptimewidget = wibox.widget.textbox()
61 uptimewidget.width, uptimewidget.align = 50, "right"
62 vicious.register(uptimewidget, vicious.widgets.uptime, "$1 $2:$3", 61)
63
64 Stacked graph
65 ^^^^^^^^^^^^^
66
67 Stacked graphs are handled specially by Vicious: ``format`` functions passed
68 to the corresponding widget types must return an array instead of a string.
69
70 .. code-block:: lua
71
72 cpugraph = wibox.widget.graph()
73 cpugraph:set_stack(true)
74 cpugraph:set_stack_colors{ "red", "yellow", "green", "blue" }
75 vicious.register(cpugraph, vicious.widgets.cpu,
76 function (widget, args)
77 return { args[2], args[3], args[4], args[5] }
78 end, 3)
79
80 The snipet above enables graph stacking/multigraph and plots usage of all four
81 CPU cores on a single graph.
82
83 Substitute widget types' symbols
84 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85
86 If you are not happy with default symbols used in volume, battery, cpufreq and
87 other widget types, use your own symbols without any need to modify modules.
88 The following example uses a custom table map to modify symbols representing
89 the mixer state: on or off/mute.
90
91 .. code-block:: lua
92
93 volumewidget = wibox.widget.textbox()
94 vicious.register(volumewidget, vicious.widgets.volume,
95 function (widget, args)
96 local label = { ["🔉"] = "O", ["🔈"] = "M" }
97 return ("Volume: %d%% State: %s"):format(
98 args[1], label[args[2]])
99 end, 2, "PCM")
100
101 .. _call-example:
102
103 Get data from the widget
104 ^^^^^^^^^^^^^^^^^^^^^^^^
105
106 :lua:func:`vicious.call` could be useful for naughty notification and scripts:
107
108 .. code-block:: lua
109
110 mybattery = wibox.widget.textbox()
111 vicious.register(mybattery, vicious.widgets.bat, "$2%", 17, "0")
112 mybattery:buttons(awful.util.table.join(awful.button(
113 {}, 1,
114 function ()
115 naughty.notify{ title = "Battery indicator",
116 text = vicious.call(vicious.widgets.bat,
117 "Remaining time: $3", "0") }
118 end)))
119
120 Format functions can be used as well:
121
122 .. code-block:: lua
123
124 mybattery:buttons(awful.util.table.join(awful.button(
125 {}, 1,
126 function ()
127 naughty.notify{
128 title = "Battery indicator",
129 text = vicious.call(
130 vicious.widgets.bat,
131 function (widget, args)
132 return ("%s: %10sh\n%s: %14d%%\n%s: %12dW"):format(
133 "Remaining time", args[3],
134 "Wear level", args[4],
135 "Present rate", args[5])
136 end, "0") }
137 end)))