Add startswith and endswith function to gears.string, change awful.completion to use startswith over local function

This commit is contained in:
Kevin Zander 2017-08-14 10:48:12 -05:00
parent bfe58d65f1
commit 5d0e9fd9e3
2 changed files with 20 additions and 5 deletions

View File

@ -21,6 +21,7 @@ local pairs = pairs
local string = string local string = string
local gears_debug = require("gears.debug") local gears_debug = require("gears.debug")
local gstring = require("gears.string")
local completion = {} local completion = {}
@ -79,10 +80,6 @@ function completion.shell(command, cur_pos, ncomp, shell)
local i = 1 local i = 1
local comptype = "file" local comptype = "file"
local function str_starts(str, start)
return string.sub(str, 1, string.len(start)) == start
end
-- do nothing if we are on a letter, i.e. not at len + 1 or on a space -- do nothing if we are on a letter, i.e. not at len + 1 or on a space
if cur_pos ~= #command + 1 and command:sub(cur_pos, cur_pos) ~= " " then if cur_pos ~= #command + 1 and command:sub(cur_pos, cur_pos) ~= " " then
return command, cur_pos return command, cur_pos
@ -160,7 +157,7 @@ function completion.shell(command, cur_pos, ncomp, shell)
while true do while true do
local line = c:read("*line") local line = c:read("*line")
if not line then break end if not line then break end
if str_starts(line, "./") and gfs.is_dir(line) then if gstring.startswith(line, "./") and gfs.is_dir(line) then
line = line .. "/" line = line .. "/"
end end
table.insert(output, bash_escape(line)) table.insert(output, bash_escape(line))

View File

@ -106,4 +106,22 @@ function gstring.split(str, delimiter)
return result return result
end end
--- Check if a string starts with another string
-- @class function
-- @name startswith
-- @tparam string str String to search
-- @tparam string sub String to check for
function gstring.startswith(str, sub)
return string.sub(str, 1, string.len(sub)) == sub
end
--- Check if a string ends with another string
-- @class function
-- @name endswith
-- @tparam string str String to search
-- @tparam string sub String to check for
function gstring.endswith(str, sub)
return sub == "" or string.sub(str,-string.len(sub)) == sub
end
return gstring return gstring