1
0
mirror of https://github.com/wting/autojump synced 2025-06-07 18:04:09 +00:00
This commit is contained in:
XLO-jiang 2025-05-18 18:45:31 +08:00 committed by GitHub
commit 49ddc1a403
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 6 deletions

View File

@ -22,6 +22,7 @@ from __future__ import print_function
import os import os
import sys import sys
import subprocess
from itertools import chain from itertools import chain
from math import sqrt from math import sqrt
from operator import attrgetter from operator import attrgetter
@ -56,6 +57,7 @@ from autojump_utils import get_tab_entry_info
from autojump_utils import has_uppercase from autojump_utils import has_uppercase
from autojump_utils import is_autojump_sourced from autojump_utils import is_autojump_sourced
from autojump_utils import is_osx from autojump_utils import is_osx
from autojump_utils import is_msys2
from autojump_utils import is_windows from autojump_utils import is_windows
from autojump_utils import last from autojump_utils import last
from autojump_utils import print_entry from autojump_utils import print_entry
@ -70,19 +72,43 @@ FUZZY_MATCH_THRESHOLD = 0.6
TAB_ENTRIES_COUNT = 9 TAB_ENTRIES_COUNT = 9
TAB_SEPARATOR = '__' TAB_SEPARATOR = '__'
def convert_path(path, option="-u"):
"""
使用 cygpath 转换路径格式
参数:
- path: 要转换的路径(字符串)
- option: 转换选项
- '-u': Windows 路径 → Unix 路径(默认)
- '-w': Unix 路径 → Windows 路径
"""
if is_msys2():
try:
result = subprocess.run(
['cygpath', option, path],
capture_output=True,
text=True,
check=True
)
return result.stdout.strip() # 返回转换后的路径
except subprocess.CalledProcessError as e:
print(f"Error: {e.stderr}")
return path # 转换失败时返回原始路径
else:
return path;
def set_defaults(): def set_defaults():
config = {} config = {}
if is_osx(): if is_osx():
data_home = os.path.join(os.path.expanduser('~'), 'Library') data_home = os.path.join(os.path.expanduser('~'), 'Library')
elif is_windows(): elif is_windows() and not is_msys2():
data_home = os.getenv('APPDATA') data_home = os.getenv('APPDATA')
else: else:
data_home = os.getenv( data_home = os.getenv(
'XDG_DATA_HOME', 'XDG_DATA_HOME',
os.path.join( os.path.join(
os.path.expanduser('~'), os.getenv('HOME'),
'.local', '.local',
'share', 'share',
), ),
@ -274,10 +300,11 @@ def main(args): # noqa
print('and read the post installation instructions.') print('and read the post installation instructions.')
return 1 return 1
config = set_defaults() config = set_defaults();
# all arguments are mutually exclusive # all arguments are mutually exclusive
if args.add: if args.add:
args.add = convert_path(args.add);
save(config, first(add_path(load(config), args.add))) save(config, first(add_path(load(config), args.add)))
elif args.complete: elif args.complete:
handle_tab_completion( handle_tab_completion(
@ -339,4 +366,25 @@ def main(args): # noqa
if __name__ == '__main__': if __name__ == '__main__':
if is_msys2():
class BinaryStdout:
'''
print(xxx, end="\n")
'''
def __init__(self, stream):
self.stream = stream
def write(self, s):
if isinstance(s, str):
s = s.encode('utf-8')
self.stream.buffer.write(s)
def flush(self):
self.stream.buffer.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
sys.stdout = BinaryStdout(sys.stdout)
sys.exit(main(parse_arguments())) sys.exit(main(parse_arguments()))

View File

@ -113,6 +113,13 @@ def is_windows():
return platform.system() == 'Windows' return platform.system() == 'Windows'
def is_msys2():
'''
判断存在环境变量MSYSTEM说明是msys2环境
'''
return "MSYSTEM" in os.environ and platform.system() == 'Windows'
def last(xs): def last(xs):
it = iter(xs) it = iter(xs)
tmp = None tmp = None

View File

@ -12,6 +12,12 @@ from autojump_argparse import ArgumentParser # noqa
SUPPORTED_SHELLS = ('bash', 'zsh', 'fish', 'tcsh') SUPPORTED_SHELLS = ('bash', 'zsh', 'fish', 'tcsh')
def is_msys2():
'''
判断存在环境变量MSYSTEM说明是msys2环境
'''
return "MSYSTEM" in os.environ and platform.system() == 'Windows'
def cp(src, dest, dryrun=False): def cp(src, dest, dryrun=False):
print('copying file: %s -> %s' % (src, dest)) print('copying file: %s -> %s' % (src, dest))
@ -55,7 +61,15 @@ def modify_autojump_lua(clink_dir, bin_dir, dryrun=False):
def parse_arguments(): # noqa def parse_arguments(): # noqa
if platform.system() == 'Windows': # USER MSYSTEM_PREFIX
# 判断存在环境变量MSYSTEM_PREFIX说明是msys2环境
if is_msys2():
# 如果是msys2环境设置默认路径
default_user_destdir = os.path.join(
os.getenv('HOME'),
'.autojump',
)
elif platform.system() == 'Windows':
default_user_destdir = os.path.join( default_user_destdir = os.path.join(
os.getenv('LOCALAPPDATA', ''), os.getenv('LOCALAPPDATA', ''),
'autojump', 'autojump',
@ -156,7 +170,7 @@ def parse_arguments(): # noqa
def show_post_installation_message(etc_dir, share_dir, bin_dir): def show_post_installation_message(etc_dir, share_dir, bin_dir):
if platform.system() == 'Windows': if platform.system() == 'Windows' and not is_msys2():
print('\nPlease manually add %s to your user path' % bin_dir) print('\nPlease manually add %s to your user path' % bin_dir)
else: else:
if get_shell() == 'fish': if get_shell() == 'fish':
@ -205,7 +219,7 @@ def main(args):
cp('./bin/icon.png', share_dir, args.dryrun) cp('./bin/icon.png', share_dir, args.dryrun)
cp('./docs/autojump.1', doc_dir, args.dryrun) cp('./docs/autojump.1', doc_dir, args.dryrun)
if platform.system() == 'Windows': if platform.system() == 'Windows' and not is_msys2():
cp('./bin/autojump.lua', args.clinkdir, args.dryrun) cp('./bin/autojump.lua', args.clinkdir, args.dryrun)
cp('./bin/autojump.bat', bin_dir, args.dryrun) cp('./bin/autojump.bat', bin_dir, args.dryrun)
cp('./bin/j.bat', bin_dir, args.dryrun) cp('./bin/j.bat', bin_dir, args.dryrun)