diff --git a/bin/autojump b/bin/autojump index d039cd1..e689df1 100755 --- a/bin/autojump +++ b/bin/autojump @@ -22,6 +22,7 @@ from __future__ import print_function import os import sys +import subprocess from itertools import chain from math import sqrt 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 is_autojump_sourced from autojump_utils import is_osx +from autojump_utils import is_msys2 from autojump_utils import is_windows from autojump_utils import last from autojump_utils import print_entry @@ -70,19 +72,43 @@ FUZZY_MATCH_THRESHOLD = 0.6 TAB_ENTRIES_COUNT = 9 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(): config = {} if is_osx(): data_home = os.path.join(os.path.expanduser('~'), 'Library') - elif is_windows(): + elif is_windows() and not is_msys2(): data_home = os.getenv('APPDATA') else: data_home = os.getenv( 'XDG_DATA_HOME', os.path.join( - os.path.expanduser('~'), + os.getenv('HOME'), '.local', 'share', ), @@ -274,10 +300,11 @@ def main(args): # noqa print('and read the post installation instructions.') return 1 - config = set_defaults() + config = set_defaults(); # all arguments are mutually exclusive if args.add: + args.add = convert_path(args.add); save(config, first(add_path(load(config), args.add))) elif args.complete: handle_tab_completion( @@ -339,4 +366,25 @@ def main(args): # noqa 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())) diff --git a/bin/autojump_utils.py b/bin/autojump_utils.py index 4ac545a..19c28a0 100644 --- a/bin/autojump_utils.py +++ b/bin/autojump_utils.py @@ -113,6 +113,13 @@ def is_windows(): return platform.system() == 'Windows' +def is_msys2(): + ''' + 判断存在环境变量MSYSTEM,说明是msys2环境 + ''' + return "MSYSTEM" in os.environ and platform.system() == 'Windows' + + def last(xs): it = iter(xs) tmp = None diff --git a/install.py b/install.py index b02abf8..8c7a0c4 100755 --- a/install.py +++ b/install.py @@ -12,6 +12,12 @@ from autojump_argparse import ArgumentParser # noqa 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): 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 - 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( os.getenv('LOCALAPPDATA', ''), 'autojump', @@ -156,7 +170,7 @@ def parse_arguments(): # noqa 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) else: if get_shell() == 'fish': @@ -205,7 +219,7 @@ def main(args): cp('./bin/icon.png', share_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.bat', bin_dir, args.dryrun) cp('./bin/j.bat', bin_dir, args.dryrun)