From 0dc718d81b975b5c0fc74d00e0a07eb53cb366f8 Mon Sep 17 00:00:00 2001 From: Zach Whaley Date: Thu, 8 Dec 2016 15:31:09 -0600 Subject: [PATCH] Use fish shell's autoloading functions for j command Fish shell allows users to put function files in a directory which are loaded as command line commands during login See https://fishshell.com/docs/current/tutorial.html#tut_autoload for more - Add an option to install autojump.fish to a user's fish function directory (typically ~/.config/fish/functions) - During system install, copy the autojump.fish function file to /usr/share/fish/vendor_functions.d, the system fish function directory - On a non-system install, print instructions to add the autojump's fish function directory to the user's fish function path --- install.py | 22 +++++++++++++++------- uninstall.py | 10 ++++++++-- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/install.py b/install.py index 4cc42ac..7092df3 100755 --- a/install.py +++ b/install.py @@ -64,9 +64,11 @@ def parse_arguments(): # noqa '.autojump') default_user_prefix = '' default_user_zshshare = 'functions' + default_user_fishfunc = 'fish_functions' default_system_destdir = '/' default_system_prefix = '/usr/local' default_system_zshshare = '/usr/share/zsh/site-functions' + default_system_fishfunc = '/usr/share/fish/vendor_functions.d' default_clink_dir = os.path.join(os.getenv('LOCALAPPDATA', ''), 'clink') parser = ArgumentParser( @@ -87,6 +89,9 @@ def parse_arguments(): # noqa parser.add_argument( '-z', '--zshshare', metavar='DIR', default=default_user_zshshare, help='set zsh share destination to DIR') + parser.add_argument( + '-i', '--fishfunc', metavar='DIR', default=default_user_fishfunc, + help='set fish function destination to DIR') parser.add_argument( '-c', '--clinkdir', metavar='DIR', default=default_clink_dir, help='set clink directory location to DIR (Windows only)') @@ -118,7 +123,8 @@ def parse_arguments(): # noqa if args.destdir != default_user_destdir \ or args.prefix != default_user_prefix \ - or args.zshshare != default_user_zshshare: + or args.zshshare != default_user_zshshare \ + or args.fishfunc != default_user_fishfunc: args.custom_install = True else: args.custom_install = False @@ -132,17 +138,17 @@ def parse_arguments(): # noqa args.destdir = default_system_destdir args.prefix = default_system_prefix args.zshshare = default_system_zshshare + args.fishfunc = default_system_fishfunc return args -def show_post_installation_message(etc_dir, share_dir, bin_dir): +def show_post_installation_message(args, etc_dir, share_dir, bin_dir, fishfunc_dir): if platform.system() == 'Windows': print('\nPlease manually add %s to your user path' % bin_dir) else: - if get_shell() == 'fish': - aj_shell = '%s/autojump.fish' % share_dir - source_msg = 'if test -f %s; . %s; end' % (aj_shell, aj_shell) + if get_shell() == 'fish' and not args.system: + source_msg = 'set fish_function_path %s $fish_function_path' % fishfunc_dir rcfile = '~/.config/fish/config.fish' else: aj_shell = '%s/autojump.sh' % etc_dir @@ -172,6 +178,7 @@ def main(args): doc_dir = os.path.join(args.destdir, args.prefix, 'share', 'man', 'man1') share_dir = os.path.join(args.destdir, args.prefix, 'share', 'autojump') zshshare_dir = os.path.join(args.destdir, args.zshshare) + fishfunc_dir = os.path.join(args.destdir, args.fishfunc) mkdir(bin_dir, args.dryrun) mkdir(doc_dir, args.dryrun) @@ -200,17 +207,18 @@ def main(args): mkdir(etc_dir, args.dryrun) mkdir(share_dir, args.dryrun) mkdir(zshshare_dir, args.dryrun) + mkdir(fishfunc_dir, args.dryrun) cp('./bin/autojump.sh', etc_dir, args.dryrun) cp('./bin/autojump.bash', share_dir, args.dryrun) - cp('./bin/autojump.fish', share_dir, args.dryrun) + cp('./bin/autojump.fish', fishfunc_dir+'/j.fish', args.dryrun) cp('./bin/autojump.zsh', share_dir, args.dryrun) cp('./bin/_j', zshshare_dir, args.dryrun) if args.custom_install: modify_autojump_sh(etc_dir, share_dir, args.dryrun) - show_post_installation_message(etc_dir, share_dir, bin_dir) + show_post_installation_message(args, etc_dir, share_dir, bin_dir, fishfunc_dir) if __name__ == '__main__': diff --git a/uninstall.py b/uninstall.py index 9e9417b..f97b48b 100755 --- a/uninstall.py +++ b/uninstall.py @@ -41,6 +41,9 @@ def parse_arguments(): parser.add_argument( '-z', '--zshshare', metavar='DIR', default='functions', help='custom zshshare') + parser.add_argument( + '-i', '--fishfunc', metavar='DIR', default='fish_functions', + help='set fish function destination to DIR') parser.add_argument( '-c', '--clinkdir', metavar='DIR', default=default_clink_dir) @@ -56,6 +59,7 @@ def remove_custom_installation(args, dryrun=False): etc_dir = os.path.join(args.destdir, 'etc', 'profile.d') share_dir = os.path.join(args.destdir, args.prefix, 'share', 'autojump') zshshare_dir = os.path.join(args.destdir, args.zshshare) + fishfunc_dir = os.path.join(args.destdir, args.fishfunc) if not os.path.exists(share_dir): return @@ -76,7 +80,7 @@ def remove_custom_installation(args, dryrun=False): else: rm(os.path.join(etc_dir, 'autojump.sh'), dryrun) rm(os.path.join(share_dir, 'autojump.bash'), dryrun) - rm(os.path.join(share_dir, 'autojump.fish'), dryrun) + rm(os.path.join(fishfunc_dir, 'j.fish'), dryrun) rm(os.path.join(share_dir, 'autojump.tcsh'), dryrun) rm(os.path.join(share_dir, 'autojump.zsh'), dryrun) rm(os.path.join(zshshare_dir, '_j'), dryrun) @@ -91,6 +95,7 @@ def remove_system_installation(dryrun=False): default_destdir = '/' default_prefix = '/usr/local' default_zshshare = '/usr/share/zsh/site-functions' + default_fishfunc = '/usr/share/fish/vendor_functions.d' bin_dir = os.path.join(default_destdir, default_prefix, 'bin') doc_dir = os.path.join( @@ -106,6 +111,7 @@ def remove_system_installation(dryrun=False): 'share', 'autojump') zshshare_dir = os.path.join(default_destdir, default_zshshare) + fishfunc_dir = os.path.join(default_destdir, default_fishfunc) if not os.path.exists(share_dir): return @@ -122,7 +128,7 @@ def remove_system_installation(dryrun=False): rm(os.path.join(bin_dir, 'autojump_utils.py'), dryrun) rm(os.path.join(etc_dir, 'autojump.sh'), dryrun) rm(os.path.join(share_dir, 'autojump.bash'), dryrun) - rm(os.path.join(share_dir, 'autojump.fish'), dryrun) + rm(os.path.join(fishfunc_dir, 'j.fish'), dryrun) rm(os.path.join(share_dir, 'autojump.tcsh'), dryrun) rm(os.path.join(share_dir, 'autojump.zsh'), dryrun) rm(os.path.join(zshshare_dir, '_j'), dryrun)