1
0
mirror of https://github.com/skywind3000/z.lua synced 2026-03-22 07:39:48 +00:00

35 Commits

Author SHA1 Message Date
Linwei
a3d4f5db68 Merge pull request #138 from FlTr/path-exists-17
path valid if error code 17 by os.rename
2021-05-05 16:41:06 +08:00
TRF2SGM
a991162428 #136 path valid if error code 17 by os.rename 2021-05-03 12:13:34 +02:00
Linwei
8210c56414 Merge pull request #132 from ilyagr/patch-1
Improve Fish installation instructions
2021-03-08 23:16:40 +08:00
Ilya Grigoriev
a01b28a8d3 Fixup #2 2021-03-08 01:26:22 -08:00
Ilya Grigoriev
695533cee1 Fixup 2021-03-08 01:17:50 -08:00
Ilya Grigoriev
58425dad65 Improve Fish installation instructions
Most notably, this adds instructions of changing $_Z_CD.  I think many
`fish` users would find them useful.
See https://github.com/skywind3000/z.lua/pull/131 for more background.

This makes the installation instructions more conventional (`|source` 
instead of `psub`). This also deletes the second (equivalent) set of instructions.
If the warning in that sentence is important, we could add it back, but
it would apply to all the shells, wouldn't it?

Finally, some wording is improved.
2021-03-08 01:15:26 -08:00
Linwei
44a2489ba3 Merge pull request #131 from ilyagr/add_builtin_cd_doc
Add default to docs for $_ZL_CD
2021-03-04 22:28:18 +08:00
Ilya Grigoriev
6e01d4ad52 Add default to docs for $_ZL_CD
I don't know if the default of `builtin cd` is a good one, as opposed to simple `cd`. It took me quite a bit of debugging before I figured out why, on my `fish` shell, changing directories with `z.lua` messed up the `cdh` (cd history) command.

I think the best thing would be to change the default. However, if you have a good reason to stick with it, I thought that documenting this potentially unexpected behavior might help.
2021-03-04 00:56:51 -08:00
skywind3000
c3c15a3ca9 remove redundant comments 2021-02-15 05:13:24 +08:00
skywind3000
cfad8f54ce update doc 2021-02-15 00:06:38 +08:00
skywind3000
501f619f94 improve powershell 7 compatibility 2021-02-15 00:02:23 +08:00
skywind3000
7a8acd469d update z.lua to 1.8.11 2020-12-31 02:11:09 +08:00
skywind3000
52e066aae7 commit new z.lua 2020-12-31 02:05:46 +08:00
skywind3000
e1a44ee9a5 fix: pushd leak in windows cmd 2020-12-31 01:47:18 +08:00
Linwei
9c0eb6cfbc Merge pull request #127 from chrisant996/fixpushd
Fix leaked pushd
2020-12-30 23:01:17 +08:00
Chris Antos
e646d5baa5 A better fix for the final blank line. 2020-12-30 05:00:26 -08:00
Chris Antos
be90b1a528 Match original output.
Originally `z foo` printed a blank line after the command.  My recent
change accidentally changed how CMD interpreted the end of the file such
that CMD stopped printing the blank line.  This change adjusts the end
of the file to once again cause CMD to print a blank line on finish.
2020-12-30 04:50:29 -08:00
Chris Antos
bda94435fc Fix leaked pushd.
`z somedir` uses double `pushd` to tunnel the new path out past the
endlocal that restores the environment state.  But it ended up leaving
a pushd on the dir stack.

