import enum
import os
import subprocess
import sys

import blessings

from .misc import isatty


if isatty(sys.stdout) and os.getenv("TERM"):
    Terminal = blessings.Terminal
else:

    class Terminal:
        def __getattr__(self, name):
            return ""


TERM = Terminal()


class Color(enum.Enum):

    none = ""
    reset = TERM.normal
    black = TERM.black
    red = TERM.red
    green = TERM.green
    yellow = TERM.yellow
    blue = TERM.blue
    magenta = TERM.magenta
    cyan = TERM.cyan
    white = TERM.white

    def __str__(self):
        return self.value


class StreamOptions(enum.Enum):

    """Choices for stream handling."""

    capture = "capture"
    hide = "hide"
    none = "none"

    @property
    def option(self):
        return {
            "capture": subprocess.PIPE,
            "hide": subprocess.DEVNULL,
            "none": None,
        }[self.value]

import enum
import os
import subprocess
import sys

import blessings

from .misc import isatty


if isatty(sys.stdout) and os.getenv("TERM"):
    Terminal = blessings.Terminal
else:
    # XXX: Mock terminal that returns "" for all attributes
    class TerminalValue:
        registry = {}

        @classmethod
        def get(cls, name):
            if name not in cls.registry:
                cls.registry[name] = cls(name)
            return cls.registry[name]

        def __init__(self, name):
            self.name = name

        def __repr__(self):
            return f"{self.__class__.__name__}({self.name})"

        def __str__(self):
            return ""

    class Terminal:
        def __getattr__(self, name):
            return TerminalValue.get(name)


TERM = Terminal()


class Color(enum.Enum):

    none = ""
    reset = TERM.normal
    black = TERM.black
    red = TERM.red
    green = TERM.green
    yellow = TERM.yellow
    blue = TERM.blue
    magenta = TERM.magenta
    cyan = TERM.cyan
    white = TERM.white

    def __str__(self):
        return str(self.value)


class StreamOptions(enum.Enum):

    """Choices for stream handling."""

    capture = "capture"
    hide = "hide"
    none = "none"

    @property
    def option(self):
        return {
            "capture": subprocess.PIPE,
            "hide": subprocess.DEVNULL,
            "none": None,
        }[self.value]

Fix Color enum setup when TERM isn't set
Fix Color enum setup when TERM isn't set

The previous version of this didn't work right because all the values
were the same empty string.

This works around that by creating distinct values that evaluate to "".

Amends 94b55ead63523f7f5677989f1a4999994b205cdf

  import enum
  import os
  import subprocess
  import sys
  
  import blessings
  
  from .misc import isatty
  
  
  if isatty(sys.stdout) and os.getenv("TERM"):
      Terminal = blessings.Terminal
  else:
+     # XXX: Mock terminal that returns "" for all attributes
+     class TerminalValue:
+         registry = {}
+ 
+         @classmethod
+         def get(cls, name):
+             if name not in cls.registry:
+                 cls.registry[name] = cls(name)
+             return cls.registry[name]
+ 
+         def __init__(self, name):
+             self.name = name
+ 
+         def __repr__(self):
+             return f"{self.__class__.__name__}({self.name})"
+ 
+         def __str__(self):
+             return ""
  
      class Terminal:
          def __getattr__(self, name):
-             return ""
+             return TerminalValue.get(name)
  
  
  TERM = Terminal()
  
  
  class Color(enum.Enum):
  
      none = ""
      reset = TERM.normal
      black = TERM.black
      red = TERM.red
      green = TERM.green
      yellow = TERM.yellow
      blue = TERM.blue
      magenta = TERM.magenta
      cyan = TERM.cyan
      white = TERM.white
  
      def __str__(self):
-         return self.value
+         return str(self.value)
  
  
  class StreamOptions(enum.Enum):
  
      """Choices for stream handling."""
  
      capture = "capture"
      hide = "hide"
      none = "none"
  
      @property
      def option(self):
          return {
              "capture": subprocess.PIPE,
              "hide": subprocess.DEVNULL,
              "none": None,
          }[self.value]
  
Fix Color enum setup when TERM isn't set
## Code Before:
import enum
import os
import subprocess
import sys

import blessings

from .misc import isatty


if isatty(sys.stdout) and os.getenv("TERM"):
    Terminal = blessings.Terminal
else:

    class Terminal:
        def __getattr__(self, name):
            return ""


TERM = Terminal()


class Color(enum.Enum):

    none = ""
    reset = TERM.normal
    black = TERM.black
    red = TERM.red
    green = TERM.green
    yellow = TERM.yellow
    blue = TERM.blue
    magenta = TERM.magenta
    cyan = TERM.cyan
    white = TERM.white

    def __str__(self):
        return self.value


class StreamOptions(enum.Enum):

    """Choices for stream handling."""

    capture = "capture"
    hide = "hide"
    none = "none"

    @property
    def option(self):
        return {
            "capture": subprocess.PIPE,
            "hide": subprocess.DEVNULL,
            "none": None,
        }[self.value]

## Instruction:
Fix Color enum setup when TERM isn't set
## Code After:
import enum
import os
import subprocess
import sys

import blessings

from .misc import isatty


if isatty(sys.stdout) and os.getenv("TERM"):
    Terminal = blessings.Terminal
else:
    # XXX: Mock terminal that returns "" for all attributes
    class TerminalValue:
        registry = {}

        @classmethod
        def get(cls, name):
            if name not in cls.registry:
                cls.registry[name] = cls(name)
            return cls.registry[name]

        def __init__(self, name):
            self.name = name

        def __repr__(self):
            return f"{self.__class__.__name__}({self.name})"

        def __str__(self):
            return ""

    class Terminal:
        def __getattr__(self, name):
            return TerminalValue.get(name)


TERM = Terminal()


class Color(enum.Enum):

    none = ""
    reset = TERM.normal
    black = TERM.black
    red = TERM.red
    green = TERM.green
    yellow = TERM.yellow
    blue = TERM.blue
    magenta = TERM.magenta
    cyan = TERM.cyan
    white = TERM.white

    def __str__(self):
        return str(self.value)


class StreamOptions(enum.Enum):

    """Choices for stream handling."""

    capture = "capture"
    hide = "hide"
    none = "none"

    @property
    def option(self):
        return {
            "capture": subprocess.PIPE,
            "hide": subprocess.DEVNULL,
            "none": None,
        }[self.value]
