awful.widget: document taglist_squares_resize and fix
[awesome.git] / lib / awful / util.lua.in
blob5cb5e709bdbc04a0f7aad59c9ec911a0eb842aca
1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
8 local os = os
9 local io = io
10 local assert = assert
11 local loadstring = loadstring
12 local loadfile = loadfile
13 local debug = debug
14 local print = print
15 local type = type
16 local capi =
18 awesome = awesome,
19 mouse = mouse
22 --- Utility module for awful
23 module("awful.util")
25 function deprecate(see)
26 io.stderr:write("W: awful: function is deprecated")
27 if see then
28 io.stderr:write(", see " .. see)
29 end
30 io.stderr:write("\n")
31 io.stderr:write(debug.traceback())
32 end
34 --- Strip alpha part of color.
35 -- @param color The color.
36 -- @return The color without alpha channel.
37 function color_strip_alpha(color)
38 if color:len() == 9 then
39 color = color:sub(1, 7)
40 end
41 return color
42 end
44 --- Make i cycle.
45 -- @param t A length.
46 -- @param i An absolute index to fit into #t.
47 -- @return The object at new index.
48 function cycle(t, i)
49 while i > t do i = i - t end
50 while i < 1 do i = i + t end
51 return i
52 end
54 --- Create a directory
55 -- @param dir The directory.
56 -- @return mkdir return code
57 function mkdir(dir)
58 return os.execute("mkdir -p " .. dir)
59 end
61 --- Spawn a program.
62 -- @param cmd The command.
63 -- @param screen The screen where to spawn window.
64 -- @return The awesome.spawn return value.
65 function spawn(cmd, screen)
66 if cmd and cmd ~= "" then
67 return capi.awesome.spawn(cmd .. "&", screen or capi.mouse.screen)
68 end
69 end
71 -- Read a program output and returns its output as a string.
72 -- @param cmd The command to run.
73 -- @return A string with the program output.
74 function pread(cmd)
75 if cmd and cmd ~= "" then
76 local f = io.popen(cmd, 'r')
77 local s = f:read("*all")
78 f:close()
79 return s
80 end
81 end
83 --- Eval Lua code.
84 -- @return The return value of Lua code.
85 function eval(s)
86 return assert(loadstring("return " .. s))()
87 end
89 --- Escape a string from XML char.
90 -- Useful to set raw text in textbox.
91 -- @param text Text to escape.
92 -- @return Escape text.
93 function escape(text)
94 if text then
95 text = text:gsub("&", "&amp;")
96 text = text:gsub("<", "&lt;")
97 text = text:gsub(">", "&gt;")
98 text = text:gsub("'", "&apos;")
99 text = text:gsub("\"", "&quot;")
101 return text
104 --- Unescape a string from entities.
105 -- @param text Text to unescape.
106 -- @return Unescaped text.
107 function unescape(text)
108 if text then
109 text = text:gsub("&amp;", "&")
110 text = text:gsub("&lt;", "<")
111 text = text:gsub("&gt;", ">")
112 text = text:gsub("&apos;", "'")
113 text = text:gsub("&quot;", "\"")
115 return text
118 --- Check if a file is a Lua valid file.
119 -- This is done by loading the content and compiling it with loadfile().
120 -- @param path The file path.
121 -- @return A function if everything is alright, a string with the error
122 -- otherwise.
123 function checkfile(path)
124 local f, e = loadfile(path)
125 -- Return function if function, otherwise return error.
126 if f then return f end
127 return e
130 --- Try to restart awesome.
131 -- It checks if the configuration file is valid, and then restart if it's ok.
132 -- If it's not ok, the error will be returned.
133 -- @return Never return if awesome restart, or return a string error.
134 function restart()
135 local c = checkfile(capi.awesome.conffile)
137 if type(c) ~= "function" then
138 return c
141 capi.awesome.restart()
144 function getdir(d)
145 if d == "config" then
146 local dir = os.getenv("XDG_CONFIG_HOME")
147 if dir then
148 return dir .. "/awesome"
150 return os.getenv("HOME") .. "/.config/awesome"
151 elseif d == "cache" then
152 local dir = os.getenv("XDG_CACHE_HOME")
153 if dir then
154 return dir .. "/awesome"
156 return os.getenv("HOME").."/.cache/awesome"
159 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80