This change exploits normal variable expansion to restore the dir stack
to its initial state while still successfully tunneling the new path out
past the endlocal.
2020-12-30 04:42:05 -08:00
skywind3000
831d617cef improve clink 1.1 compatibility 2020-12-24 15:27:48 +08:00
Linwei
97fa941ac3 Merge pull request #126 from chrisant996/master
Add flags to the `z` arg matcher for Clink.
2020-12-24 10:36:57 +08:00
Chris Antos
c0ff299d67 Add flags to the z arg matcher for Clink. 2020-12-23 14:48:48 -08:00
skywind3000
3deb8c7b5c advance version number 2020-12-23 16:37:56 +08:00
Linwei
222b28571f Merge pull request #125 from fcying/master
add _ZL_CLINK_PROMPT_PRIORITY set clink prompt register priority.
2020-12-23 16:36:16 +08:00
fcying
48a77adf35 add _ZL_CLINK_PROMPT_PRIORITY set clink prompt register priority. 2020-12-23 14:41:27 +08:00
Linwei
72c3676344 Merge pull request #123 from kankaristo/patch-1
Fix `z -I` in ranger_zlua.py
2020-11-25 22:12:30 +08:00
Sami Kankaristo
9dc5875fa3 Fix z -I in ranger_zlua.py
`z -I` had 2 issues:
- it used  `subprocess.check_output()`, but didn't UTF-8 decode the string (like the `if mode` statement's `else` branch does on row 80)
- it wasn't actually interactive, like `-I` is supposed to be (subprocess.check_output()` is not interactive)

I'm not sure why `-I` mode was originally treated differently from e.g. `-i` mode, but the same code works for both.

I took the `redraw_window` console command from the original `-I` mode code, because without it the screen updates a bit slower, causing an ugly "flash".
2020-11-25 14:19:08 +02:00
skywind3000
c90279b677 expose '--add' in cmd.exe 2020-10-31 18:59:03 +08:00
Linwei
9fb8cadda3 Merge pull request #120 from azuwis/master
Use `command -v` to reduce zsh startup time
2020-10-22 11:50:19 +08:00
Zhong Jianxin
edd92d935d Use command -v to reduce zsh startup time 2020-10-22 10:55:23 +08:00
Linwei
7848a0a2f3 Merge pull request #114 from ayalhw/patch-1
Update Fisher compatibility
2020-08-09 14:59:25 +08:00
Coelacanthus
515da0a3d1 Update Fisher compatibility
更新 fisher 路径
https://github.com/jorgebucaran/fisher/pull/571
2020-08-09 13:14:07 +08:00
Linwei
aebd18cec4 Merge pull request #113 from 3usi9/master
Add Powershell support
2020-08-04 17:48:34 +08:00
3usi9
90223cf10b Add Powershell support 2020-08-04 17:37:46 +08:00
Linwei
d666c93ed9 Merge pull request #111 from stigkj/patch-1
Fix benchmark table markdown formatting
2020-08-01 02:45:46 +08:00
Stig Kleppe-Jørgensen
805e5c24f3 Fix benchmark table markdown formatting 2020-07-31 17:31:16 +02:00
7 changed files with 93 additions and 42 deletions

View File

