diff --git a/PythonicGcodeMachine/Gcode/Rs274/Ast.py b/PythonicGcodeMachine/Gcode/Rs274/Ast.py index 9a64839375040f27aabafed3464e60fbdef7d33a..c974f3157006e8ef1405054d1c1cdef55b516bc6 100644 --- a/PythonicGcodeMachine/Gcode/Rs274/Ast.py +++ b/PythonicGcodeMachine/Gcode/Rs274/Ast.py @@ -65,6 +65,8 @@ __all__ = [ import math +import colors + #################################################################################################### class Program: @@ -128,7 +130,9 @@ class Program: #################################################################################################### class LineItem: - pass + + def ansi_str(self): + return str(self) #################################################################################################### @@ -149,6 +153,14 @@ class Line: """ + ANSI_DELETED = colors.red + ANSI_LINE_NUMBER = colors.blue + ANSI_COMMENT = colors.green + ANSI_SETTING = colors.blue + ANSI_G = colors.red + ANSI_X = colors.blue + ANSI_VALUE = colors.black + ############################################## def __init__(self, deleted=False, line_number=None, comment=None): @@ -248,6 +260,22 @@ class Line: return line + ############################################## + + def ansi_str(self): + + line = '' + if not self: + # line += self.ANSI_DELETED('/ ') + return self.ANSI_DELETED(str(self)) + if self._line_number: + line += self.ANSI_LINE_NUMBER('N{} '.format(self._line_number)) + line += ' '.join([item.ansi_str() for item in self]) + if self._comment: + line += ' ' + self.ANSI_COMMENT('; ' + self._comment) + + return line + #################################################################################################### class Comment(LineItem): @@ -282,6 +310,9 @@ class Comment(LineItem): def __str__(self): return '({0._text})'.format(self) + def ansi_str(self): + return Line.ANSI_COMMENT(str(self)) + #################################################################################################### class Word(LineItem): @@ -329,6 +360,13 @@ class Word(LineItem): def __str__(self): return '{0._letter}{0._value}'.format(self) + def ansi_str(self): + + if self._letter in 'GM': + return Line.ANSI_G(str(self)) + else: + return Line.ANSI_X(self._letter) + Line.ANSI_VALUE(str(self._value)) + #################################################################################################### class RealValue: @@ -385,6 +423,9 @@ class ParameterSetting(LineItem, ParameterMixin): def __str__(self): return '#{0._parameter}={0._value}'.format(self) + def ansi_str(self): + return Line.ANSI_SETTING('#{0._parameter}='.format(self)) + Line.ANSI_VALUE(str(self._value)) + #################################################################################################### class Parameter(RealValue, ParameterMixin): diff --git a/requirements.txt b/requirements.txt index 954b8b705610bab484efd5afbded58ca48d3dd57..6e4516b3dc77fc40edd23c647522fb412356bb1e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ +ansicolors ply>=3.11 PyYAML>=3.13 diff --git a/unit-test/Gcode/test_Parser.py b/unit-test/Gcode/test_Parser.py index ee5245cee41cb22cbe56ddfeb442c250baa566ec..70f4f9227c967713b3079b6020c27b06b5fb638b 100644 --- a/unit-test/Gcode/test_Parser.py +++ b/unit-test/Gcode/test_Parser.py @@ -115,7 +115,7 @@ class TestGcodeParser(unittest.TestCase): print(gcode) try: line = parser.parse(gcode) - print('>', line) + print('>', line.ansi_str()) except GcodeParserError as exception: position, = exception.args print(' ' * position + '^')