mirror of
				https://github.com/nvbn/thefuck.git
				synced 2025-10-30 22:54:14 +00:00 
			
		
		
		
	Merge pull request #215 from scorphus/fish-functions
Cache aliases to speed up subsequent calls and add support to Fish functions
This commit is contained in:
		| @@ -78,6 +78,12 @@ class TestFish(object): | |||||||
|     def shell(self): |     def shell(self): | ||||||
|         return shells.Fish() |         return shells.Fish() | ||||||
|  |  | ||||||
|  |     @pytest.fixture(autouse=True) | ||||||
|  |     def Popen(self, mocker): | ||||||
|  |         mock = mocker.patch('thefuck.shells.Popen') | ||||||
|  |         mock.return_value.stdout.read.return_value = (b'funced\nfuncsave\ngrep') | ||||||
|  |         return mock | ||||||
|  |  | ||||||
|     @pytest.mark.parametrize('before, after', [ |     @pytest.mark.parametrize('before, after', [ | ||||||
|         ('pwd', 'pwd'), |         ('pwd', 'pwd'), | ||||||
|         ('ll', 'll')])  # Fish has no aliases but functions |         ('ll', 'll')])  # Fish has no aliases but functions | ||||||
| @@ -98,7 +104,9 @@ class TestFish(object): | |||||||
|         assert shell.and_('foo', 'bar') == 'foo; and bar' |         assert shell.and_('foo', 'bar') == 'foo; and bar' | ||||||
|  |  | ||||||
|     def test_get_aliases(self, shell): |     def test_get_aliases(self, shell): | ||||||
|         assert shell.get_aliases() == {} |         assert shell.get_aliases() == {'funced': 'funced', | ||||||
|  |                                        'funcsave': 'funcsave', | ||||||
|  |                                        'grep': 'grep'} | ||||||
|  |  | ||||||
|  |  | ||||||
| @pytest.mark.usefixtures('isfile') | @pytest.mark.usefixtures('isfile') | ||||||
|   | |||||||
| @@ -12,8 +12,10 @@ from .utils import DEVNULL | |||||||
|  |  | ||||||
|  |  | ||||||
| class Generic(object): | class Generic(object): | ||||||
|  |     _aliases = {} | ||||||
|  |  | ||||||
|     def get_aliases(self): |     def get_aliases(self): | ||||||
|         return {} |         return self._aliases | ||||||
|  |  | ||||||
|     def _expand_aliases(self, command_script): |     def _expand_aliases(self, command_script): | ||||||
|         aliases = self.get_aliases() |         aliases = self.get_aliases() | ||||||
| @@ -62,12 +64,16 @@ class Bash(Generic): | |||||||
|         return name, value |         return name, value | ||||||
|  |  | ||||||
|     def get_aliases(self): |     def get_aliases(self): | ||||||
|         proc = Popen('bash -ic alias', stdout=PIPE, stderr=DEVNULL, shell=True) |         if not self._aliases: | ||||||
|         return dict( |             proc = Popen('bash -ic alias', stdout=PIPE, stderr=DEVNULL, | ||||||
|  |                          shell=True) | ||||||
|  |             self._aliases = dict( | ||||||
|                 self._parse_alias(alias) |                 self._parse_alias(alias) | ||||||
|                 for alias in proc.stdout.read().decode('utf-8').split('\n') |                 for alias in proc.stdout.read().decode('utf-8').split('\n') | ||||||
|                 if alias and '=' in alias) |                 if alias and '=' in alias) | ||||||
|  |  | ||||||
|  |         return self._aliases | ||||||
|  |  | ||||||
|     def _get_history_file_name(self): |     def _get_history_file_name(self): | ||||||
|         return os.environ.get("HISTFILE", |         return os.environ.get("HISTFILE", | ||||||
|                               os.path.expanduser('~/.bash_history')) |                               os.path.expanduser('~/.bash_history')) | ||||||
| @@ -91,6 +97,15 @@ class Fish(Generic): | |||||||
|                 "    end\n" |                 "    end\n" | ||||||
|                 "end") |                 "end") | ||||||
|  |  | ||||||
|  |     def get_aliases(self): | ||||||
|  |         if not self._aliases: | ||||||
|  |             proc = Popen('fish -ic functions', stdout=PIPE, stderr=DEVNULL, | ||||||
|  |                          shell=True) | ||||||
|  |             functions = proc.stdout.read().decode('utf-8').strip().split('\n') | ||||||
|  |             self._aliases = dict((function, function) for function in functions) | ||||||
|  |  | ||||||
|  |         return self._aliases | ||||||
|  |  | ||||||
|     def _get_history_file_name(self): |     def _get_history_file_name(self): | ||||||
|         return os.path.expanduser('~/.config/fish/fish_history') |         return os.path.expanduser('~/.config/fish/fish_history') | ||||||
|  |  | ||||||
| @@ -112,12 +127,16 @@ class Zsh(Generic): | |||||||
|         return name, value |         return name, value | ||||||
|  |  | ||||||
|     def get_aliases(self): |     def get_aliases(self): | ||||||
|         proc = Popen('zsh -ic alias', stdout=PIPE, stderr=DEVNULL, shell=True) |         if not self._aliases: | ||||||
|         return dict( |             proc = Popen('zsh -ic alias', stdout=PIPE, stderr=DEVNULL, | ||||||
|  |                          shell=True) | ||||||
|  |             self._aliases = dict( | ||||||
|                 self._parse_alias(alias) |                 self._parse_alias(alias) | ||||||
|                 for alias in proc.stdout.read().decode('utf-8').split('\n') |                 for alias in proc.stdout.read().decode('utf-8').split('\n') | ||||||
|                 if alias and '=' in alias) |                 if alias and '=' in alias) | ||||||
|  |  | ||||||
|  |         return self._aliases | ||||||
|  |  | ||||||
|     def _get_history_file_name(self): |     def _get_history_file_name(self): | ||||||
|         return os.environ.get("HISTFILE", |         return os.environ.get("HISTFILE", | ||||||
|                               os.path.expanduser('~/.zsh_history')) |                               os.path.expanduser('~/.zsh_history')) | ||||||
| @@ -135,12 +154,16 @@ class Tcsh(Generic): | |||||||
|         return name, value |         return name, value | ||||||
|  |  | ||||||
|     def get_aliases(self): |     def get_aliases(self): | ||||||
|         proc = Popen('tcsh -ic alias', stdout=PIPE, stderr=DEVNULL, shell=True) |         if not self._aliases: | ||||||
|         return dict( |             proc = Popen('tcsh -ic alias', stdout=PIPE, stderr=DEVNULL, | ||||||
|  |                          shell=True) | ||||||
|  |             self._aliases = dict( | ||||||
|                 self._parse_alias(alias) |                 self._parse_alias(alias) | ||||||
|                 for alias in proc.stdout.read().decode('utf-8').split('\n') |                 for alias in proc.stdout.read().decode('utf-8').split('\n') | ||||||
|                 if alias and '\t' in alias) |                 if alias and '\t' in alias) | ||||||
|  |  | ||||||
|  |         return self._aliases | ||||||
|  |  | ||||||
|     def _get_history_file_name(self): |     def _get_history_file_name(self): | ||||||
|         return os.environ.get("HISTFILE", |         return os.environ.get("HISTFILE", | ||||||
|                               os.path.expanduser('~/.history')) |                               os.path.expanduser('~/.history')) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user