@@ -65,7 +65,12 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
antigen bundle skywind3000/z.lua antigen bundle skywind3000/z.lua
就可以了(主要要放在 antigen apply 语句之前)。 就可以了(主要要放在 antigen apply 语句之前)。
**注意**:使用 WSL-1 的用户,需要安装 `lua-filesystem` 包:
sudo apt-get install lua-filesystem
这是由于 wsl-1 的 [bug](https://github.com/microsoft/WSL/issues/5505) 引起的,使用 lua-filesystem 可以避免该问题。
- Fish Shell: - Fish Shell:
@@ -83,7 +88,7 @@ z -b foo # 跳转到父目录中名称以 foo 开头的那一级
在你 Power Shell 的配置文件 `profile.ps1` 中放入下面语句: 在你 Power Shell 的配置文件 `profile.ps1` 中放入下面语句:
iex ($(lua /path/to/z.lua --init powershell) -join "`n") Invoke-Expression (& { (lua /path/to/z.lua --init powershell) -join "`n" })
- Windows cmd (with clink): - Windows cmd (with clink):

View File

@@ -69,13 +69,19 @@ z -b foo # cd to the parent directory starting with foo
and perhaps this: and perhaps this:
export _ZL_ECHO=1 eval "$(lua /path/to/z.lua --init bash enhanced once echo)"
if you want `z.lua` print the new directory after cd. if you want `z.lua` print the new directory after cd.
If you want `fzf` tab completion use: For `fzf` tab completion use:
eval "$(lua /path/to/z.lua --init bash enhanced once fzf)" eval "$(lua /path/to/z.lua --init bash enhanced once fzf)"
**NOTE**: For wsl-1 users, `lua-filesystem` must be installed:
sudo apt-get install lua-filesystem
To avoid a wsl-1 [defect](https://github.com/microsoft/WSL/issues/5505).
- Zsh: - Zsh:
@@ -85,6 +91,8 @@ z -b foo # cd to the parent directory starting with foo
Options like "enhanced" and "once" can be used after `--init` too. It can also be initialized from "skywind3000/z.lua" with your zsh plugin managers (antigen / oh-my-zsh). Options like "enhanced" and "once" can be used after `--init` too. It can also be initialized from "skywind3000/z.lua" with your zsh plugin managers (antigen / oh-my-zsh).
**NOTE**: for wsl-1 users, `lua-filesystem` must be installed.
- Posix Shells: - Posix Shells:
put something like this in your `.profile`: put something like this in your `.profile`:
@@ -97,37 +105,45 @@ z -b foo # cd to the parent directory starting with foo
To generate old posix compatible script. To generate old posix compatible script.
- Fish Shell: - Fish Shell (version `2.4.0` or above):
Create `~/.config/fish/conf.d/z.fish` with following code Create `~/.config/fish/conf.d/z.fish` with following code
source (lua /path/to/z.lua --init fish | psub) lua /path/to/z.lua --init fish | source
Fish version `2.4.0` or above is required. If you'd like `z.lua` to cooperate with fish's own [directory history](https://fishshell.com/docs/3.2/index.html#id34), you can put
lua /path/to/z.lua --init fish > ~/.config/fish/conf.d/z.fish set -gx _ZL_CD cd
This is another way to initialize z.lua in fish shell, but remember to regenerate z.fish if z.lua has been updated or moved. into the same file.
- Power Shell: - Power Shell:
put something like this in your `profile.ps1`: put something like this in your `profile.ps1`:
iex ($(lua /path/to/z.lua --init powershell) -join "`n") Invoke-Expression (& { (lua /path/to/z.lua --init powershell) -join "`n" })
- Windows cmd (with clink): - Windows cmd (with clink):
- copy z.lua and z.cmd to clink's home directory - Copy z.lua and z.cmd to clink's home directory
- Add clink's home to `%PATH%` (z.cmd can be called anywhere) - Add clink's home to `%PATH%` (z.cmd can be called anywhere)
- Ensure that "lua" can be called in `%PATH%` - Ensure that "lua" can be called in `%PATH%`
- Windows cmder: - Windows cmder:
- copy z.lua and z.cmd to cmder/vendor - Copy z.lua and z.cmd to cmder/vendor
- Add cmder/vendor to `%PATH%` - Add cmder/vendor to `%PATH%`
- Ensure that "lua" can be called in `%PATH%` - Ensure that "lua" can be called in `%PATH%`
- Windows WSL-1:
Install `lua-filesystem` module before init z.lua:
sudo apt-get install lua-filesystem
This module is required due to a wsl-1 [defect](https://github.com/microsoft/WSL/issues/5505).
## Options ## Options
@@ -137,11 +153,12 @@ z -b foo # cd to the parent directory starting with foo
- set `$_ZL_EXCLUDE_DIRS` to a comma separated list of dirs to exclude. - set `$_ZL_EXCLUDE_DIRS` to a comma separated list of dirs to exclude.
- set `$_ZL_ADD_ONCE` to '1' to update database only if `$PWD` changed. - set `$_ZL_ADD_ONCE` to '1' to update database only if `$PWD` changed.
- set `$_ZL_MAXAGE` to define a aging threshold (default is 5000). - set `$_ZL_MAXAGE` to define a aging threshold (default is 5000).
- set `$_ZL_CD` to specify your own cd command. - set `$_ZL_CD` to specify your own cd command (default is `builtin cd` in Unix shells).
- set `$_ZL_ECHO` to 1 to display new directory name after cd. - set `$_ZL_ECHO` to 1 to display new directory name after cd.
- set `$_ZL_MATCH_MODE` to 1 to enable enhanced matching. - set `$_ZL_MATCH_MODE` to 1 to enable enhanced matching.
- set `$_ZL_NO_CHECK` to 1 to disable path validation, use `z --purge` to clean - set `$_ZL_NO_CHECK` to 1 to disable path validation, use `z --purge` to clean
- set `$_ZL_HYPHEN` to 1 to treat hyphon (-) as a normal character not a lua regexp keyword. - set `$_ZL_HYPHEN` to 1 to treat hyphon (-) as a normal character not a lua regexp keyword.
- set `$_ZL_CLINK_PROMPT_PRIORITY` change clink prompt register priority (default 99).
## Aging ## Aging
@@ -251,7 +268,7 @@ To enable this, you can set `$_ZL_ADD_ONCE` to `1` before init z.lua. Or you can
````bash ````bash
eval "$(lua /path/to/z.lua --init bash once)" eval "$(lua /path/to/z.lua --init bash once)"
eval "$(lua /path/to/z.lua --init zsh once)" eval "$(lua /path/to/z.lua --init zsh once)"
source (lua /path/to/z.lua --init fish once | psub) lua /path/to/z.lua --init fish once | source
```` ````
With `add once` mode off (default), z.lua will consider the time you spent in the directory (like z.sh). When this mode is on, consider the times you accessed the directory (like autojump), and that could be much faster on slow hardware. With `add once` mode off (default), z.lua will consider the time you spent in the directory (like z.sh). When this mode is on, consider the times you accessed the directory (like autojump), and that could be much faster on slow hardware.
@@ -469,7 +486,7 @@ So I still recommend the pure lua script for portability and flexibility, but fo
Average performance: Average performance:
| Name | czmod | z.lua | | Name | czmod | z.lua |
|-|-|-|-|-| |-|-|-|
| **Update Time** | 1.6ms | 13.2ms | | **Update Time** | 1.6ms | 13.2ms |
| **Query Time** | 1.5ms | 9.8ms | | **Query Time** | 1.5ms | 9.8ms |

View File

@@ -17,9 +17,9 @@ end
set -x _ZL_DATA "$_ZL_DATA" set -x _ZL_DATA "$_ZL_DATA"
set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME ~/.config set -q XDG_DATA_HOME; or set XDG_DATA_HOME ~/.local/share
if functions -q fisher if functions -q fisher
set _zlua_dir $XDG_CONFIG_HOME/fisher/github.com/skywind3000/z.lua set _zlua_dir $XDG_DATA_HOME/fisher/github.com/skywind3000/z.lua
else else
set _zlua_dir (dirname (status --current-filename)) set _zlua_dir (dirname (status --current-filename))
end end

View File

@@ -69,14 +69,10 @@ class z(ranger.api.commands.Command):
p = self.fm.execute_command(cmd + ' 2>&1 | less +G', universal_newlines=True) p = self.fm.execute_command(cmd + ' 2>&1 | less +G', universal_newlines=True)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
else: else:
if mode == '-I': p = self.fm.execute_command(cmd, universal_newlines=True, stdout=subprocess.PIPE)
os.environ['_ZL_FZF_HEIGHT'] = '0' stdout, stderr = p.communicate()
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args) path = stdout.rstrip('\n')
self.fm.execute_console('redraw_window') self.fm.execute_console('redraw_window')
else:
p = self.fm.execute_command(cmd, universal_newlines=True, stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
path = stdout.rstrip('\n')
if path and os.path.exists(path): if path and os.path.exists(path):
self.fm.cd(path) self.fm.cd(path)
else: else:

18
z.cmd
View File

@@ -54,6 +54,12 @@ if /i "%1"=="-x" (
goto parse goto parse
) )
if /i "%1"=="--add" (
set "RunMode=--add"
shift /1
goto parse
)
if "%1"=="-i" ( if "%1"=="-i" (
set "InterMode=-i" set "InterMode=-i"
shift /1 shift /1
@@ -100,13 +106,23 @@ if /i "%RunMode%"=="-n" (
pushd !NewPath! pushd !NewPath!
pushd !NewPath! pushd !NewPath!
endlocal endlocal
popd goto popdir
) )
) )
) else ( ) else (
call "%LuaExe%" "%LuaScript%" "%RunMode%" %MatchType% %StrictSub% %InterMode% %StripMode% %* call "%LuaExe%" "%LuaScript%" "%RunMode%" %MatchType% %StrictSub% %InterMode% %StripMode% %*
) )
goto end
:popdir
rem -- Exploits variable expansion and the pushd stack to set the current
rem -- directory without leaking a pushd.
popd
setlocal
set NewPath=%CD%
endlocal & popd & cd /d "%NewPath%"
:end :end
echo. echo.

