eric6/MicroPython/MicroPythonReplWidget.py

branch
micropython
changeset 7069
a09a30251d4e
parent 7067
3fc4082fc6ba
child 7077
3b7475b7a1ef
--- a/eric6/MicroPython/MicroPythonReplWidget.py	Sat Jul 20 14:46:15 2019 +0200
+++ b/eric6/MicroPython/MicroPythonReplWidget.py	Sat Jul 20 14:47:24 2019 +0200
@@ -15,7 +15,7 @@
     pyqtSlot, pyqtSignal, Qt, QPoint, QEvent, QIODevice, QTimer
 )
 from PyQt5.QtGui import (
-    QColor, QKeySequence, QTextCursor, QBrush, QTextCharFormat
+    QColor, QKeySequence, QTextCursor, QBrush
 )
 from PyQt5.QtWidgets import (
     QWidget, QMenu, QApplication, QHBoxLayout, QSpacerItem, QSizePolicy
@@ -43,6 +43,100 @@
 import UI.PixmapCache
 import Preferences
 
+# ANSI Colors (see https://en.wikipedia.org/wiki/ANSI_escape_code)
+AnsiColorSchemes = {
+    "Windows 7": {
+        0: QBrush(QColor(0, 0, 0)),
+        1: QBrush(QColor(128, 0, 0)),
+        2: QBrush(QColor(0, 128, 0)),
+        3: QBrush(QColor(128, 128, 0)),
+        4: QBrush(QColor(0, 0, 128)),
+        5: QBrush(QColor(128, 0, 128)),
+        6: QBrush(QColor(0, 128, 128)),
+        7: QBrush(QColor(192, 192, 192)),
+        10: QBrush(QColor(128, 128, 128)),
+        11: QBrush(QColor(255, 0, 0)),
+        12: QBrush(QColor(0, 255, 0)),
+        13: QBrush(QColor(255, 255, 0)),
+        14: QBrush(QColor(0, 0, 255)),
+        15: QBrush(QColor(255, 0, 255)),
+        16: QBrush(QColor(0, 255, 255)),
+        17: QBrush(QColor(255, 255, 255)),
+    },
+    "Windows 10": {
+        0: QBrush(QColor(12, 12, 12)),
+        1: QBrush(QColor(197, 15, 31)),
+        2: QBrush(QColor(19, 161, 14)),
+        3: QBrush(QColor(193, 156, 0)),
+        4: QBrush(QColor(0, 55, 218)),
+        5: QBrush(QColor(136, 23, 152)),
+        6: QBrush(QColor(58, 150, 221)),
+        7: QBrush(QColor(204, 204, 204)),
+        10: QBrush(QColor(118, 118, 118)),
+        11: QBrush(QColor(231, 72, 86)),
+        12: QBrush(QColor(22, 198, 12)),
+        13: QBrush(QColor(249, 241, 165)),
+        14: QBrush(QColor(59, 12, 255)),
+        15: QBrush(QColor(180, 0, 158)),
+        16: QBrush(QColor(97, 214, 214)),
+        17: QBrush(QColor(242, 242, 242)),
+    },
+    "PuTTY": {
+        0: QBrush(QColor(0, 0, 0)),
+        1: QBrush(QColor(187, 0, 0)),
+        2: QBrush(QColor(0, 187, 0)),
+        3: QBrush(QColor(187, 187, 0)),
+        4: QBrush(QColor(0, 0, 187)),
+        5: QBrush(QColor(187, 0, 187)),
+        6: QBrush(QColor(0, 187, 187)),
+        7: QBrush(QColor(187, 187, 187)),
+        10: QBrush(QColor(85, 85, 85)),
+        11: QBrush(QColor(255, 85, 85)),
+        12: QBrush(QColor(85, 255, 85)),
+        13: QBrush(QColor(255, 255, 85)),
+        14: QBrush(QColor(85, 85, 255)),
+        15: QBrush(QColor(255, 85, 255)),
+        16: QBrush(QColor(85, 255, 255)),
+        17: QBrush(QColor(255, 255, 255)),
+    },
+    "xterm": {
+        0: QBrush(QColor(0, 0, 0)),
+        1: QBrush(QColor(205, 0, 0)),
+        2: QBrush(QColor(0, 205, 0)),
+        3: QBrush(QColor(205, 205, 0)),
+        4: QBrush(QColor(0, 0, 238)),
+        5: QBrush(QColor(205, 0, 205)),
+        6: QBrush(QColor(0, 205, 205)),
+        7: QBrush(QColor(229, 229, 229)),
+        10: QBrush(QColor(127, 127, 127)),
+        11: QBrush(QColor(255, 0, 0)),
+        12: QBrush(QColor(0, 255, 0)),
+        13: QBrush(QColor(255, 255, 0)),
+        14: QBrush(QColor(0, 0, 255)),
+        15: QBrush(QColor(255, 0, 255)),
+        16: QBrush(QColor(0, 255, 255)),
+        17: QBrush(QColor(255, 255, 255)),
+    },
+    "Ubuntu": {
+        0: QBrush(QColor(1, 1, 1)),
+        1: QBrush(QColor(222, 56, 43)),
+        2: QBrush(QColor(57, 181, 74)),
+        3: QBrush(QColor(255, 199, 6)),
+        4: QBrush(QColor(0, 11, 184)),
+        5: QBrush(QColor(118, 38, 113)),
+        6: QBrush(QColor(44, 181, 233)),
+        7: QBrush(QColor(204, 204, 204)),
+        10: QBrush(QColor(128, 128, 128)),
+        11: QBrush(QColor(255, 0, 0)),
+        12: QBrush(QColor(0, 255, 0)),
+        13: QBrush(QColor(255, 255, 0)),
+        14: QBrush(QColor(0, 0, 255)),
+        15: QBrush(QColor(255, 0, 255)),
+        16: QBrush(QColor(0, 255, 255)),
+        17: QBrush(QColor(255, 255, 255)),
+    },
+}
+
 
 class MicroPythonReplWidget(QWidget, Ui_MicroPythonReplWidget):
     """
@@ -59,26 +153,6 @@
     
     dataReceived = pyqtSignal(bytes)
     
-    # ANSI Colors
-    AnsiColors = {
-        0: QBrush(Qt.black),
-        1: QBrush(Qt.darkRed),
-        2: QBrush(Qt.darkGreen),
-        3: QBrush(Qt.darkYellow),
-        4: QBrush(Qt.darkBlue),
-        5: QBrush(Qt.darkMagenta),
-        6: QBrush(Qt.darkCyan),
-        7: QBrush(Qt.lightGray),
-        10: QBrush(Qt.darkGray),
-        11: QBrush(Qt.red),
-        12: QBrush(Qt.green),
-        13: QBrush(Qt.yellow),
-        14: QBrush(Qt.blue),
-        15: QBrush(Qt.magenta),
-        16: QBrush(Qt.cyan),
-        17: QBrush(Qt.white),
-    }
-    
     def __init__(self, parent=None):
         """
         Constructor
@@ -89,6 +163,8 @@
         super(MicroPythonReplWidget, self).__init__(parent)
         self.setupUi(self)
         
+        self.__ui = parent
+        
         self.deviceIconLabel.setPixmap(MicroPythonDevices.getDeviceIcon(
             "", False))
         
@@ -121,8 +197,6 @@
         self.__zoomWidget.valueChanged.connect(self.__doZoom)
         self.__currentZoom = 0
         
-        self.__ui = None
-        
         self.__serial = None
         self.__device = None
         self.setConnected(False)
@@ -143,21 +217,19 @@
         
         self.__populateDeviceTypeComboBox()
         
-        self.replEdit.setAcceptRichText(False)
-        self.replEdit.setUndoRedoEnabled(False)
         self.replEdit.setContextMenuPolicy(Qt.CustomContextMenu)
         
         self.replEdit.installEventFilter(self)
         
         self.replEdit.customContextMenuRequested.connect(
             self.__showContextMenu)
+        self.__ui.preferencesChanged.connect(self.__handlePreferencesChanged)
         
-        font = Preferences.getEditorOtherFonts("MonospacedFont")
-        self.replEdit.setFontFamily(font.family())
-        self.replEdit.setFontPointSize(font.pointSize())
-        self.DefaultCharFormat = self.replEdit.currentCharFormat()
-        self.DefaultForeground = self.DefaultCharFormat.foreground()
-        self.DefaultBackground = self.DefaultCharFormat.background()
+        self.__handlePreferencesChanged()
+        
+        charFormat = self.replEdit.currentCharFormat()
+        self.DefaultForeground = charFormat.foreground()
+        self.DefaultBackground = charFormat.background()
     
     def __populateDeviceTypeComboBox(self):
         """
@@ -188,6 +260,17 @@
         
         self.on_deviceTypeComboBox_activated(0)
     
+    def __handlePreferencesChanged(self):
+        """
+        Private slot to handle a change in preferences.
+        """
+        self.__colorScheme = Preferences.getMicroPython("ColorScheme")
+        
+        self.__font = Preferences.getEditorOtherFonts("MonospacedFont")
+        
+        self.replEdit.setFontFamily(self.__font.family())
+        self.replEdit.setFontPointSize(self.__font.pointSize())
+    
     @pyqtSlot(int)
     def on_deviceTypeComboBox_activated(self, index):
         """
@@ -419,6 +502,12 @@
         while tc.movePosition(QTextCursor.Down):
             pass
         
+        # set the font
+        charFormat = tc.charFormat()
+        charFormat.setFontFamily(self.__font.family())
+        charFormat.setFontPointSize(self.__font.pointSize())
+        tc.setCharFormat(charFormat)
+        
         index = 0
         while index < len(data):
             if data[index] == 8:        # \b
@@ -437,38 +526,53 @@
                     # ++ will be done at end of loop
                     index += match.end() - 1
                     
-                    if match.group("count") == "":
-                        count = 1
-                    else:
-                        count = int(match.group("count"))
-                    
                     action = match.group("action")
-                    if action == "A":       # up
-                        tc.movePosition(QTextCursor.Up, n=count)
-                        self.replEdit.setTextCursor(tc)
-                    elif action == "B":     # down
-                        tc.movePosition(QTextCursor.Down, n=count)
-                        self.replEdit.setTextCursor(tc)
-                    elif action == "C":     # right
-                        tc.movePosition(QTextCursor.Right, n=count)
-                        self.replEdit.setTextCursor(tc)
-                    elif action == "D":     # left
-                        tc.movePosition(QTextCursor.Left, n=count)
-                        self.replEdit.setTextCursor(tc)
+                    if action in "ABCD":
+                        if match.group("count") == "":
+                            count = 1
+                        else:
+                            count = int(match.group("count"))
+                        
+                        if action == "A":       # up
+                            tc.movePosition(QTextCursor.Up, n=count)
+                            self.replEdit.setTextCursor(tc)
+                        elif action == "B":     # down
+                            tc.movePosition(QTextCursor.Down, n=count)
+                            self.replEdit.setTextCursor(tc)
+                        elif action == "C":     # right
+                            tc.movePosition(QTextCursor.Right, n=count)
+                            self.replEdit.setTextCursor(tc)
+                        elif action == "D":     # left
+                            tc.movePosition(QTextCursor.Left, n=count)
+                            self.replEdit.setTextCursor(tc)
                     elif action == "K":     # delete things
-                        if match.group("count") == "":      # delete to eol
+                        if match.group("count") in ("", "0"):
+                            # delete to end of line
                             tc.movePosition(QTextCursor.EndOfLine,
                                             mode=QTextCursor.KeepAnchor)
                             tc.removeSelectedText()
                             self.replEdit.setTextCursor(tc)
+                        elif match.group("count") == "1":
+                            # delete to beinning of line
+                            tc.movePosition(QTextCursor.StartOfLine,
+                                            mode=QTextCursor.KeepAnchor)
+                            tc.removeSelectedText()
+                            self.replEdit.setTextCursor(tc)
+                        elif match.group("count") == "2":
+                            # delete whole line
+                            tc.movePosition(QTextCursor.EndOfLine)
+                            tc.movePosition(QTextCursor.StartOfLine,
+                                            mode=QTextCursor.KeepAnchor)
+                            tc.removeSelectedText()
+                            self.replEdit.setTextCursor(tc)
                     elif action == "m":
                         self.__setCharFormat(match.group(0)[:-1].split(";"),
                                              tc)
-            elif data[index] == 10:     # \n
-                tc.movePosition(QTextCursor.End)
-                self.replEdit.setTextCursor(tc)
-                self.replEdit.insertPlainText(chr(data[index]))
-                self.__setCharFormat(["0"], tc)     # reset format after a \n
+##            elif data[index] == 10:     # \n
+##                tc.movePosition(QTextCursor.End)
+##                self.replEdit.setTextCursor(tc)
+##                self.replEdit.insertPlainText(chr(data[index]))
+##                self.__setCharFormat(["0"], tc)     # reset format after a \n
             else:
                 tc.deleteChar()
                 self.replEdit.setTextCursor(tc)
@@ -504,6 +608,7 @@
         <li>35: foreground Dark Magenta</li>
         <li>36: foreground Dark Cyan</li>
         <li>37: foreground Light Gray</li>
+        <li>39: reset foreground to default</li>
         <li>40: background Black</li>
         <li>41: background Dark Red</li>
         <li>42: background Dark Green</li>
@@ -512,6 +617,7 @@
         <li>45: background Dark Magenta</li>
         <li>46: background Dark Cyan</li>
         <li>47: background Light Gray</li>
+        <li>49: reset background to default</li>
         <li>53: Overlined font</li>
         <li>55: Overline off</li>
         <li>90: bright foreground Dark Gray</li>
@@ -580,13 +686,21 @@
             elif formatCode == 55:
                 charFormat.setFontOverline(False)
             elif formatCode in (30, 31, 32, 33, 34, 35, 36, 37):
-                charFormat.setForeground(self.AnsiColors[formatCode - 30])
+                charFormat.setForeground(
+                    AnsiColorSchemes[self.__colorScheme][formatCode - 30])
             elif formatCode in (40, 41, 42, 43, 44, 45, 46, 47):
-                charFormat.setBackground(self.AnsiColors[formatCode - 40])
+                charFormat.setBackground(
+                    AnsiColorSchemes[self.__colorScheme][formatCode - 40])
             elif formatCode in (90, 91, 92, 93, 94, 95, 96, 97):
-                charFormat.setForeground(self.AnsiColors[formatCode - 80])
+                charFormat.setForeground(
+                    AnsiColorSchemes[self.__colorScheme][formatCode - 80])
             elif formatCode in (100, 101, 102, 103, 104, 105, 106, 107):
-                charFormat.setBackground(self.AnsiColors[formatCode - 90])
+                charFormat.setBackground(
+                    AnsiColorSchemes[self.__colorScheme][formatCode - 90])
+            elif formatCode == 39:
+                charFormat.setForeground(self.DefaultForeground)
+            elif formatCode == 49:
+                charFormat.setBackground(self.DefaultBackground)
         
         textCursor.setCharFormat(charFormat)
     

eric ide

mercurial