mirror of
https://github.com/skywind3000/z.lua
synced 2026-03-19 06:09:48 +00:00
213
z.lua
213
z.lua
@@ -23,7 +23,7 @@
|
||||
-- * z -x path # remove path from history
|
||||
-- * z -i foo # cd with interactive selection
|
||||
--
|
||||
-- Bash Install:
|
||||
-- Bash Install:
|
||||
-- * put something like this in your .bashrc:
|
||||
-- eval "$(lua /path/to/z.lua --init bash)"
|
||||
--
|
||||
@@ -34,7 +34,7 @@
|
||||
-- Zsh Install:
|
||||
-- * put something like this in your .zshrc:
|
||||
-- eval "$(lua /path/to/z.lua --init zsh)"
|
||||
--
|
||||
--
|
||||
-- Posix Shell Install:
|
||||
-- * put something like this in your .profile:
|
||||
-- eval "$(lua /path/to/z.lua --init posix)"
|
||||
@@ -245,7 +245,7 @@ function printT(table, level)
|
||||
func(v, level + 1)
|
||||
else
|
||||
local content = string.format("%s%s = %s", indent .. " ",tostring(k), tostring(v))
|
||||
print(content)
|
||||
print(content)
|
||||
end
|
||||
end
|
||||
print(indent .. "}")
|
||||
@@ -439,7 +439,7 @@ end
|
||||
-- is absolute path
|
||||
-----------------------------------------------------------------------
|
||||
function os.path.isabs(path)
|
||||
if path == nil or path == '' then
|
||||
if path == nil or path == '' then
|
||||
return false
|
||||
elseif path:sub(1, 1) == '/' then
|
||||
return true
|
||||
@@ -460,7 +460,7 @@ end
|
||||
-- normalize path
|
||||
-----------------------------------------------------------------------
|
||||
function os.path.norm(pathname)
|
||||
if windows then
|
||||
if windows then
|
||||
pathname = pathname:gsub('\\', '/')
|
||||
end
|
||||
if windows then
|
||||
@@ -709,7 +709,7 @@ function os.interpreter()
|
||||
io.stderr:write("cannot get arguments (arg), recompiled your lua\n")
|
||||
return nil
|
||||
end
|
||||
local lua = os.argv[-1]
|
||||
local lua = os.argv[-1]
|
||||
if lua == nil then
|
||||
io.stderr:write("cannot get executable name, recompiled your lua\n")
|
||||
end
|
||||
@@ -846,7 +846,7 @@ end
|
||||
-- returns true for path is insensitive
|
||||
-----------------------------------------------------------------------
|
||||
function path_case_insensitive()
|
||||
if windows then
|
||||
if windows then
|
||||
return true
|
||||
end
|
||||
local eos = os.getenv('OS')
|
||||
@@ -906,7 +906,7 @@ function data_save(filename, M)
|
||||
fp = io.open(filename, 'w')
|
||||
else
|
||||
math.random_init()
|
||||
tmpname = filename .. '.' .. tostring(os.time())
|
||||
tmpname = filename .. '.' .. tostring(os.time())
|
||||
tmpname = tmpname .. math.random_string(8)
|
||||
local rnd = os.getenv('_ZL_RANDOM')
|
||||
tmpname = tmpname .. '' .. (rnd and rnd or '')
|
||||
@@ -953,7 +953,7 @@ end
|
||||
function data_insert(M, filename)
|
||||
local i = 1
|
||||
local sumscore = 0
|
||||
for i = 1, #M do
|
||||
for i = 1, #M do
|
||||
local item = M[i]
|
||||
sumscore = sumscore + item.rank
|
||||
end
|
||||
@@ -1095,7 +1095,7 @@ function data_update_frecent(M)
|
||||
for i = 1, #M do
|
||||
local item = M[i]
|
||||
local dx = current - item.time
|
||||
if dx < 3600 then
|
||||
if dx < 3600 then
|
||||
item.frecent = item.rank * 4
|
||||
elseif dx < 86400 then
|
||||
item.frecent = item.rank * 2
|
||||
@@ -1238,7 +1238,7 @@ function z_match(patterns, method, subdir)
|
||||
if pwd == nil or pwd == '' then
|
||||
pwd = os.pwd()
|
||||
end
|
||||
if pwd ~= '' and pwd ~= nil then
|
||||
if pwd ~= '' and pwd ~= nil then
|
||||
if subdir then
|
||||
local N = {}
|
||||
for _, item in pairs(M) do
|
||||
@@ -1266,7 +1266,7 @@ end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- pretty print
|
||||
-- pretty print
|
||||
-----------------------------------------------------------------------
|
||||
function z_print(M, weight, number)
|
||||
local N = {}
|
||||
@@ -1282,7 +1282,7 @@ function z_print(M, weight, number)
|
||||
end
|
||||
end
|
||||
local fp = io.stdout
|
||||
if PRINT_MODE == '<stdout>' then
|
||||
if PRINT_MODE == '<stdout>' then
|
||||
fp = io.stdout
|
||||
elseif PRINT_MODE == '<stderr>' then
|
||||
fp = io.stderr
|
||||
@@ -1294,8 +1294,8 @@ function z_print(M, weight, number)
|
||||
local line = record.score
|
||||
while true do
|
||||
local tail = line:sub(-1, -1)
|
||||
if tail ~= '0' and tail ~= '.' then
|
||||
break
|
||||
if tail ~= '0' and tail ~= '.' then
|
||||
break
|
||||
end
|
||||
line = line:sub(1, -2)
|
||||
if tail == '.' then
|
||||
@@ -1697,10 +1697,10 @@ _zlua() {
|
||||
elif [ "$1" = "--complete" ]; then
|
||||
shift
|
||||
"$ZLUA_LUAEXE" "$ZLUA_SCRIPT" --complete "$@"
|
||||
return
|
||||
return
|
||||
fi
|
||||
while [ "$1" ]; do
|
||||
case "$1" in
|
||||
case "$1" in
|
||||
-l) local arg_mode="-l" ;;
|
||||
-e) local arg_mode="-e" ;;
|
||||
-x) local arg_mode="-x" ;;
|
||||
@@ -1738,14 +1738,14 @@ alias ${_ZL_CMD:-z}='_zlua'
|
||||
]]
|
||||
|
||||
local script_init_bash = [[
|
||||
case "$PROMPT_COMMAND" in
|
||||
case "$PROMPT_COMMAND" in
|
||||
*_zlua?--add*) ;;
|
||||
*) PROMPT_COMMAND="(_zlua --add \"\$(command pwd 2>/dev/null)\" &);$PROMPT_COMMAND" ;;
|
||||
esac
|
||||
]]
|
||||
|
||||
local script_init_bash_fast = [[
|
||||
case "$PROMPT_COMMAND" in
|
||||
case "$PROMPT_COMMAND" in
|
||||
*_zlua?--add*) ;;
|
||||
*) PROMPT_COMMAND="(_zlua --add \"\$PWD\" &);$PROMPT_COMMAND" ;;
|
||||
esac
|
||||
@@ -1757,7 +1757,7 @@ _zlua_precmd() {
|
||||
_ZL_PREVIOUS_PWD="$PWD"
|
||||
(_zlua --add "$PWD" 2> /dev/null &)
|
||||
}
|
||||
case "$PROMPT_COMMAND" in
|
||||
case "$PROMPT_COMMAND" in
|
||||
*_zlua_precmd*) ;;
|
||||
*) PROMPT_COMMAND="_zlua_precmd;$PROMPT_COMMAND" ;;
|
||||
esac
|
||||
@@ -1838,7 +1838,7 @@ function z_shell_init(opts)
|
||||
print(script_init_bash_once)
|
||||
elseif opts.fast then
|
||||
print(script_init_bash_fast)
|
||||
else
|
||||
else
|
||||
print(script_init_bash)
|
||||
end
|
||||
end
|
||||
@@ -1860,7 +1860,7 @@ function z_shell_init(opts)
|
||||
print(script_init_bash_once)
|
||||
elseif opts.fast then
|
||||
print(script_init_bash_fast)
|
||||
else
|
||||
else
|
||||
print(script_init_bash)
|
||||
end
|
||||
print(script_complete_bash)
|
||||
@@ -2074,18 +2074,170 @@ if /i "%RunMode%"=="-n" (
|
||||
:end
|
||||
]]
|
||||
|
||||
local script_init_powershell = [[
|
||||
function Init-ZLua {
|
||||
|
||||
# Prevent repeating init
|
||||
if ($global:_zlua_inited) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!$script:LuaExe) {
|
||||
$script:LuaExe = "lua.exe"
|
||||
}
|
||||
|
||||
if (!$script:LuaScript) {
|
||||
$script:LuaScript = "z.lua"
|
||||
}
|
||||
|
||||
if (!$env:_ZL_CD) {
|
||||
$env:_ZL_CD = "Push-Location"
|
||||
}
|
||||
|
||||
if (!$env:_ZL_CMD) {
|
||||
$env:_ZL_CMD = "z"
|
||||
}
|
||||
|
||||
function global:_zlua {
|
||||
$arg_mode = ""
|
||||
$arg_type = ""
|
||||
$arg_subdir = ""
|
||||
$arg_inter = ""
|
||||
$arg_strip = ""
|
||||
|
||||
|
||||
if ($args[0] -eq "--add") {
|
||||
$_, $rest = $args
|
||||
$env:_ZL_RANDOM = Get-Random
|
||||
& $script:LuaExe $script:LuaScript --add $rest
|
||||
return
|
||||
} elseif ($args[0] -eq "--complete") {
|
||||
$_, $rest = $args
|
||||
& $script:LuaExe $script:LuaScript --complete $rest
|
||||
return
|
||||
}
|
||||
|
||||
$first = $args[0]
|
||||
:loop while ($first) {
|
||||
switch ($first) {
|
||||
"-l" {
|
||||
$arg_mode = "-l"
|
||||
break
|
||||
}
|
||||
"-e" {
|
||||
$arg_mode = "-e"
|
||||
break
|
||||
}
|
||||
"-x" {
|
||||
$arg_mode = "-x"
|
||||
break
|
||||
}
|
||||
"-t" {
|
||||
$arg_type = "-t"
|
||||
break
|
||||
}
|
||||
"-r" {
|
||||
$arg_type = "-r"
|
||||
break
|
||||
}
|
||||
"-c" {
|
||||
$arg_subdir="-c"
|
||||
break
|
||||
}
|
||||
"-s" {
|
||||
$arg_strip="-s"
|
||||
break
|
||||
}
|
||||
"-i" {
|
||||
$arg_inter="-i"
|
||||
break
|
||||
}
|
||||
"-I" {
|
||||
$arg_inter="-I"
|
||||
break
|
||||
}
|
||||
"-h" {
|
||||
$arg_mode="-h"
|
||||
break
|
||||
}
|
||||
"--help" {
|
||||
$arg_mode="-h"
|
||||
break
|
||||
}
|
||||
Default {
|
||||
break loop
|
||||
}
|
||||
}
|
||||
$_, $args = $args
|
||||
if(!$args) {
|
||||
break loop
|
||||
}
|
||||
$first = $args[0]
|
||||
}
|
||||
|
||||
$env:PWD = ([string] $PWD)
|
||||
if ($arg_mode -eq "-h") {
|
||||
& $script:LuaExe $script:LuaScript -h
|
||||
} elseif ($arg_mode -eq "-l" -or $args.Length -eq 0) {
|
||||
& $script:LuaExe $script:LuaScript -l $arg_subdir $arg_type $arg_strip $args
|
||||
} elseif ($arg_mode -ne "") {
|
||||
& $script:LuaExe $script:LuaScript $arg_mode $arg_subdir $arg_type $arg_inter $args
|
||||
} else {
|
||||
$dest = & $script:LuaExe $script:LuaScript --cd $arg_type $arg_subdir $arg_inter $args
|
||||
if ($dest) {
|
||||
& $env:_ZL_CD "$dest"
|
||||
if ($env:_ZL_ECHO) {
|
||||
Write-Host $PWD
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Set-Alias $env:_ZL_CMD _zlua -Scope Global
|
||||
|
||||
if (!$env:_ZL_NO_PROMPT_COMMAND) {
|
||||
$script:_zlua_orig_prompt = ([ref] $function:prompt)
|
||||
$script:_zlua_previous = ""
|
||||
function global:prompt {
|
||||
& $script:_zlua_orig_prompt.value
|
||||
$str_pwd = ([string] $PWD)
|
||||
if ((!$env:_ZL_ADD_ONCE) -or
|
||||
($env:_ZL_ADD_ONCE -and ($script:_zlua_previous -ne $str_pwd))) {
|
||||
$script:_zlua_previous = $str_pwd
|
||||
_zlua --add $str_pwd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$global:_zlua_inited = $True
|
||||
}
|
||||
|
||||
Init-ZLua
|
||||
]]
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- initialize cmd
|
||||
-----------------------------------------------------------------------
|
||||
function z_windows_init(opts)
|
||||
print('@echo off')
|
||||
print('setlocal EnableDelayedExpansion')
|
||||
print('set "LuaExe=' .. os.interpreter() .. '"')
|
||||
print('set "LuaScript=' .. os.scriptname() .. '"')
|
||||
print(script_init_cmd)
|
||||
if opts.newline then
|
||||
print('echo.')
|
||||
if opts.powershell ~= nil then
|
||||
print('$LuaExe = "' .. os.interpreter() .. '"')
|
||||
print('$LuaScript = "' .. os.scriptname() .. '"')
|
||||
if opts.enhanced ~= nil then
|
||||
print('$env:_ZL_MATCH_MODE = 1')
|
||||
end
|
||||
if opts.once ~= nil then
|
||||
print('$env:_ZL_ADD_ONCE = 1')
|
||||
end
|
||||
print(script_init_powershell)
|
||||
else
|
||||
print('@echo off')
|
||||
print('setlocal EnableDelayedExpansion')
|
||||
print('set "LuaExe=' .. os.interpreter() .. '"')
|
||||
print('set "LuaScript=' .. os.scriptname() .. '"')
|
||||
print(script_init_cmd)
|
||||
if opts.newline then
|
||||
print('echo.')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2118,6 +2270,3 @@ if not pcall(debug.getlocal, 4, 1) then
|
||||
main()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user