Add startswith and endswith function to gears.string, change awful.completion to use startswith over local function
This commit is contained in:
parent
bfe58d65f1
commit
5d0e9fd9e3
|
@ -21,6 +21,7 @@ local pairs = pairs
|
|||
local string = string
|
||||
|
||||
local gears_debug = require("gears.debug")
|
||||
local gstring = require("gears.string")
|
||||
|
||||
local completion = {}
|
||||
|
||||
|
@ -79,10 +80,6 @@ function completion.shell(command, cur_pos, ncomp, shell)
|
|||
local i = 1
|
||||
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
|
||||
if cur_pos ~= #command + 1 and command:sub(cur_pos, cur_pos) ~= " " then
|
||||
return command, cur_pos
|
||||
|
@ -160,7 +157,7 @@ function completion.shell(command, cur_pos, ncomp, shell)
|
|||
while true do
|
||||
local line = c:read("*line")
|
||||
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 .. "/"
|
||||
end
|
||||
table.insert(output, bash_escape(line))
|
||||
|
|
|
@ -106,4 +106,22 @@ function gstring.split(str, delimiter)
|
|||
return result
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue