Created
September 20, 2014 15:43
-
-
Save nocd5/1add5bd3e0c6e73f9d0d to your computer and use it in GitHub Desktop.
nyagosでpecoを使ったディレクトリ移動を行う
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alias{ | |
pcd = function () | |
local line = nyagos.eval("ls -la") -- nyagos組み込みの`ls` | |
local complst = split(line, "[\r\n]") | |
local directories = "" | |
for i, e in ipairs(complst) do | |
-- 末尾が'/'ならディレクトリって事で決め打ち | |
if (e:match('.+/$')) then | |
-- ls -lの結果が | |
-- <パーミション> <サイズ> <日付> <時間> <ファイル名 or ディレクトリ名> | |
-- と出力されるので、スペースで区切られた5つ目の要素を取得 | |
directories = directories .. '\n' .. e:gsub(".-%s+", "", 4) | |
end | |
end | |
if (directories == "") then | |
print ("Could not find any directory.") | |
else | |
local dir = nyagos.eval("echo " .. directories .. " | peco") | |
if (dir ~= nil) then | |
nyagos.exec("cd " .. '"' .. chomp(dir) .. '"') | |
end | |
end | |
end | |
} | |
-- code from 'http://lua-users.org/wiki/SplitJoin' | |
function split(str, pat) | |
local t = {} -- NOTE: use {n = 0} in Lua-5.0 | |
local fpat = "(.-)" .. pat | |
local last_end = 1 | |
local s, e, cap = str:find(fpat, 1) | |
while s do | |
if s ~= 1 or cap ~= "" then | |
table.insert(t,cap) | |
end | |
last_end = e+1 | |
s, e, cap = str:find(fpat, last_end) | |
end | |
if last_end <= #str then | |
cap = str:sub(last_end) | |
table.insert(t, cap) | |
end | |
return t | |
end | |
function chomp(src) | |
return string.gsub(src, "[\r\n]+$", "") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment