/usr/share/awesome/lib/flaw/helper.lua is in awesome-extra 2017110501.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | -- flaw, a Lua OO management framework for Awesome WM widgets.
-- Copyright (C) 2009,2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net>
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- Grab environment.
local io = io
local os = os
local math = math
local pairs = pairs
local string = string
local tostring = tostring
local table = require('table')
local naughty = require('naughty')
local beautiful = require('beautiful')
--- Utilities.
--
-- <p>This module mainly contains utilities borrowed and improved from
-- wicked and other scripts found on the <b>awesome</b> wiki. Many
-- thanks to all the wiki contributors.</p>
--
--
-- @author David Soulayrol <david.soulayrol AT gmail DOT com>
-- @copyright 2009,2010,2011 David Soulayrol
module('flaw.helper')
local unities = { 'b', 'Kb', 'Mb', 'Gb', 'Tb' }
--- File related tools.
--
-- @class table
-- @name file
file = {}
--- Append the given string to a temporary log file.
--
-- @param line the line to write down.
function file.log(line)
if line ~= nil then
local log = io.open('/tmp/awesome_rc.log', 'a')
log:write(os.date('%c\t') .. tostring(line) .. '\n')
log:close()
end
end
--- Load a file with lines formatted as "name: value".
--
-- <p>For each line "k:v" read in the file f, the given table is
-- completed with v for the key "f_k" (everything in lower
-- case). Spaces in k are replaces with underscores.</p>
--
-- @param path the file path.
-- @param name the filename.
-- @param t the table which will hold the parsed values.
-- @return the number of lines parsed, or -1 if the file could not be opened.
function file.load_state_file(path, name, t)
local f = io.open(path .. '/' .. name)
local r = -1
if f ~= nil then
local prefix = name:lower() .. '_'
local line = nil
r = 0
repeat
line = f:read()
if line then
n, v = line:match('([%a%d ]+):%s+([%w%.- ]+)')
if n ~= nil and v ~= nil then
t[prefix .. n:gsub(' ', '_'):lower()] = v:lower()
r = r + 1
end
end
until line == nil
f:close()
end
return r
end
--- Text formatting related functions.
--
-- <p>This family of functions returns the given text formatted with
-- special attributes.</p>
--
-- @class table
-- @name format
format = {}
--- Return the string with a background color.
--
-- @param color the color to apply in background.
-- @param text the text to format.
-- @return The formatted text.
function format.set_bg(color, text)
return '<bg color="'..color..'" />'..text
end
--- Return the string with the given color.
--
-- @param color the color to apply on the text.
-- @param text the text to format.
-- @return The formatted text.
function format.set_fg(color, text)
return '<span color="'..color..'">'..text..'</span>'
end
--- Return the string with background and foreground color.
--
-- @param bgcolor the color to apply in background.
-- @param fgcolor the color to apply on the text.
-- @param text the text to format.
-- @return The formatted text.
function format.set_bg_fg(bgcolor, fgcolor, text)
return '<bg color="'..bgcolor..'" /><span color="'..fgcolor..'">'..text..'</span>'
end
--- Return the string with a defined font.
--
-- @param font the font to apply on the text.
-- @param text the text to format.
-- @return The formatted text.
function format.set_font(font, text)
return '<span font_desc="'..font..'">'..text..'</span>'
end
--- String related functions.
--
-- @class table
-- @name strings
strings = {}
--- Split the given string by whitespaces.
--
-- @param str the string to split.
-- @return a table with the string tokens.
function strings.split(str)
str = str or ''
values = {}
local start = 1
while true do
local splitstart, splitend = string.find(str, ' ', start)
local token = string.sub(str, start, splitstart and splitstart - 1 or nil)
if token:gsub(' ','') ~= '' then
table.insert(values, token)
end
if splitstart == nil then break end
start = splitend+1
splitstart, splitend = string.find(str, ' ', start)
end
return values
end
--- Crop the given string to the given maximum width.
--
-- <p>If string is too large, the right-most part is replaced by an
-- ellipsis.</p>
--
-- @param str the string to crop.
-- @param width the maximum result width.
-- @return the resulting string.
function strings.crop(str, width)
str = str or ''
local len = str:len()
width = width or len
if width < 3 then
str = ''
elseif len > width then
str = str:sub(1, width - 3) .. '...'
end
return str
end
--- Force a fixed width on the given string.
--
-- <p>If the string is too short, it is padded with spaces on the
-- left. If too long, it is cropped without an ellipsis.</p>
--
-- @param str the string to handle.
-- @param width the width to apply.
-- @return the resulting string.
function strings.pad_left(str, width)
str = str or ''
local len = str:len()
width = width or len
if width > len then
for i = 1, width - len do
str = ' ' .. str
end
else
str = str:sub(0, width)
end
return str
end
--- Force a fixed width on the given string.
--
-- <p>If the string is too short, it is padded with spaces on the
-- right. If too long, it is cropped without an ellipsis.</p>
--
-- @param str the string to handle.
-- @param width the width to apply.
-- @return the resulting string.
function strings.pad_right(str, width)
str = str or ''
local len = str:len()
width = width or len
if width > len then
for i = 1, width - len do
str = str .. ' '
end
else
str = str:sub(0, width)
end
return str
end
--- Pad a number to a minimum amount of digits.
--
-- <p>Caveat: the result is wrong for numbers between 0 and 1.</p>
--
-- @param str number the number to handle as a string.
-- @param padding the number of digits to add if necessary.
-- @return the resulting string.
function strings.pad_number(number, padding)
number = number or 0
local str = tostring(number) or ''
padding = padding or str:len()
for i = 1, padding do
if math.floor(number / math.pow(10, (i - 1))) == 0 then
str = '0' .. str
end
end
if number == 0 then
str = str:sub(2)
end
return str
end
--- Fill in a string with given arguments.
--
-- <p>This method replaces the $key patterns in the given string with
-- value got by indexing the given table with key.</p>
--
-- @param pattern the string to translate.
-- @param args the values table.
-- @return the resulting string.
function strings.format(pattern, args)
pattern = pattern or ''
args = args or {}
for key, value in pairs(args) do
if key ~= nil and value ~= nil then
pattern = string.gsub(pattern, '$' .. key, value)
end
end
return pattern
end
-- Convert amount of bytes to string.
function strings.format_bytes(bytes, padding)
bytes = bytes and tonumber(bytes) or ''
padding = padding
local sign = 1
while bytes / 1024 > 1 and unities[sign + 1] ~= nil do
bytes = bytes / 1024
sign = sign + 1
end
bytes = math.floor(bytes * 10)/10
if padding then
bytes = pad_number(bytes * 10, padding + 1)
bytes = bytes:sub(1, bytes:len() - 1) .. '.' .. bytes:sub(bytes:len())
end
return tostring(bytes) .. unities[sign]
end
--- Strip left spaces on a string.
--
-- @param str the string to handle.
-- @return the resulting string.
function strings.lstrip(str)
return str:match("^[ \t]*(.*)$")
end
--- Strip spaces on a string.
--
-- @param str the string to handle.
-- @return the resulting string.
function strings.strip(str)
return str:match("^[ \t]*(.-)[ \t]*$")
end
--- Strip right spaces on a string.
--
-- @param str the string to handle.
-- @return the resulting string.
function strings.rstrip(str)
return str:match("^(.-)[ \t]*$")
end
--- Escape a string
--
-- <p>This method replaces common URL-forbidden characters by their
-- HTML entity.</p>
--
-- @param text the string to handle.
-- @return the resulting string.
function strings.escape(text)
local xml_entities = {
["\""] = """,
["&"] = "&",
["'"] = "'",
["<"] = "<",
[">"] = ">"
}
return text and text:gsub("[\"&'<>]", xml_entities)
end
function round(n, p)
local m = 10^(p or 0)
return math.floor(m*n + 0.5)/m
end
--- Debug related tools.
--
-- @class table
-- @name debug
debug = {}
--- Display a message using a naughty notification.
--
-- @param message the message to display.
function debug.display(message)
naughty.notify{
title = "DEBUG",
text = tostring(message),
timeout = 5
}
end
--- Output a warning on error output.
--
-- @param message the message to log.
function debug.warn(message)
io.stderr:write(os.date('%A %d %B %H:%M:%S') .. ' WARNING: ' .. tostring(message) .. '\n')
end
--- Output an error on error output.
--
-- @param message the message to log.
function debug.error(message)
io.stderr:write(os.date('%A %d %B %H:%M:%S') .. ' ERROR: ' .. tostring(message) .. '\n')
end
|