File: format.rst

package info (click to toggle)
awesome-extra 2023010601
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,340 kB
  • sloc: cpp: 112; sh: 84; makefile: 25; python: 11
file content (137 lines) | stat: -rw-r--r-- 4,724 bytes parent folder | download | duplicates (2)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
.. _format-func:

Format Functions
================

You can use a function instead of a string as the format parameter.
Then you are able to check the value returned by the widget type
and change it or perform some action.  You can change the color of
the battery widget when it goes below a certain point, hide widgets
when they return a certain value or maybe use ``string.format`` for padding.

Do not confuse this with just coloring the widget, in those cases
standard Pango markup can be inserted into the format string.

The format function will get the widget as its first argument, table with
the values otherwise inserted into the format string as its second argument,
and will return the text/data to be used for the widget.

Examples
--------

Hide mpd widget when no song is playing
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: lua

   mpdwidget = wibox.widget.textbox()
   vicious.register(
       mpdwidget,
       vicious.widgets.mpd,
       function (widget, args)
           if args["{state}"] == "Stop" then
               return ''
           else
               return ('<span color="white">MPD:</span> %s - %s'):format(
                   args["{Artist}"], args["{Title}"])
           end
       end)

Use string.format for padding
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: lua

   uptimewidget = wibox.widget.textbox()
   vicious.register(uptimewidget, vicious.widgets.uptime,
                    function (widget, args)
                        return ("Uptime: %02d %02d:%02d "):format(
                            args[1], args[2], args[3])
                    end, 61)

When it comes to padding it is also useful to mention how a widget
can be configured to have a fixed width.  You can set a fixed width on
your textbox widgets by changing their ``width`` field (by default width
is automatically adapted to text width).  The following code forces
a fixed width of 50 px to the uptime widget, and aligns its text to the right:

.. code-block:: lua

   uptimewidget = wibox.widget.textbox()
   uptimewidget.width, uptimewidget.align = 50, "right"
   vicious.register(uptimewidget, vicious.widgets.uptime, "$1 $2:$3", 61)

Stacked graph
^^^^^^^^^^^^^

Stacked graphs are handled specially by Vicious: ``format`` functions passed
to the corresponding widget types must return an array instead of a string.

.. code-block:: lua

   cpugraph = wibox.widget.graph()
   cpugraph:set_stack(true)
   cpugraph:set_stack_colors{ "red", "yellow", "green", "blue" }
   vicious.register(cpugraph, vicious.widgets.cpu,
                    function (widget, args)
                        return { args[2], args[3], args[4], args[5] }
                    end, 3)

The snipet above enables graph stacking/multigraph and plots usage of all four
CPU cores on a single graph.

Substitute widget types' symbols
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you are not happy with default symbols used in volume, battery, cpufreq and
other widget types, use your own symbols without any need to modify modules.
The following example uses a custom table map to modify symbols representing
the mixer state: on or off/mute.

.. code-block:: lua

   volumewidget = wibox.widget.textbox()
   vicious.register(volumewidget, vicious.widgets.volume,
                    function (widget, args)
                        local label = { ["🔉"] = "O", ["🔈"] = "M" }
                        return ("Volume: %d%% State: %s"):format(
                            args[1], label[args[2]])
                    end, 2, "PCM")

.. _call-example:

Get data from the widget
^^^^^^^^^^^^^^^^^^^^^^^^

:lua:func:`vicious.call` could be useful for naughty notification and scripts:

.. code-block:: lua

   mybattery = wibox.widget.textbox()
   vicious.register(mybattery, vicious.widgets.bat, "$2%", 17, "0")
   mybattery:buttons(awful.util.table.join(awful.button(
       {}, 1,
       function ()
           naughty.notify{ title = "Battery indicator",
                           text = vicious.call(vicious.widgets.bat,
                                               "Remaining time: $3", "0") }
       end)))

Format functions can be used as well:

.. code-block:: lua

   mybattery:buttons(awful.util.table.join(awful.button(
       {}, 1,
       function ()
           naughty.notify{
               title = "Battery indicator",
               text = vicious.call(
                   vicious.widgets.bat,
                   function (widget, args)
                       return ("%s: %10sh\n%s: %14d%%\n%s: %12dW"):format(
                           "Remaining time", args[3],
                           "Wear level", args[4],
                           "Present rate", args[5])
                   end, "0") }
       end)))