Move string functions out of awful.util into new gears.string (#1584)

Update deprecated awful.util string function calls to gears.string calls
This commit is contained in:
Kevin Zander 2017-03-11 17:57:32 -06:00 committed by Emmanuel Lepage Vallée
parent 2d91d49b30
commit 2f105eac86
13 changed files with 174 additions and 85 deletions

View File

@ -13,6 +13,7 @@ local capi = {
} }
local awful = require("awful") local awful = require("awful")
local gtable = require("gears.table") local gtable = require("gears.table")
local gstring = require("gears.string")
local wibox = require("wibox") local wibox = require("wibox")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local dpi = beautiful.xresources.apply_dpi local dpi = beautiful.xresources.apply_dpi
@ -329,7 +330,7 @@ function widget.new(args)
joined_descriptions = joined_descriptions .. key.description .. (i~=#keys and "\n" or "") joined_descriptions = joined_descriptions .. key.description .. (i~=#keys and "\n" or "")
end end
-- +1 for group label: -- +1 for group label:
local items_height = awful.util.linecount(joined_descriptions) * line_height + group_label_height local items_height = gstring.linecount(joined_descriptions) * line_height + group_label_height
local current_column local current_column
local available_height_px = max_height_px local available_height_px = max_height_px
local add_new_column = true local add_new_column = true
@ -392,7 +393,7 @@ function widget.new(args)
end end
-- +1 for group label: -- +1 for group label:
current_column.height_px = (current_column.height_px or 0) + current_column.height_px = (current_column.height_px or 0) +
awful.util.linecount(joined_labels)*line_height + group_label_height gstring.linecount(joined_labels)*line_height + group_label_height
if _add_new_column then if _add_new_column then
table.insert(column_layouts, current_column) table.insert(column_layouts, current_column)
end end

View File

@ -10,7 +10,7 @@
local wibox = require("wibox") local wibox = require("wibox")
local button = require("awful.button") local button = require("awful.button")
local util = require("awful.util") local gstring = require("gears.string")
local gtable = require("gears.table") local gtable = require("gears.table")
local spawn = require("awful.spawn") local spawn = require("awful.spawn")
local tags = require("awful.tag") local tags = require("awful.tag")
@ -552,7 +552,7 @@ function menu.entry(parent, args) -- luacheck: no unused args
local key = '' local key = ''
label:set_font(args.theme.font) label:set_font(args.theme.font)
label:set_markup(string.gsub( label:set_markup(string.gsub(
util.escape(args.text), "&(%w)", gstring.xml_escape(args.text), "&(%w)",
function (l) function (l)
key = string.lower(l) key = string.lower(l)
return "<u>" .. l .. "</u>" return "<u>" .. l .. "</u>"

View File

@ -128,6 +128,7 @@ local akey = require("awful.key")
local debug = require('gears.debug') local debug = require('gears.debug')
local gtable = require("gears.table") local gtable = require("gears.table")
local gcolor = require("gears.color") local gcolor = require("gears.color")
local gstring = require("gears.string")
local prompt = {} local prompt = {}
@ -278,20 +279,20 @@ local function prompt_text_with_cursor(args)
local underline = args.cursor_ul or "none" local underline = args.cursor_ul or "none"
if args.selectall then if args.selectall then
if #text == 0 then char = " " else char = util.escape(text) end if #text == 0 then char = " " else char = gstring.xml_escape(text) end
spacer = " " spacer = " "
text_start = "" text_start = ""
text_end = "" text_end = ""
elseif #text < args.cursor_pos then elseif #text < args.cursor_pos then
char = " " char = " "
spacer = "" spacer = ""
text_start = util.escape(text) text_start = gstring.xml_escape(text)
text_end = "" text_end = ""
else else
char = util.escape(text:sub(args.cursor_pos, args.cursor_pos)) char = gstring.xml_escape(text:sub(args.cursor_pos, args.cursor_pos))
spacer = " " spacer = " "
text_start = util.escape(text:sub(1, args.cursor_pos - 1)) text_start = gstring.xml_escape(text:sub(1, args.cursor_pos - 1))
text_end = util.escape(text:sub(args.cursor_pos + 1)) text_end = gstring.xml_escape(text:sub(args.cursor_pos + 1))
end end
local cursor_color = gcolor.ensure_pango_color(args.cursor_color) local cursor_color = gcolor.ensure_pango_color(args.cursor_color)

View File

@ -16,6 +16,7 @@ local pairs = pairs
local type = type local type = type
local gtable = require("gears.table") local gtable = require("gears.table")
local string = string local string = string
local gstring = require("gears.string")
local grect = require("gears.geometry").rectangle local grect = require("gears.geometry").rectangle
local Gio = require("lgi").Gio local Gio = require("lgi").Gio
local gcolor = require("gears.color") local gcolor = require("gears.color")
@ -137,22 +138,27 @@ function util.eval(s)
return assert(load(s))() return assert(load(s))()
end end
local xml_entity_names = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
--- Escape a string from XML char. --- Escape a string from XML char.
-- Useful to set raw text in textbox. -- Useful to set raw text in textbox.
-- @deprecated util.escape
-- @param text Text to escape. -- @param text Text to escape.
-- @return Escape text. -- @return Escape text.
-- @see gears.string
function util.escape(text) function util.escape(text)
return text and text:gsub("['&<>\"]", xml_entity_names) or nil util.deprecate("gears.string.xml_escape", {deprecated_in=5})
return gstring.xml_escape(text)
end end
local xml_entity_chars = { lt = "<", gt = ">", nbsp = " ", quot = "\"", apos = "'", ndash = "-", mdash = "-",
amp = "&" };
--- Unescape a string from entities. --- Unescape a string from entities.
-- @deprecated util.unescape
-- @param text Text to unescape. -- @param text Text to unescape.
-- @return Unescaped text. -- @return Unescaped text.
-- @see gears.string
function util.unescape(text) function util.unescape(text)
return text and text:gsub("&(%a+);", xml_entity_chars) or nil util.deprecate("gears.string.xml_unescape", {deprecated_in=5})
return gstring.xml_unescape(text)
end end
--- Check if a file is a Lua valid file. --- Check if a file is a Lua valid file.
@ -380,30 +386,27 @@ function util.table.hasitem(t, item)
end end
--- Split a string into multiple lines --- Split a string into multiple lines
-- @deprecated util.linewrap
-- @param text String to wrap. -- @param text String to wrap.
-- @param width Maximum length of each line. Default: 72. -- @param width Maximum length of each line. Default: 72.
-- @param indent Number of spaces added before each wrapped line. Default: 0. -- @param indent Number of spaces added before each wrapped line. Default: 0.
-- @return The string with lines wrapped to width. -- @return The string with lines wrapped to width.
-- @see gears.string
function util.linewrap(text, width, indent) function util.linewrap(text, width, indent)
text = text or "" util.deprecate("gears.string.linewrap", {deprecated_in=5})
width = width or 72
indent = indent or 0
local pos = 1 return gstring.linewrap(text, width, indent)
return text:gsub("(%s+)()(%S+)()",
function(_, st, word, fi)
if fi - pos > width then
pos = st
return "\n" .. string.rep(" ", indent) .. word
end
end)
end end
--- Count number of lines in a string --- Count number of lines in a string
-- @deprecated util.linecount
-- @tparam string text Input string. -- @tparam string text Input string.
-- @treturn int Number of lines. -- @treturn int Number of lines.
-- @see gears.string
function util.linecount(text) function util.linecount(text)
return select(2, text:gsub('\n', '\n')) + 1 util.deprecate("gears.string.linecount", {deprecated_in=5})
return gstring.linecount(text)
end end
--- Get a sorted table with all integer keys from a table --- Get a sorted table with all integer keys from a table
@ -485,23 +488,22 @@ end
-- Escape all special pattern-matching characters so that lua interprets them -- Escape all special pattern-matching characters so that lua interprets them
-- literally instead of as a character class. -- literally instead of as a character class.
-- Source: http://stackoverflow.com/a/20778724/15690 -- Source: http://stackoverflow.com/a/20778724/15690
-- @deprecated util.quote_pattern
-- @see gears.string
function util.quote_pattern(s) function util.quote_pattern(s)
-- All special characters escaped in a string: %%, %^, %$, ... util.deprecate("gears.string.quote_pattern", {deprecated_in=5})
local patternchars = '['..("%^$().[]*+-?"):gsub("(.)", "%%%1")..']'
return string.gsub(s, patternchars, "%%%1") return gstring.quote_pattern(s)
end end
-- Generate a pattern matching expression that ignores case. -- Generate a pattern matching expression that ignores case.
-- @param s Original pattern matching expression. -- @param s Original pattern matching expression.
-- @deprecated util.query_to_pattern
-- @see gears.string
function util.query_to_pattern(q) function util.query_to_pattern(q)
local s = util.quote_pattern(q) util.deprecate("gears.string.query_to_pattern", {deprecated_in=5})
-- Poor man's case-insensitive character matching.
s = string.gsub(s, "%a", return gstring.query_to_pattern(q)
function (c)
return string.format("[%s%s]", string.lower(c),
string.upper(c))
end)
return s
end end
--- Round a number to an integer. --- Round a number to an integer.

View File

@ -15,13 +15,13 @@ local pairs = pairs
local ipairs = ipairs local ipairs = ipairs
local table = table local table = table
local common = require("awful.widget.common") local common = require("awful.widget.common")
local util = require("awful.util")
local tag = require("awful.tag") local tag = require("awful.tag")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local fixed = require("wibox.layout.fixed") local fixed = require("wibox.layout.fixed")
local surface = require("gears.surface") local surface = require("gears.surface")
local timer = require("gears.timer") local timer = require("gears.timer")
local gcolor = require("gears.color") local gcolor = require("gears.color")
local gstring = require("gears.string")
local function get_screen(s) local function get_screen(s)
return s and capi.screen[s] return s and capi.screen[s]
@ -330,9 +330,9 @@ function taglist.taglist_label(t, args)
text = "<span font_desc='"..font.."'>" text = "<span font_desc='"..font.."'>"
if fg_color then if fg_color then
text = text .. "<span color='" .. gcolor.ensure_pango_color(fg_color) .. text = text .. "<span color='" .. gcolor.ensure_pango_color(fg_color) ..
"'>" .. (util.escape(t.name) or "") .. "</span>" "'>" .. (gstring.xml_escape(t.name) or "") .. "</span>"
else else
text = text .. (util.escape(t.name) or "") text = text .. (gstring.xml_escape(t.name) or "")
end end
text = text .. "</span>" text = text .. "</span>"
end end

View File

@ -37,11 +37,11 @@ local setmetatable = setmetatable
local table = table local table = table
local common = require("awful.widget.common") local common = require("awful.widget.common")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local util = require("awful.util")
local tag = require("awful.tag") local tag = require("awful.tag")
local flex = require("wibox.layout.flex") local flex = require("wibox.layout.flex")
local timer = require("gears.timer") local timer = require("gears.timer")
local gcolor = require("gears.color") local gcolor = require("gears.color")
local gstring = require("gears.string")
local function get_screen(s) local function get_screen(s)
return s and screen[s] return s and screen[s]
@ -264,9 +264,10 @@ local function tasklist_label(c, args, tb)
if not disable_task_name then if not disable_task_name then
if c.minimized then if c.minimized then
name = name .. (util.escape(c.icon_name) or util.escape(c.name) or util.escape("<untitled>")) name = name .. (gstring.xml_escape(c.icon_name) or gstring.xml_escape(c.name) or
gstring.xml_escape("<untitled>"))
else else
name = name .. (util.escape(c.name) or util.escape("<untitled>")) name = name .. (gstring.xml_escape(c.name) or gstring.xml_escape("<untitled>"))
end end
end end

View File

@ -20,6 +20,7 @@ return
geometry = require("gears.geometry"); geometry = require("gears.geometry");
math = require("gears.math"); math = require("gears.math");
table = require("gears.table"); table = require("gears.table");
string = require("gears.string");
} }
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

87
lib/gears/string.lua Normal file
View File

@ -0,0 +1,87 @@
---------------------------------------------------------------------------
--- String module for gears
--
-- @module gears.string
---------------------------------------------------------------------------
local gstring = {}
local xml_entity_names = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };
--- Escape a string from XML char.
-- Useful to set raw text in textbox.
-- @class function
-- @name escape
-- @param text Text to escape.
-- @return Escape text.
function gstring.xml_escape(text)
return text and text:gsub("['&<>\"]", xml_entity_names) or nil
end
local xml_entity_chars = { lt = "<", gt = ">", nbsp = " ", quot = "\"", apos = "'", ndash = "-", mdash = "-",
amp = "&" };
--- Unescape a string from entities.
-- @class function
-- @name unescape
-- @param text Text to unescape.
-- @return Unescaped text.
function gstring.xml_unescape(text)
return text and text:gsub("&(%a+);", xml_entity_chars) or nil
end
--- Count number of lines in a string
-- @class function
-- @name linecount
-- @tparam string text Input string.
-- @treturn int Number of lines.
function gstring.linecount(text)
return select(2, text:gsub('\n', '\n')) + 1
end
--- Split a string into multiple lines
-- @class function
-- @name linewrap
-- @param text String to wrap.
-- @param width Maximum length of each line. Default: 72.
-- @param indent Number of spaces added before each wrapped line. Default: 0.
-- @return The string with lines wrapped to width.
function gstring.linewrap(text, width, indent)
text = text or ""
width = width or 72
indent = indent or 0
local pos = 1
return text:gsub("(%s+)()(%S+)()",
function(_, st, word, fi)
if fi - pos > width then
pos = st
return "\n" .. string.rep(" ", indent) .. word
end
end)
end
--- Escape all special pattern-matching characters so that lua interprets them
-- literally instead of as a character class.
-- Source: http://stackoverflow.com/a/20778724/15690
-- @class function
-- @name quote_pattern
function gstring.quote_pattern(s)
-- All special characters escaped in a string: %%, %^, %$, ...
local patternchars = '['..("%^$().[]*+-?"):gsub("(.)", "%%%1")..']'
return string.gsub(s, patternchars, "%%%1")
end
--- Generate a pattern matching expression that ignores case.
-- @param s Original pattern matching expression.
-- @class function
-- @name query_to_pattern
function gstring.query_to_pattern(q)
local s = gstring.quote_pattern(q)
-- Poor man's case-insensitive character matching.
s = string.gsub(s, "%a",
function (c)
return string.format("[%s%s]", string.lower(c),
string.upper(c))
end)
return s
end
return gstring

View File

@ -31,6 +31,7 @@ local common = require("awful.widget.common")
local theme = require("beautiful") local theme = require("beautiful")
local wibox = require("wibox") local wibox = require("wibox")
local gcolor = require("gears.color") local gcolor = require("gears.color")
local gstring = require("gears.string")
local function get_screen(s) local function get_screen(s)
return s and capi.screen[s] return s and capi.screen[s]
@ -224,7 +225,7 @@ end
local function menulist_update(scr) local function menulist_update(scr)
local query = instance.query or "" local query = instance.query or ""
shownitems = {} shownitems = {}
local pattern = awful.util.query_to_pattern(query) local pattern = gstring.query_to_pattern(query)
-- All entries are added to a list that will be sorted -- All entries are added to a list that will be sorted
-- according to the priority (first) and weight (second) of its -- according to the priority (first) and weight (second) of its

View File

@ -20,6 +20,7 @@ local glib = lgi.GLib
local wibox = require("wibox") local wibox = require("wibox")
local debug = require("gears.debug") local debug = require("gears.debug")
local protected_call = require("gears.protected_call") local protected_call = require("gears.protected_call")
local gstring = require("gears.string")
local utils = {} local utils = {}
@ -305,7 +306,7 @@ end
-- @tparam number|screen s Screen -- @tparam number|screen s Screen
-- @treturn int Text width. -- @treturn int Text width.
function utils.compute_text_width(text, s) function utils.compute_text_width(text, s)
return utils.compute_textbox_width(wibox.widget.textbox(awful_util.escape(text)), s) return utils.compute_textbox_width(wibox.widget.textbox(gstring.escape(text)), s)
end end
return utils return utils

View File

@ -118,7 +118,7 @@ end
--- Set the text of the textbox (with --- Set the text of the textbox (with
-- [Pango markup](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html)). -- [Pango markup](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html)).
-- @tparam string text The text to set. This can contain pango markup (e.g. -- @tparam string text The text to set. This can contain pango markup (e.g.
-- `<b>bold</b>`). You can use `awful.util.escape` to escape -- `<b>bold</b>`). You can use `gears.string.escape` to escape
-- parts of it. -- parts of it.
-- @treturn[1] boolean true -- @treturn[1] boolean true
-- @treturn[2] boolean false -- @treturn[2] boolean false
@ -146,7 +146,7 @@ end
-- [Pango markup](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html)). -- [Pango markup](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html)).
-- @property markup -- @property markup
-- @tparam string text The text to set. This can contain pango markup (e.g. -- @tparam string text The text to set. This can contain pango markup (e.g.
-- `<b>bold</b>`). You can use `awful.util.escape` to escape -- `<b>bold</b>`). You can use `gears.string.escape` to escape
-- parts of it. -- parts of it.
-- @see text -- @see text

