File: statusbar_external.lua

package info (click to toggle)
ion3-scripts 20050418-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 300 kB
  • ctags: 153
  • sloc: makefile: 18
file content (187 lines) | stat: -rw-r--r-- 5,396 bytes parent folder | download
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
--
-- statusbar_external.lua
--

local settings = {

    -- a simple example which shows utc time and date
    -- in %udate every second with 'date -u'
    {
        template_key = 'udate',
        external_program = 'date -u',
        execute_delay = 1 * 1000,
    },

    -- a more complicated example showing the usage
    -- percentage of hda1 every 30 seconds in %hda1u
    -- with df, grep and sed.
    {
        template_key = 'hda1u',
        external_program =
            'df|grep hda1|sed "s/.*\\(..%\\).*/\\1/"',
        execute_delay = 30 * 1000,
        meter_length = 4,
        hint_regexp = {
            important = '9[456].',
            critical = {
                '9[789].',
                '1...',
            },
        },
    },

}

--
-- -- What is this?
--
-- This is a simple "general" statusbar lua script which
-- lets you show output of external programs in ion
-- statusbar. This is convenient if you are not familiar
-- with lua but can handle yourself with some other
-- language enough to output what you would want the
-- statusbar to show.
--
-- 
-- -- How to use it?
--
-- Short version:
--
-- 1 ) modify settings variable
-- 2 ) update template to include specified keys
-- 3 ) dopath( "statusbar_external" ) in cfg_user.lua
--
--
-- Longer version:
--
-- There is an example settings-variable which can be
-- modified to specify your own programs. The needed
-- parameters are the following :
--
-- * template_key
--  An unique name for the meter. You must add this
--  name with preceding % (for example %meter_name)
--  into your statusbar template which is usually set
--  in cfg_statusbar.lua.
-- 
-- * external_program
--  The program to execute. The last line of each
--  flushed output this program writes to stdout is
--  inserted as the content of the meter.
--
-- * execute_delay
--  The delay to wait before running the program again
--  once it stops sending data. Some programs just
--  send the data and exit while others continue
--  sending data periodically. This value can be used
--  to control the update rate for the first type of
--  programs.
--
-- * meter_length (optional)
--  The space reserved for this meter in characters.
--  If the value is not specified or is less than 1
--  then the space is determined by the length of the
--  data being shown but some people do not want their
--  statusbar width to change randomly.
--
-- * hint_regexp (poptional)
--  These regular expressions (in Lua syntax) are
--  matched with the data and in case of a match the
--  named hint is set to the meter. The value can be
--  either a string containing the regexp or a table
--  containing several possible regexps (Lua regexp
--  doesn't have an 'or'). By default the themes define
--  a color for 'important' and 'critical' hints.
--
-- After this script is loaded it starts executing all
-- the provided programs and updates the statusbar every
-- time one of them prints (and flushes) something.
--
-- Also this is not a normal statusd script so it
-- doesn't get loaded by template name automatically
-- when ion starts. You must add
--
--   dopath( "statusbar_external" )
--
-- to some script that is being loaded in Ion. For
-- example ~/.ion3/cfg_user.lua is a good place. Note
-- that cfg_statusbar.lua is not loaded inside the
-- Ion process but in a separate process and thus this
-- script can not be loaded from that file!
--
-- 

---------------------------------------------------------

local timers = {}
local inform_key, start_execute;

start_execute = function ( key )
    ioncore.popen_bgread(
        settings[key].external_program,
        function ( data ) inform_key( key, data ) end
    )
end

inform_key = function ( key, data )
    if not data then
        timers[key]:set(
            settings[key].execute_delay,
            function () start_execute( key ) end
        )
    else
        -- remove all but the last line
        data = string.gsub( data, "\n$", "" )
        data = string.gsub( data, ".*\n", "" );
        
        local sets = settings[key]
        local t_key = sets.template_key
        local t_length = sets.meter_length
        local h_regexp = sets.hint_regexp

        if not tlength or t_length < 1 then
            t_length = string.len( data )
        end

        mod_statusbar.inform( t_key, data )
        mod_statusbar.inform( t_key .. "_hint", '' )
        mod_statusbar.inform(
            t_key .. "_template",
            string.rep( ' ', t_length )
        )

        if not h_regexp then h_regexp = {} end

        for hint, re in pairs( h_regexp ) do
            if ( type( re ) == 'string' ) then
                re = { re }
            end
            
            for _, r in ipairs( re ) do
                if ( string.find( data, r ) ) then
                    mod_statusbar.inform(
                        t_key .. "_hint", hint
                    )
                end
            end
        end
   
        mod_statusbar.update()
    end
end

-- Start all

for key in pairs(settings) do
    timers[key] = ioncore.create_timer()
    start_execute( key )
end

--
-- Copyright (c) Antti Vhkotamki 2005.
--
-- Ion is free software; you can redistribute it and/or modify it under
-- the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--