from .generic import Generic


class Powershell(Generic):
    def app_alias(self, fuck):
        return 'function ' + fuck + ' { \n' \
               '    $fuck = $(thefuck (Get-History -Count 1).CommandLine);\n' \
               '    if (-not [string]::IsNullOrWhiteSpace($fuck)) {\n' \
               '        if ($fuck.StartsWith("echo")) { $fuck = $fuck.Substring(5); }\n' \
               '        else { iex "$fuck"; }\n' \
               '    }\n' \
               '}\n'

    def and_(self, *commands):
        return u' -and '.join('({0})'.format(c) for c in commands)

    def how_to_configure(self):
        return {
            'content': 'iex "thefuck --alias"',
            'path': '$profile',
            'reload': '& $profile',
        }

from .generic import Generic


class Powershell(Generic):
    def app_alias(self, fuck):
        return 'function ' + fuck + ' {\n' \
               '    $history = (Get-History -Count 1).CommandLine;\n' \
               '    if (-not [string]::IsNullOrWhiteSpace($history)) {\n' \
               '        $fuck = $(thefuck $history);\n' \
               '        if (-not [string]::IsNullOrWhiteSpace($fuck)) {\n' \
               '            if ($fuck.StartsWith("echo")) { $fuck = $fuck.Substring(5); }\n' \
               '            else { iex "$fuck"; }\n' \
               '        }\n' \
               '    }\n' \
               '}\n'

    def and_(self, *commands):
        return u' -and '.join('({0})'.format(c) for c in commands)

    def how_to_configure(self):
        return {
            'content': 'iex "thefuck --alias"',
            'path': '$profile',
            'reload': '& $profile',
        }

Update PowerShell alias to handle no history
Update PowerShell alias to handle no history

If history is cleared (or the shell is new and there is no history),
invoking thefuck results in an error because the alias attempts to execute
the usage string.

The fix is to check if Get-History returns anything before invoking
thefuck.

  from .generic import Generic
  
  
  class Powershell(Generic):
      def app_alias(self, fuck):
-         return 'function ' + fuck + ' { \n' \
+         return 'function ' + fuck + ' {\n' \
-                '    $fuck = $(thefuck (Get-History -Count 1).CommandLine);\n' \
+                '    $history = (Get-History -Count 1).CommandLine;\n' \
+                '    if (-not [string]::IsNullOrWhiteSpace($history)) {\n' \
+                '        $fuck = $(thefuck $history);\n' \
-                '    if (-not [string]::IsNullOrWhiteSpace($fuck)) {\n' \
+                '        if (-not [string]::IsNullOrWhiteSpace($fuck)) {\n' \
-                '        if ($fuck.StartsWith("echo")) { $fuck = $fuck.Substring(5); }\n' \
+                '            if ($fuck.StartsWith("echo")) { $fuck = $fuck.Substring(5); }\n' \
-                '        else { iex "$fuck"; }\n' \
+                '            else { iex "$fuck"; }\n' \
+                '        }\n' \
                 '    }\n' \
                 '}\n'
  
      def and_(self, *commands):
          return u' -and '.join('({0})'.format(c) for c in commands)
  
      def how_to_configure(self):
          return {
              'content': 'iex "thefuck --alias"',
              'path': '$profile',
              'reload': '& $profile',
          }
  
Update PowerShell alias to handle no history
## Code Before:
from .generic import Generic


class Powershell(Generic):
    def app_alias(self, fuck):
        return 'function ' + fuck + ' { \n' \
               '    $fuck = $(thefuck (Get-History -Count 1).CommandLine);\n' \
               '    if (-not [string]::IsNullOrWhiteSpace($fuck)) {\n' \
               '        if ($fuck.StartsWith("echo")) { $fuck = $fuck.Substring(5); }\n' \
               '        else { iex "$fuck"; }\n' \
               '    }\n' \
               '}\n'

    def and_(self, *commands):
        return u' -and '.join('({0})'.format(c) for c in commands)

    def how_to_configure(self):
        return {
            'content': 'iex "thefuck --alias"',
            'path': '$profile',
            'reload': '& $profile',
        }

## Instruction:
Update PowerShell alias to handle no history
## Code After:
from .generic import Generic


class Powershell(Generic):
    def app_alias(self, fuck):
        return 'function ' + fuck + ' {\n' \
               '    $history = (Get-History -Count 1).CommandLine;\n' \
               '    if (-not [string]::IsNullOrWhiteSpace($history)) {\n' \
               '        $fuck = $(thefuck $history);\n' \
               '        if (-not [string]::IsNullOrWhiteSpace($fuck)) {\n' \
               '            if ($fuck.StartsWith("echo")) { $fuck = $fuck.Substring(5); }\n' \
               '            else { iex "$fuck"; }\n' \
               '        }\n' \
               '    }\n' \
               '}\n'

    def and_(self, *commands):
        return u' -and '.join('({0})'.format(c) for c in commands)

    def how_to_configure(self):
        return {
            'content': 'iex "thefuck --alias"',
            'path': '$profile',
            'reload': '& $profile',
        }
