#N/A: Improve how version is fetched for all shells (#920)

This commit is contained in:
Pablo Aguiar
2019-05-27 18:24:55 +02:00
committed by Vladimir Iakovlev
parent ba949f7fd9
commit ff2944086d
12 changed files with 137 additions and 23 deletions
+5 -4
View File
@@ -9,6 +9,8 @@ from .generic import Generic
class Bash(Generic):
friendly_name = 'Bash'
def app_alias(self, alias_name):
# It is VERY important to have the variables declared WITHIN the function
return '''
@@ -83,9 +85,8 @@ class Bash(Generic):
path=config,
reload=u'source {}'.format(config))
def info(self):
"""Returns the name and version of the current shell"""
def _get_version(self):
"""Returns the version of the current shell"""
proc = Popen(['bash', '-c', 'echo $BASH_VERSION'],
stdout=PIPE, stderr=DEVNULL)
version = proc.stdout.read().decode('utf-8').strip()
return u'Bash {}'.format(version)
return proc.stdout.read().decode('utf-8').strip()
+6 -6
View File
@@ -38,6 +38,8 @@ def _get_aliases(overridden):
class Fish(Generic):
friendly_name = 'Fish Shell'
def _get_overridden_aliases(self):
overridden = os.environ.get('THEFUCK_OVERRIDDEN_ALIASES',
os.environ.get('TF_OVERRIDDEN_ALIASES', ''))
@@ -104,12 +106,10 @@ class Fish(Generic):
path='~/.config/fish/config.fish',
reload='fish')
def info(self):
"""Returns the name and version of the current shell"""
proc = Popen(['fish', '--version'],
stdout=PIPE, stderr=DEVNULL)
version = proc.stdout.read().decode('utf-8').split()[-1]
return u'Fish Shell {}'.format(version)
def _get_version(self):
"""Returns the version of the current shell"""
proc = Popen(['fish', '--version'], stdout=PIPE, stderr=DEVNULL)
return proc.stdout.read().decode('utf-8').split()[-1]
def put_to_history(self, command):
try:
+12 -1
View File
@@ -14,6 +14,8 @@ ShellConfiguration = namedtuple('ShellConfiguration', (
class Generic(object):
friendly_name = 'Generic Shell'
def get_aliases(self):
return {}
@@ -131,9 +133,18 @@ class Generic(object):
'type', 'typeset', 'ulimit', 'umask', 'unalias', 'unset',
'until', 'wait', 'while']
def _get_version(self):
"""Returns the version of the current shell"""
return ''
def info(self):
"""Returns the name and version of the current shell"""
return 'Generic Shell'
try:
version = self._get_version()
except Exception as e:
warn(u'Could not determine shell version: {}'.format(e))
version = ''
return u'{} {}'.format(self.friendly_name, version).rstrip()
def _create_shell_configuration(self, content, path, reload):
return ShellConfiguration(
+17
View File
@@ -1,7 +1,11 @@
from subprocess import Popen, PIPE
from ..utils import DEVNULL
from .generic import Generic, ShellConfiguration
class Powershell(Generic):
friendly_name = 'PowerShell'
def app_alias(self, alias_name):
return 'function ' + alias_name + ' {\n' \
' $history = (Get-History -Count 1).CommandLine;\n' \
@@ -24,3 +28,16 @@ class Powershell(Generic):
path='$profile',
reload='& $profile',
can_configure_automatically=False)
def _get_version(self):
"""Returns the version of the current shell"""
try:
proc = Popen(
['powershell.exe', '$PSVersionTable.PSVersion'],
stdout=PIPE,
stderr=DEVNULL)
version = proc.stdout.read().decode('utf-8').rstrip().split('\n')
return '.'.join(version[-1].split())
except IOError:
proc = Popen(['pwsh', '--version'], stdout=PIPE, stderr=DEVNULL)
return proc.stdout.read().decode('utf-8').split()[-1]
+7
View File
@@ -6,6 +6,8 @@ from .generic import Generic
class Tcsh(Generic):
friendly_name = 'Tcsh'
def app_alias(self, alias_name):
return ("alias {0} 'setenv TF_SHELL tcsh && setenv TF_ALIAS {0} && "
"set fucked_cmd=`history -h 2 | head -n 1` && "
@@ -35,3 +37,8 @@ class Tcsh(Generic):
content=u'eval `thefuck --alias`',
path='~/.tcshrc',
reload='tcsh')
def _get_version(self):
"""Returns the version of the current shell"""
proc = Popen(['tcsh', '--version'], stdout=PIPE, stderr=DEVNULL)
return proc.stdout.read().decode('utf-8').split()[1]
+5 -4
View File
@@ -10,6 +10,8 @@ from .generic import Generic
class Zsh(Generic):
friendly_name = 'ZSH'
def app_alias(self, alias_name):
# It is VERY important to have the variables declared WITHIN the function
return '''
@@ -87,9 +89,8 @@ class Zsh(Generic):
path='~/.zshrc',
reload='source ~/.zshrc')
def info(self):
"""Returns the name and version of the current shell"""
def _get_version(self):
"""Returns the version of the current shell"""
proc = Popen(['zsh', '-c', 'echo $ZSH_VERSION'],
stdout=PIPE, stderr=DEVNULL)
version = proc.stdout.read().decode('utf-8').strip()
return u'ZSH {}'.format(version)
return proc.stdout.read().decode('utf-8').strip()