| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
local io = io |
| 20 |
local os = os |
| 21 |
local math = math |
| 22 |
local pairs = pairs |
| 23 |
local string = string |
| 24 |
local tostring = tostring |
| 25 |
|
| 26 |
local table = require('table') |
| 27 |
|
| 28 |
local naughty = require('naughty') |
| 29 |
local beautiful = require('beautiful') |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
module('flaw.helper') |
| 42 |
|
| 43 |
local unities = { 'b', 'Kb', 'Mb', 'Gb', 'Tb' } |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
file = {} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
function file.log(line) |
| 55 |
if line ~= nil then |
| 56 |
local log = io.open('/tmp/awesome_rc.log', 'a') |
| 57 |
log:write(os.date('%c\t') .. tostring(line) .. '\n') |
| 58 |
log:close() |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
function file.load_state_file(path, name, t) |
| 73 |
local f = io.open(path .. '/' .. name) |
| 74 |
local r = -1 |
| 75 |
|
| 76 |
if f ~= nil then |
| 77 |
local prefix = name:lower() .. '_' |
| 78 |
local line = nil |
| 79 |
r = 0 |
| 80 |
repeat |
| 81 |
line = f:read() |
| 82 |
if line then |
| 83 |
n, v = line:match('([%a%d ]+):%s+([%w%.- ]+)') |
| 84 |
if n ~= nil and v ~= nil then |
| 85 |
t[prefix .. n:gsub(' ', '_'):lower()] = v:lower() |
| 86 |
r = r + 1 |
| 87 |
end |
| 88 |
end |
| 89 |
until line == nil |
| 90 |
f:close() |
| 91 |
end |
| 92 |
return r |
| 93 |
end |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
format = {} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
function format.set_bg(color, text) |
| 110 |
return '<bg color="'..color..'" />'..text |
| 111 |
end |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
function format.set_fg(color, text) |
| 119 |
return '<span color="'..color..'">'..text..'</span>' |
| 120 |
end |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
function format.set_bg_fg(bgcolor, fgcolor, text) |
| 129 |
return '<bg color="'..bgcolor..'" /><span color="'..fgcolor..'">'..text..'</span>' |
| 130 |
end |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
function format.set_font(font, text) |
| 138 |
return '<span font_desc="'..font..'">'..text..'</span>' |
| 139 |
end |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
strings = {} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
function strings.split(str) |
| 152 |
str = str or '' |
| 153 |
values = {} |
| 154 |
|
| 155 |
local start = 1 |
| 156 |
while true do |
| 157 |
local splitstart, splitend = string.find(str, ' ', start) |
| 158 |
local token = string.sub(str, start, splitstart and splitstart - 1 or nil) |
| 159 |
if token:gsub(' ','') ~= '' then |
| 160 |
table.insert(values, token) |
| 161 |
end |
| 162 |
if splitstart == nil then break end |
| 163 |
|
| 164 |
start = splitend+1 |
| 165 |
splitstart, splitend = string.find(str, ' ', start) |
| 166 |
end |
| 167 |
|
| 168 |
return values |
| 169 |
end |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
function strings.crop(str, width) |
| 180 |
str = str or '' |
| 181 |
local len = str:len() |
| 182 |
width = width or len |
| 183 |
if width < 3 then |
| 184 |
str = '' |
| 185 |
elseif len > width then |
| 186 |
str = str:sub(1, width - 3) .. '...' |
| 187 |
end |
| 188 |
return str |
| 189 |
end |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
function strings.pad_left(str, width) |
| 200 |
str = str or '' |
| 201 |
local len = str:len() |
| 202 |
width = width or len |
| 203 |
if width > len then |
| 204 |
for i = 1, width - len do |
| 205 |
str = ' ' .. str |
| 206 |
end |
| 207 |
else |
| 208 |
str = str:sub(0, width) |
| 209 |
end |
| 210 |
return str |
| 211 |
end |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
function strings.pad_right(str, width) |
| 222 |
str = str or '' |
| 223 |
local len = str:len() |
| 224 |
width = width or len |
| 225 |
if width > len then |
| 226 |
for i = 1, width - len do |
| 227 |
str = str .. ' ' |
| 228 |
end |
| 229 |
else |
| 230 |
str = str:sub(0, width) |
| 231 |
end |
| 232 |
return str |
| 233 |
end |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
function strings.pad_number(number, padding) |
| 243 |
number = number or 0 |
| 244 |
local str = tostring(number) or '' |
| 245 |
padding = padding or str:len() |
| 246 |
for i = 1, padding do |
| 247 |
if math.floor(number / math.pow(10, (i - 1))) == 0 then |
| 248 |
str = '0' .. str |
| 249 |
end |
| 250 |
end |
| 251 |
if number == 0 then |
| 252 |
str = str:sub(2) |
| 253 |
end |
| 254 |
return str |
| 255 |
end |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
function strings.format(pattern, args) |
| 266 |
pattern = pattern or '' |
| 267 |
args = args or {} |
| 268 |
for key, value in pairs(args) do |
| 269 |
if key ~= nil and value ~= nil then |
| 270 |
pattern = string.gsub(pattern, '$' .. key, value) |
| 271 |
end |
| 272 |
end |
| 273 |
return pattern |
| 274 |
end |
| 275 |
|
| 276 |
|
| 277 |
function strings.format_bytes(bytes, padding) |
| 278 |
bytes = bytes and tonumber(bytes) or '' |
| 279 |
padding = padding |
| 280 |
local sign = 1 |
| 281 |
while bytes / 1024 > 1 and unities[sign + 1] ~= nil do |
| 282 |
bytes = bytes / 1024 |
| 283 |
sign = sign + 1 |
| 284 |
end |
| 285 |
bytes = math.floor(bytes * 10)/10 |
| 286 |
if padding then |
| 287 |
bytes = pad_number(bytes * 10, padding + 1) |
| 288 |
bytes = bytes:sub(1, bytes:len() - 1) .. '.' .. bytes:sub(bytes:len()) |
| 289 |
end |
| 290 |
return tostring(bytes) .. unities[sign] |
| 291 |
end |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
function strings.lstrip(str) |
| 298 |
return str:match("^[ \t]*(.*)$") |
| 299 |
end |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
function strings.strip(str) |
| 306 |
return str:match("^[ \t]*(.-)[ \t]*$") |
| 307 |
end |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
function strings.rstrip(str) |
| 314 |
return str:match("^(.-)[ \t]*$") |
| 315 |
end |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
function strings.escape(text) |
| 325 |
local xml_entities = { |
| 326 |
["\""] = """, |
| 327 |
["&"] = "&", |
| 328 |
["'"] = "'", |
| 329 |
["<"] = "<", |
| 330 |
[">"] = ">" |
| 331 |
} |
| 332 |
return text and text:gsub("[\"&'<>]", xml_entities) |
| 333 |
end |
| 334 |
|
| 335 |
function round(n, p) |
| 336 |
local m = 10^(p or 0) |
| 337 |
return math.floor(m*n + 0.5)/m |
| 338 |
end |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
debug = {} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
function debug.display(message) |
| 350 |
naughty.notify{ |
| 351 |
title = "DEBUG", |
| 352 |
text = tostring(message), |
| 353 |
timeout = 5 |
| 354 |
} |
| 355 |
end |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
function debug.warn(message) |
| 361 |
io.stderr:write(os.date('%A %d %B %H:%M:%S') .. ' WARNING: ' .. tostring(message) .. '\n') |
| 362 |
end |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
function debug.error(message) |
| 368 |
io.stderr:write(os.date('%A %d %B %H:%M:%S') .. ' ERROR: ' .. tostring(message) .. '\n') |
| 369 |
end |