From 5d0e9fd9e300a95119264419b00c388bd9f91475 Mon Sep 17 00:00:00 2001 From: Kevin Zander Date: Mon, 14 Aug 2017 10:48:12 -0500 Subject: [PATCH] Add startswith and endswith function to gears.string, change awful.completion to use startswith over local function --- lib/awful/completion.lua | 7 ++----- lib/gears/string.lua | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/awful/completion.lua b/lib/awful/completion.lua index c62f22c07..4fd14840b 100644 --- a/lib/awful/completion.lua +++ b/lib/awful/completion.lua @@ -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)) diff --git a/lib/gears/string.lua b/lib/gears/string.lua index b11da8171..186082ff8 100644 --- a/lib/gears/string.lua +++ b/lib/gears/string.lua @@ -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