View File

@ -1,38 +0,0 @@
---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @copyright 2015 Uli Schlachter
---------------------------------------------------------------------------
local util = require("awful.util")
describe("awful.util", function()
describe("quote_pattern", function()
it("text", function()
assert.is.equal(util.quote_pattern("text"), "text")
end)
it("do.t", function()
assert.is.equal(util.quote_pattern("do.t"), "do%.t")
end)
it("per%cen[tage", function()
assert.is.equal(util.quote_pattern("per%cen[tage"), "per%%cen%[tage")
end)
end)
describe("query_to_pattern", function()
it("DownLow", function()
assert.is.equal(string.match("DownLow", util.query_to_pattern("downlow")), "DownLow")
end)
it("%word", function()
assert.is.equal(string.match("%word", util.query_to_pattern("%word")), "%word")
end)
it("Substring of DownLow", function()
assert.is.equal(string.match("DownLow", util.query_to_pattern("ownl")), "ownL")
end)
end)
end)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -0,0 +1,32 @@
local gstring = require("gears.string")
describe("gears.string", function()
describe("quote_pattern", function()
it("text", function()
assert.is.equal(gstring.quote_pattern("text"), "text")
end)
it("do.t", function()
assert.is.equal(gstring.quote_pattern("do.t"), "do%.t")
end)
it("per%cen[tage", function()
assert.is.equal(gstring.quote_pattern("per%cen[tage"), "per%%cen%[tage")
end)
end)
describe("query_to_pattern", function()
it("DownLow", function()
assert.is.equal(string.match("DownLow", gstring.query_to_pattern("downlow")), "DownLow")
end)
it("%word", function()
assert.is.equal(string.match("%word", gstring.query_to_pattern("%word")), "%word")
end)
it("Substring of DownLow", function()
assert.is.equal(string.match("DownLow", gstring.query_to_pattern("ownl")), "ownL")
end)
end)
end)