35
z.lua
View File

@@ -1,10 +1,10 @@
#! /usr/bin/env lua #! /usr/bin/env lua
--===================================================================== --=====================================================================
-- --
-- z.lua - a cd command that learns, by skywind 2018, 2019, 2020 -- z.lua - a cd command that learns, by skywind 2018, 2019, 2020, 2021
-- Licensed under MIT license. -- Licensed under MIT license.
-- --
-- Version 1.8.7, Last Modified: 2020/06/29 18:04 -- Version 1.8.12, Last Modified: 2021/02/15 00:05
-- --
-- * 10x faster than fasd and autojump, 3x faster than z.sh -- * 10x faster than fasd and autojump, 3x faster than z.sh
-- * available for posix shells: bash, zsh, sh, ash, dash, busybox -- * available for posix shells: bash, zsh, sh, ash, dash, busybox
@@ -49,9 +49,9 @@
-- source (lua /path/to/z.lua --init fish | psub) -- source (lua /path/to/z.lua --init fish | psub)
-- --
-- Power Shell Install: -- Power Shell Install:
--
-- * put something like this in your config file: -- * put something like this in your config file:
-- iex ($(lua /path/to/z.lua --init powershell) -join "`n") -- Invoke-Expression (& {
-- (lua /path/to/z.lua --init powershell) -join "`n" })
-- --
-- Windows Install (with Clink): -- Windows Install (with Clink):
-- * copy z.lua and z.cmd to clink's home directory -- * copy z.lua and z.cmd to clink's home directory
@@ -63,6 +63,10 @@
-- * Add cmder/vendor to %PATH% -- * Add cmder/vendor to %PATH%
-- * Ensure that "lua" can be called in %PATH% -- * Ensure that "lua" can be called in %PATH%
-- --
-- Windows WSL-1:
-- * Install lua-filesystem module before init z.lua:
-- sudo apt-get install lua-filesystem
--
-- Configure (optional): -- Configure (optional):
-- set $_ZL_CMD in .bashrc/.zshrc to change the command (default z). -- set $_ZL_CMD in .bashrc/.zshrc to change the command (default z).
-- set $_ZL_DATA in .bashrc/.zshrc to change the datafile (default ~/.zlua). -- set $_ZL_DATA in .bashrc/.zshrc to change the datafile (default ~/.zlua).
@@ -576,7 +580,7 @@ function os.path.exists(name)
end end
local ok, err, code = os.rename(name, name) local ok, err, code = os.rename(name, name)
if not ok then if not ok then
if code == 13 then if code == 13 or code == 17 then
return true return true
elseif code == 30 then elseif code == 30 then
local f = io.open(name,"r") local f = io.open(name,"r")
@@ -1955,6 +1959,8 @@ function main(argv)
z_windows_init(opts) z_windows_init(opts)
elseif opts.fish then elseif opts.fish then
z_fish_init(opts) z_fish_init(opts)
elseif opts.powershell then
z_windows_init(opts)
else else
z_shell_init(opts) z_shell_init(opts)
end end
@@ -2057,6 +2063,7 @@ end
----------------------------------------------------------------------- -----------------------------------------------------------------------
function z_clink_init() function z_clink_init()
local once = os.environ("_ZL_ADD_ONCE", false) local once = os.environ("_ZL_ADD_ONCE", false)
local _zl_clink_prompt_priority = os.environ('_ZL_CLINK_PROMPT_PRIORITY', 99)
local previous = '' local previous = ''
function z_add_to_database() function z_add_to_database()
pwd = clink.get_cwd() pwd = clink.get_cwd()
@@ -2068,7 +2075,7 @@ function z_clink_init()
end end
z_add(clink.get_cwd()) z_add(clink.get_cwd())
end end
clink.prompt.register_filter(z_add_to_database, 99) clink.prompt.register_filter(z_add_to_database, _zl_clink_prompt_priority)
function z_match_completion(word) function z_match_completion(word)
local M = z_match({word}, Z_METHOD, Z_SUBDIR) local M = z_match({word}, Z_METHOD, Z_SUBDIR)
for _, item in pairs(M) do for _, item in pairs(M) do
@@ -2078,6 +2085,8 @@ function z_clink_init()
end end
local z_parser = clink.arg.new_parser() local z_parser = clink.arg.new_parser()
z_parser:set_arguments({ z_match_completion }) z_parser:set_arguments({ z_match_completion })
z_parser:set_flags("-c", "-r", "-i", "--cd", "-e", "-b", "--add", "-x", "--purge",
"--init", "-l", "-s", "--complete", "--help", "-h")
clink.arg.register_parser("z", z_parser) clink.arg.register_parser("z", z_parser)
end end
@@ -2500,6 +2509,11 @@ if /i "%1"=="-x" (
shift /1 shift /1
goto parse goto parse
) )
if /i "%1"=="--add" (
set "RunMode=--add"
shift /1
goto parse
)
if "%1"=="-i" ( if "%1"=="-i" (
set "InterMode=-i" set "InterMode=-i"
shift /1 shift /1
@@ -2538,12 +2552,18 @@ if /i "%RunMode%"=="-n" (
pushd !NewPath! pushd !NewPath!
pushd !NewPath! pushd !NewPath!
endlocal endlocal
popd goto popdir
) )
) )
) else ( ) else (
call "%LuaExe%" "%LuaScript%" "%RunMode%" %MatchType% %StrictSub% %InterMode% %StripMode% %* call "%LuaExe%" "%LuaScript%" "%RunMode%" %MatchType% %StrictSub% %InterMode% %StripMode% %*
) )
goto end
:popdir
popd
setlocal
set "NewPath=%CD%"
endlocal & popd & cd /d "%NewPath%"
:end :end
]] ]]
@@ -2727,4 +2747,5 @@ if not pcall(debug.getlocal, 4, 1) then
end end
end end
-- vim: set ts=4 sw=4 tw=0 noet :

View File

@@ -6,15 +6,11 @@ ZLUA_SCRIPT="${0:A:h}/z.lua"
# search lua executable # search lua executable
if [[ -z "$ZLUA_EXEC" ]]; then if [[ -z "$ZLUA_EXEC" ]]; then
if [[ -x "$(command which lua)" ]]; then for lua in lua lua5.3 lua5.2 lua5.1; do
ZLUA_EXEC="$(command which lua)" ZLUA_EXEC="$(command -v "$lua")"
elif [[ -x "$(command which lua5.3)" ]]; then [[ -n "$ZLUA_EXEC" ]] && break
ZLUA_EXEC="$(command which lua5.3)" done
elif [[ -x "$(command which lua5.2)" ]]; then if [[ -z "$ZLUA_EXEC" ]]; then
ZLUA_EXEC="$(command which lua5.2)"
elif [[ -x "$(command which lua5.1)" ]]; then
ZLUA_EXEC="$(command which lua5.1)"
else
echo "Not find lua in your $PATH, please install it." echo "Not find lua in your $PATH, please install it."
return return
fi fi