Commit 899f0fde authored by Yannick Kirschhoffer's avatar Yannick Kirschhoffer

#18: Fetch a general tty capacity detection code and integrate it in the parser module.

parent 51d778a0
import argparse
import os
import sys
from typing import Tuple
from docker_ascii_map import __version__
# This code is taken from Django
def supports_color():
"""
Returns True if the running system's terminal supports color, and False
otherwise.
"""
plat = sys.platform
supported_platform = plat != 'Pocket PC' and (plat != 'win32' or 'ANSICON' in os.environ)
# isatty is not always implemented, #6223.
is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
if not supported_platform or not is_a_tty:
return False
return True
def get_input_parameters() -> Tuple[bool]:
parser = argparse.ArgumentParser(description='Display the docker host contents on a visual map.')
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('-c', '--color', action='store_const', const='color', help='Use color in map display')
parser.add_argument('-m', '--mono', action='store_const', const='mono', help='Render the map in monochrome')
terminal = os.getenv('TERM')
args = parser.parse_args()
color_mode = False
if terminal:
color_mode = 'color' in terminal
color_mode = supports_color()
if args.color:
color_mode = True
......@@ -24,4 +35,4 @@ def get_input_parameters() -> Tuple[bool]:
if args.mono:
color_mode = False
return color_mode, None
return color_mode,
......@@ -2,6 +2,8 @@ import unittest
from argparse import ArgumentParser
from unittest.mock import patch
import sys
import docker_ascii_map.argument_parser
......@@ -13,16 +15,29 @@ class Args:
class ParametersTests(unittest.TestCase):
def test_monoterm(self):
with patch('os.getenv', return_value='vt100') as mock_getenv:
with patch.object(ArgumentParser, 'parse_args', return_value=Args()) as mock_parse:
r = docker_ascii_map.argument_parser.get_input_parameters()
self.assertEqual(False, r[0])
with patch('sys.platform', return_value='linux') as mock_getenv, \
patch.object(sys.stdout, 'isatty', return_value=False) as mock_stdout, \
patch.object(ArgumentParser, 'parse_args', return_value=Args()) as mock_parse:
r = docker_ascii_map.argument_parser.get_input_parameters()
self.assertEqual(False, r[0])
def test_colorterm(self):
with patch('os.getenv', return_value='xterm-256color') as mock_getenv:
with patch.object(ArgumentParser, 'parse_args', return_value=Args()) as mock_parse:
r = docker_ascii_map.argument_parser.get_input_parameters()
self.assertEqual(True, r[0])
with patch('sys.platform', return_value='linux') as mock_getenv, \
patch.object(sys.stdout, 'isatty', return_value=True) as mock_stdout, \
patch.object(ArgumentParser, 'parse_args', return_value=Args()) as mock_parse:
r = docker_ascii_map.argument_parser.get_input_parameters()
self.assertEqual(True, r[0])
def test_colorterm_win(self):
with patch('sys.platform', return_value='win32') as mock_getenv, \
patch.object(sys.stdout, 'isatty', return_value=True) as mock_stdout, \
patch('os.environ', return_value=['ANSICON']) as mock_environ, \
patch.object(ArgumentParser, 'parse_args', return_value=Args()) as mock_parse:
r = docker_ascii_map.argument_parser.get_input_parameters()
self.assertEqual(True, r[0])
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment