b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | From dffcc1c5823bcce10b420467db41e42ec41f4702 Mon Sep 17 00:00:00 2001 |
| 2 | From: Jeffery To <jeffery.to@gmail.com> |
| 3 | Date: Thu, 9 Nov 2023 17:48:50 +0800 |
| 4 | Subject: [PATCH 1/2] Use Sh as base class for Bash and Zsh |
| 5 | |
| 6 | --- |
| 7 | userpath/shells.py | 41 ++++++++++++++++++++++++++--------------- |
| 8 | 1 file changed, 26 insertions(+), 15 deletions(-) |
| 9 | |
| 10 | --- a/userpath/shells.py |
| 11 | +++ b/userpath/shells.py |
| 12 | @@ -12,24 +12,36 @@ class Shell(object): |
| 13 | |
| 14 | |
| 15 | class Sh(Shell): |
| 16 | - def config(self, location, front=True): |
| 17 | + name = 'sh' |
| 18 | + |
| 19 | + def _config_contents(self, location, front=True): |
| 20 | head, tail = (location, '$PATH') if front else ('$PATH', location) |
| 21 | new_path = '{}{}{}'.format(head, pathsep, tail) |
| 22 | + return 'export PATH="{}"'.format(new_path) |
| 23 | + |
| 24 | + def config(self, location, front=True): |
| 25 | + contents = self._config_contents(location, front=front) |
| 26 | + return {path.join(self.home, '.profile'): contents} |
| 27 | |
| 28 | - return {path.join(self.home, '.profile'): 'PATH="{}"'.format(new_path)} |
| 29 | + @classmethod |
| 30 | + def _interactive_show_path_command(cls): |
| 31 | + return [cls.name, '-i', '-c', 'echo $PATH'] |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def _interactive_login_show_path_command(cls): |
| 35 | + return [cls.name, '-i', '-l', '-c', 'echo $PATH'] |
| 36 | |
| 37 | @classmethod |
| 38 | def show_path_commands(cls): |
| 39 | # TODO: Find out what file influences non-login shells. The issue may simply be our Docker setup. |
| 40 | - return [['sh', '-i', '-l', '-c', 'echo $PATH']] |
| 41 | + return [cls._interactive_login_show_path_command()] |
| 42 | |
| 43 | |
| 44 | -class Bash(Shell): |
| 45 | - def config(self, location, front=True): |
| 46 | - head, tail = (location, '$PATH') if front else ('$PATH', location) |
| 47 | - new_path = '{}{}{}'.format(head, pathsep, tail) |
| 48 | - contents = 'export PATH="{}"'.format(new_path) |
| 49 | +class Bash(Sh): |
| 50 | + name = 'bash' |
| 51 | |
| 52 | + def config(self, location, front=True): |
| 53 | + contents = self._config_contents(location, front=front) |
| 54 | configs = {path.join(self.home, '.bashrc'): contents} |
| 55 | |
| 56 | # https://github.com/ofek/userpath/issues/3#issuecomment-492491977 |
| 57 | @@ -50,7 +62,7 @@ class Bash(Shell): |
| 58 | |
| 59 | @classmethod |
| 60 | def show_path_commands(cls): |
| 61 | - return [['bash', '-i', '-c', 'echo $PATH'], ['bash', '-i', '-l', '-c', 'echo $PATH']] |
| 62 | + return [cls._interactive_show_path_command(), cls._interactive_login_show_path_command()] |
| 63 | |
| 64 | |
| 65 | class Fish(Shell): |
| 66 | @@ -88,18 +100,17 @@ class Xonsh(Shell): |
| 67 | return [['xonsh', '-i', '-c', command], ['xonsh', '-i', '--login', '-c', command]] |
| 68 | |
| 69 | |
| 70 | -class Zsh(Shell): |
| 71 | - def config(self, location, front=True): |
| 72 | - head, tail = (location, '$PATH') if front else ('$PATH', location) |
| 73 | - new_path = '{}{}{}'.format(head, pathsep, tail) |
| 74 | - contents = 'export PATH="{}"'.format(new_path) |
| 75 | +class Zsh(Sh): |
| 76 | + name = 'zsh' |
| 77 | |
| 78 | + def config(self, location, front=True): |
| 79 | + contents = self._config_contents(location, front=front) |
| 80 | zdotdir = environ.get('ZDOTDIR', self.home) |
| 81 | return {path.join(zdotdir, '.zshrc'): contents, path.join(zdotdir, '.zprofile'): contents} |
| 82 | |
| 83 | @classmethod |
| 84 | def show_path_commands(cls): |
| 85 | - return [['zsh', '-i', '-c', 'echo $PATH'], ['zsh', '-i', '-l', '-c', 'echo $PATH']] |
| 86 | + return [cls._interactive_show_path_command(), cls._interactive_login_show_path_command()] |
| 87 | |
| 88 | |
| 89 | SHELLS = { |