From 7c98dff872e48ee7aa7b838ac62191482476aa32 Mon Sep 17 00:00:00 2001 From: manhong2112 Date: Sun, 3 Feb 2019 01:19:26 +0800 Subject: [PATCH 1/6] [+] Powershell support --- z.lua | 110 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 31 deletions(-) diff --git a/z.lua b/z.lua index cf06a53..fabedd9 100755 --- a/z.lua +++ b/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)" @@ -200,7 +200,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 .. "}") @@ -401,7 +401,7 @@ end -- normalize path ----------------------------------------------------------------------- function os.path.norm(pathname) - if windows then + if windows then pathname = pathname:gsub('\\', '/') end if windows then @@ -491,7 +491,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 @@ -628,7 +628,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') @@ -688,7 +688,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 '') @@ -735,7 +735,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 @@ -877,7 +877,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 @@ -1017,7 +1017,7 @@ function z_match(patterns, method, subdir) end table.sort(M, function (a, b) return a.score > b.score end) local pwd = (PWD == nil or PWD == '') and os.getenv('PWD') or PWD - if pwd ~= '' and pwd ~= nil then + if pwd ~= '' and pwd ~= nil then if subdir then local N = {} for _, item in pairs(M) do @@ -1045,7 +1045,7 @@ end ----------------------------------------------------------------------- --- pretty print +-- pretty print ----------------------------------------------------------------------- function z_print(M, weight, number) local N = {} @@ -1061,7 +1061,7 @@ function z_print(M, weight, number) end end local fp = io.stdout - if PRINT_MODE == '' then + if PRINT_MODE == '' then fp = io.stdout elseif PRINT_MODE == '' then fp = io.stderr @@ -1073,8 +1073,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 @@ -1415,10 +1415,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" ;; @@ -1456,14 +1456,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 @@ -1475,7 +1475,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 @@ -1556,7 +1556,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 @@ -1578,7 +1578,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) @@ -1792,18 +1792,69 @@ if /i "%RunMode%"=="-n" ( :end ]] +local script_init_powershell = [[ + + function z_init_powershell { + if (!$LuaExe) { + $LuaExe = "luajit.exe" + } + if (!$LuaScript) { + $LuaScript = ${HOME} + "\" + "z.lua" + } + if (!$env:_ZL_CD) { + $env:_ZL_CD = "Push-Location" + } + + function global:z_add { + param ( + [string] $path + ) + & $LuaExe $LuaScript --add $path + } + + function global:z { + foreach ($i in $args) { + if ($i -eq "-e" -or $i -eq "-x" -or $i -eq "-l") { + & $LuaExe $LuaScript $args + return + } + } + & $env:_ZL_CD (& $LuaExe $LuaScript -e $args) + } + + $script:_zlua_orig_prompt = ([ref] $function:prompt) + $script:_zlua_previous = "" + function global:prompt { + & $script:_zlua_orig_prompt.value + $local:str_pwd = ([string] $PWD) + if (($env:_ZL_ADD_ONCE -eq 0) -or + (($env:_ZL_ADD_ONCE -eq 1) -and ($script:_zlua_previous -ne $str_pwd))) { + $script:_zlua_previous = $str_pwd + z_add $str_pwd + } + } + } + + z_init_powershell +]] ----------------------------------------------------------------------- -- 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() .. '"') + 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 @@ -1836,6 +1887,3 @@ if not pcall(debug.getlocal, 4, 1) then main() end end - - - From 919c3f7229c8944afd93ab4f6e682dc7523f27b1 Mon Sep 17 00:00:00 2001 From: manhong2112 Date: Sun, 3 Feb 2019 14:47:40 +0800 Subject: [PATCH 2/6] [+] Param parse of powershell support --- z.lua | 134 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 102 insertions(+), 32 deletions(-) diff --git a/z.lua b/z.lua index fabedd9..0184601 100755 --- a/z.lua +++ b/z.lua @@ -1793,49 +1793,119 @@ if /i "%RunMode%"=="-n" ( ]] local script_init_powershell = [[ +function Init-ZLua { + if (!$script:LuaExe) { + $script:LuaExe = "luajit.exe" + } + if (!$script:LuaScript) { + $script:LuaScript = "z.lua" + } + if (!$env:_ZL_CD) { + $env:_ZL_CD = "Push-Location" + } - function z_init_powershell { - if (!$LuaExe) { - $LuaExe = "luajit.exe" - } - if (!$LuaScript) { - $LuaScript = ${HOME} + "\" + "z.lua" - } - if (!$env:_ZL_CD) { - $env:_ZL_CD = "Push-Location" + function global:z { + $arg_mode = "" + $arg_type = "" + $arg_subdir = "" + $arg_inter = "" + $arg_strip = "" + + + if ($args[0] -eq "--add") { + $_, $rest = $args + & $script:LuaExe $script:LuaScript --add $rest + return + } elseif ($args[0] -eq "--complete") { + $_, $rest = $args + & $script:LuaExe $script:LuaScript --complete $rest + return } - function global:z_add { - param ( - [string] $path - ) - & $LuaExe $LuaScript --add $path - } - - function global:z { - foreach ($i in $args) { - if ($i -eq "-e" -or $i -eq "-x" -or $i -eq "-l") { - & $LuaExe $LuaScript $args - 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_mode = "-t" + break + } + "-r" { + $arg_mode = "-r" + break + } + "-c" { + $arg_subdir="-c" + break + } + "-s" { + $arg_strip="-s" + break + } + "-i" { + $arg_inter="-i" + break + } + "-I" { + $arg_inter="-I" + break + } + "-h" {} "--help" { + $arg_mode="-h" + break + } + Default { + break loop } } - & $env:_ZL_CD (& $LuaExe $LuaScript -e $args) + $_, $args = $args + if(!$args) { + break loop + } + $first = $args[0] } - $script:_zlua_orig_prompt = ([ref] $function:prompt) - $script:_zlua_previous = "" - function global:prompt { - & $script:_zlua_orig_prompt.value - $local:str_pwd = ([string] $PWD) - if (($env:_ZL_ADD_ONCE -eq 0) -or - (($env:_ZL_ADD_ONCE -eq 1) -and ($script:_zlua_previous -ne $str_pwd))) { - $script:_zlua_previous = $str_pwd - z_add $str_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 + } } } } - z_init_powershell + $script:_zlua_orig_prompt = ([ref] $function:prompt) + $script:_zlua_previous = "" + function global:prompt { + & $script:_zlua_orig_prompt.value + $local:str_pwd = ([string] $PWD) + if (($env:_ZL_ADD_ONCE -eq 0) -or + (($env:_ZL_ADD_ONCE -eq 1) -and ($script:_zlua_previous -ne $str_pwd))) { + $script:_zlua_previous = $str_pwd + & $script:LuaExe $script:LuaScript --add $str_pwd + } + } +} +Init-ZLua ]] ----------------------------------------------------------------------- From d0b1df5a01f8ef4066459cf77ba401cb89e05ce3 Mon Sep 17 00:00:00 2001 From: manhong2112 Date: Sun, 3 Feb 2019 15:04:22 +0800 Subject: [PATCH 3/6] [+] _ZL_CMD support --- z.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/z.lua b/z.lua index 0184601..9ef2f1d 100755 --- a/z.lua +++ b/z.lua @@ -1793,6 +1793,7 @@ if /i "%RunMode%"=="-n" ( ]] local script_init_powershell = [[ + function Init-ZLua { if (!$script:LuaExe) { $script:LuaExe = "luajit.exe" @@ -1803,8 +1804,11 @@ function Init-ZLua { if (!$env:_ZL_CD) { $env:_ZL_CD = "Push-Location" } + if (!$env:_ZL_CMD) { + $env:_ZL_CMD = "j" + } - function global:z { + function global:_zlua { $arg_mode = "" $arg_type = "" $arg_subdir = "" @@ -1861,7 +1865,8 @@ function Init-ZLua { $arg_inter="-I" break } - "-h" {} "--help" { + "-h" {} + "--help" { $arg_mode="-h" break } @@ -1893,6 +1898,7 @@ function Init-ZLua { } } + Set-Alias $env:_ZL_CMD _zlua -Scope Global $script:_zlua_orig_prompt = ([ref] $function:prompt) $script:_zlua_previous = "" function global:prompt { @@ -1905,6 +1911,7 @@ function Init-ZLua { } } } + Init-ZLua ]] From bfabcee4dd51dd9c23ae744c808c69c59054d1b3 Mon Sep 17 00:00:00 2001 From: manhong2112 Date: Sun, 3 Feb 2019 15:13:02 +0800 Subject: [PATCH 4/6] [+] _ZL_RANDOM [=] change default _ZL_CMD to z [=] fix -h --- z.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/z.lua b/z.lua index 9ef2f1d..c644369 100755 --- a/z.lua +++ b/z.lua @@ -1805,7 +1805,10 @@ function Init-ZLua { $env:_ZL_CD = "Push-Location" } if (!$env:_ZL_CMD) { - $env:_ZL_CMD = "j" + $env:_ZL_CMD = "z" + } + if (!$env:_ZL_RANDOM) { + $env:_ZL_RANDOM = Get-Random } function global:_zlua { @@ -1865,7 +1868,10 @@ function Init-ZLua { $arg_inter="-I" break } - "-h" {} + "-h" { + $arg_mode="-h" + break + } "--help" { $arg_mode="-h" break @@ -1913,6 +1919,7 @@ function Init-ZLua { } Init-ZLua + ]] ----------------------------------------------------------------------- From 77c96548b6fcdcd08cb67530198849e66d250616 Mon Sep 17 00:00:00 2001 From: manhong2112 Date: Mon, 4 Feb 2019 02:32:51 +0800 Subject: [PATCH 5/6] [+] init enchaned, once [+] _ZL_NO_PROMPT_COMMAND [+] Prevent repeating init [+] pwd [=] change default value of LuaExe [=] fix wrong impl of _ZL_RANDOM [=] fix wrong type of param -t -r --- z.lua | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/z.lua b/z.lua index a3d216f..3554e92 100755 --- a/z.lua +++ b/z.lua @@ -2077,21 +2077,27 @@ if /i "%RunMode%"=="-n" ( local script_init_powershell = [[ function Init-ZLua { - if (!$script:LuaExe) { - $script:LuaExe = "luajit.exe" + + # 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" } - if (!$env:_ZL_RANDOM) { - $env:_ZL_RANDOM = Get-Random - } function global:_zlua { $arg_mode = "" @@ -2103,6 +2109,7 @@ function Init-ZLua { if ($args[0] -eq "--add") { $_, $rest = $args + $env:_ZL_RANDOM = Get-Random & $script:LuaExe $script:LuaScript --add $rest return } elseif ($args[0] -eq "--complete") { @@ -2127,11 +2134,11 @@ function Init-ZLua { break } "-t" { - $arg_mode = "-t" + $arg_type = "-t" break } "-r" { - $arg_mode = "-r" + $arg_type = "-r" break } "-c" { @@ -2167,6 +2174,7 @@ function Init-ZLua { break loop } $first = $args[0] + $env:PWD = ([string] $PWD) } if ($arg_mode -eq "-h") { @@ -2187,21 +2195,25 @@ function Init-ZLua { } Set-Alias $env:_ZL_CMD _zlua -Scope Global - $script:_zlua_orig_prompt = ([ref] $function:prompt) - $script:_zlua_previous = "" - function global:prompt { - & $script:_zlua_orig_prompt.value - $local:str_pwd = ([string] $PWD) - if (($env:_ZL_ADD_ONCE -eq 0) -or - (($env:_ZL_ADD_ONCE -eq 1) -and ($script:_zlua_previous -ne $str_pwd))) { - $script:_zlua_previous = $str_pwd - & $script:LuaExe $script:LuaScript --add $str_pwd + + 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 - ]] ----------------------------------------------------------------------- @@ -2211,6 +2223,12 @@ function z_windows_init(opts) 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') From 6b3dad6b1834a6aceb806317bbab427332216628 Mon Sep 17 00:00:00 2001 From: manhong2112 Date: Mon, 4 Feb 2019 02:51:07 +0800 Subject: [PATCH 6/6] [=] move pwd outside of while loop --- z.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/z.lua b/z.lua index 3554e92..5151aa9 100755 --- a/z.lua +++ b/z.lua @@ -2075,7 +2075,6 @@ if /i "%RunMode%"=="-n" ( ]] local script_init_powershell = [[ - function Init-ZLua { # Prevent repeating init @@ -2174,9 +2173,9 @@ function Init-ZLua { break loop } $first = $args[0] - $env:PWD = ([string] $PWD) } + $env:PWD = ([string] $PWD) if ($arg_mode -eq "-h") { & $script:LuaExe $script:LuaScript -h } elseif ($arg_mode -eq "-l" -or $args.Length -eq 0) {