Merge pull request #1876 from FloGa/FloGa-patch-complete-directory

completion: Show command prior to directory
This commit is contained in:
Emmanuel Lepage Vallée 2017-08-09 23:01:45 -04:00 committed by GitHub
commit 3cf09b1b48
3 changed files with 105 additions and 13 deletions

View File

@ -9,6 +9,8 @@
-- @module awful.completion -- @module awful.completion
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
local gfs = require("gears.filesystem")
-- Grab environment we need -- Grab environment we need
local io = io local io = io
local os = os local os = os
@ -77,6 +79,10 @@ 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
@ -154,7 +160,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 os.execute("test -d " .. string.format('%q', line)) == 0 then if str_starts(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

@ -53,6 +53,17 @@ function filesystem.file_readable(filename)
gfileinfo:get_attribute_boolean("access::can-read") gfileinfo:get_attribute_boolean("access::can-read")
end end
--- Check if a file exists, is executable and not a directory.
-- @tparam string filename The file path.
-- @treturn boolean True if file exists and is executable.
function filesystem.file_executable(filename)
local gfile = Gio.File.new_for_path(filename)
local gfileinfo = gfile:query_info("standard::type,access::can-execute",
Gio.FileQueryInfoFlags.NONE)
return gfileinfo and gfileinfo:get_file_type() ~= "DIRECTORY" and
gfileinfo:get_attribute_boolean("access::can-execute")
end
--- Check if a path exists, is readable and a directory. --- Check if a path exists, is readable and a directory.
-- @tparam string path The directory path. -- @tparam string path The directory path.
-- @treturn boolean True if path exists and is readable. -- @treturn boolean True if path exists and is readable.

View File

@ -6,6 +6,7 @@ end
local gfs = require("gears.filesystem") local gfs = require("gears.filesystem")
local Gio = require("lgi").Gio local Gio = require("lgi").Gio
local GLib = require("lgi").GLib local GLib = require("lgi").GLib
local lfs = require("lfs")
local has_bash = GLib.find_program_in_path("bash") local has_bash = GLib.find_program_in_path("bash")
local has_zsh = GLib.find_program_in_path("zsh") local has_zsh = GLib.find_program_in_path("zsh")
@ -59,20 +60,18 @@ local function get_test_dir()
end end
describe("awful.completion.shell in empty directory", function() describe("awful.completion.shell in empty directory", function()
local orig_popen = io.popen local orig_dir = lfs.currentdir()
setup(function() setup(function()
test_dir = get_test_dir() test_dir = get_test_dir()
io.popen = function(...) --luacheck: ignore lfs.chdir(test_dir)
return orig_popen(string.format('cd %s && ', test_dir) .. ...)
end
test_path = get_test_path_dir() test_path = get_test_path_dir()
end) end)
teardown(function() teardown(function()
assert.True(os.remove(test_dir)) assert.True(os.remove(test_dir))
io.popen = orig_popen --luacheck: ignore
remove_test_path_dir(test_path) remove_test_path_dir(test_path)
lfs.chdir(orig_dir)
end) end)
if has_bash then if has_bash then
@ -91,23 +90,46 @@ describe("awful.completion.shell in empty directory", function()
end) end)
describe("awful.completion.shell", function() describe("awful.completion.shell", function()
local orig_popen = io.popen local orig_dir = lfs.currentdir()
setup(function() setup(function()
test_dir = get_test_dir() test_dir = get_test_dir()
gfs.make_directories(test_dir .. '/true')
Gio.File.new_for_path(test_dir .. '/true/with_file'):create(Gio.FileCreateFlags.NONE);
gfs.make_directories(test_dir .. '/just_a_directory')
Gio.File.new_for_path(test_dir .. '/just_a_directory/with_file'):create(Gio.FileCreateFlags.NONE);
-- Chaotic order is intended!
gfs.make_directories(test_dir .. '/ambiguous_dir_a')
gfs.make_directories(test_dir .. '/ambiguous_dir_e')
Gio.File.new_for_path(test_dir .. '/ambiguous_dir_e/with_file'):create(Gio.FileCreateFlags.NONE);
gfs.make_directories(test_dir .. '/ambiguous_dir_c')
Gio.File.new_for_path(test_dir .. '/ambiguous_dir_c/with_file'):create(Gio.FileCreateFlags.NONE);
gfs.make_directories(test_dir .. '/ambiguous_dir_d')
gfs.make_directories(test_dir .. '/ambiguous_dir_b')
gfs.make_directories(test_dir .. '/ambiguous_dir_f')
os.execute(string.format( os.execute(string.format(
'cd %s && touch localcommand && chmod +x localcommand', test_dir)) 'cd %s && touch localcommand && chmod +x localcommand', test_dir))
io.popen = function(...) --luacheck: ignore lfs.chdir(test_dir)
return orig_popen(string.format('cd %s && ', test_dir) .. ...)
end
test_path = get_test_path_dir() test_path = get_test_path_dir()
end) end)
teardown(function() teardown(function()
assert.True(os.remove(test_dir .. '/ambiguous_dir_a'))
assert.True(os.remove(test_dir .. '/ambiguous_dir_b'))
assert.True(os.remove(test_dir .. '/ambiguous_dir_c/with_file'))
assert.True(os.remove(test_dir .. '/ambiguous_dir_c'))
assert.True(os.remove(test_dir .. '/ambiguous_dir_d'))
assert.True(os.remove(test_dir .. '/ambiguous_dir_e/with_file'))
assert.True(os.remove(test_dir .. '/ambiguous_dir_e'))
assert.True(os.remove(test_dir .. '/ambiguous_dir_f'))
assert.True(os.remove(test_dir .. '/just_a_directory/with_file'))
assert.True(os.remove(test_dir .. '/just_a_directory'))
assert.True(os.remove(test_dir .. '/localcommand')) assert.True(os.remove(test_dir .. '/localcommand'))
assert.True(os.remove(test_dir .. '/true/with_file'))
assert.True(os.remove(test_dir .. '/true'))
assert.True(os.remove(test_dir)) assert.True(os.remove(test_dir))
io.popen = orig_popen --luacheck: ignore
remove_test_path_dir(test_path) remove_test_path_dir(test_path)
lfs.chdir(orig_dir)
end) end)
if has_bash then if has_bash then
@ -137,12 +159,65 @@ describe("awful.completion.shell", function()
if has_bash then if has_bash then
it("completes local file (bash)", function() it("completes local file (bash)", function()
assert.same(shell('ls ', 4, 1, 'bash'), {'ls localcommand', 16, {'localcommand'}}) assert.same(shell('ls l', 5, 1, 'bash'), {'ls localcommand', 16, {'localcommand'}})
end) end)
end end
if has_zsh then if has_zsh then
it("completes local file (zsh)", function() it("completes local file (zsh)", function()
assert.same(shell('ls ', 4, 1, 'zsh'), {'ls localcommand', 16, {'localcommand'}}) assert.same(shell('ls l', 5, 1, 'zsh'), {'ls localcommand', 16, {'localcommand'}})
end)
end
if has_bash then
it("completes command regardless of local directory (bash)", function()
assert.same(shell('true', 5, 1, 'bash'), {'true', 5, {'true'}})
end)
end
if has_zsh then
it("completes command regardless of local directory (zsh)", function()
assert.same(shell('true', 5, 1, 'zsh'), {'true', 5, {'true'}})
end)
end
it("completes command regardless of local directory (nil)", function()
assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}})
end)
if has_bash then
it("does not complete local directory not starting with ./ (bash)", function()
assert.same(shell('just_a', 7, 1, 'bash'), {'just_a', 7})
end)
end
if has_zsh then
it("does not complete local directory not starting with ./ (zsh)", function()
assert.same(shell('just_a', 7, 1, 'zsh'), {'just_a', 7})
end)
end
if has_bash then
it("completes local directories starting with ./ (bash)", function()
assert.same(shell('./just', 7, 1, 'bash'), {'./just_a_directory/', 20, {'./just_a_directory/'}})
assert.same(shell('./t', 4, 1, 'bash'), {'./true/', 8, {'./true/'}})
end)
end
if has_zsh then
it("completes local directories starting with ./ (zsh, non-empty)", function()
assert.same(shell('./just', 7, 1, 'zsh'), {'./just_a_directory/', 20, {'./just_a_directory/'}})
assert.same(shell('./t', 4, 1, 'zsh'), {'./true/', 8, {'./true/'}})
end)
end
--]]
if has_bash then
it("correctly sorts completed items (bash)", function()
assert.same(shell('./ambi', 7, 1, 'bash'), {'./ambiguous_dir_a/', 19,
{'./ambiguous_dir_a/', './ambiguous_dir_b/', './ambiguous_dir_c/',
'./ambiguous_dir_d/', './ambiguous_dir_e/', './ambiguous_dir_f/'}})
end)
end
if has_zsh then
it("correctly sorts completed items (zsh)", function()
assert.same(shell('./ambi', 7, 1, 'zsh'), {'./ambiguous_dir_c/', 19,
{'./ambiguous_dir_c/', './ambiguous_dir_e/'}})
end) end)
end end
end) end)