mbien commented on code in PR #9741:
URL: https://github.com/apache/netbeans/pull/9741#discussion_r4345279713


##########
ide/lib.terminalemulator/src/org/netbeans/lib/terminalemulator/Attr.java:
##########
@@ -70,536 +57,596 @@ enum Attr {
 
     public static final int PAL_SIZE = 259;
 
-    private final int width;   // ... of field
-    private final int fmask;   // ... corresponding to width
+    private final Color fgColor;
 
-    private int offset;                // ... of field from the "left"
-    private int wmask;         // ... over the word. 1 where bits are set.
+    private final Color bgColor;
 
-    static {
-       // System.out.printf("Attr.static()\n");
-       for (Attr a : values())
-           a.init();
-       ALT = Attr.BGCOLOR.wmask | Attr.FGCOLOR.wmask | Attr.REVERSE.wmask | 
Attr.ACTIVE.wmask;
-    }
+    private final int attributes;
 
-    /*
-     * Explicitly set the offset.
-     */
-    private Attr(int offset, int width) {
-       // System.out.printf("Attr(%d, %d) -> %d\n", offset, width, ordinal());
-       this.offset = offset;
-       this.width = width;
-       this.fmask = (1<<width)-1;
+    public Attr() {
+        this(0, null, null);
     }
 
-    /*
-     * Automatically set the offset.
-     */
-    private Attr(int width) {
-       // System.out.printf("Attr(%d) -> %d\n", width, ordinal());
-       this.offset = -1;
-       this.width = width;
-       this.fmask = (1<<width)-1;
+    public Attr(int attributes, Color fgColor, Color bgColor) {
+        this.attributes = attributes;
+        this.fgColor = fgColor;
+        this.bgColor = bgColor;
     }
 
-    private void init() {
-       if (this.offset == -1) {
-           if (ordinal() == 0) {
-               this.offset = 0;
-           } else {
-               Attr prev = Attr.values()[ordinal()-1];
-               offset = prev.offset + prev.width;
-           }
-       }
-       wmask = fmask << offset;
-       // System.out.printf("%s\n", this);
+    public boolean isEmpty() {
+        return attributes == 0 && fgColor == null && bgColor == null;
     }
 
-    @Override
-    public String toString() {
-       return String.format("%d %10s(%2d, %2d, 0x%02x %8s, 0x%08x %32s)", // 
NOI18N
-                            ordinal(), name(),
-                            offset, width,
-                            fmask, Integer.toBinaryString(fmask),
-                            wmask, Integer.toBinaryString(wmask) );
+    public boolean isHidden() {
+        return AttrEncoding.HIDDEN.isSet(this.attributes);
     }
 
-    public static String toString(int attr) {
-       return String.format("%32s", Integer.toBinaryString(attr)).replace(" ", 
"0"); // NOI18N
-
+    public boolean isReverse() {
+        return AttrEncoding.REVERSE.isSet(this.attributes);
     }
 
-    public final int get(int attr) {
-       return (attr >> offset) & fmask;
+    public boolean isBlink() {
+        return AttrEncoding.BLINK.isSet(this.attributes);
     }
 
-    /*
-     * Use for setting a wide field.
-     * Works for 1-bit field but set(int) is more efficient.
-     */
-    public final int set(int attr, int value) {
-       // value &= fmask;      // throw all but lowest relevant bits away
-       // attr &= ~ wmask;     // 0 out existing bits
-       // attr |= value << offset;// set new value
+    public boolean isUnderscore() {
+        return AttrEncoding.UNDERSCORE.isSet(this.attributes);
+    }
 
-       return (attr & ~wmask) | ((value & fmask) << offset);
+    public boolean isBright() {
+        return AttrEncoding.BRIGHT.isSet(this.attributes);
     }
 
-    /*
-     * Use for setting a 1 bit field
-     */
-    public final int set(int attr) {
-       assert width == 1;
-       return attr | (1 << offset);
+    public boolean isDim() {
+        return AttrEncoding.DIM.isSet(this.attributes);
     }
 
-    /*
-     * Use for clearing any width field.
-     */
-    public final int clear(int attr) {
-       return attr & ~wmask;
+    public boolean isActive() {
+        return AttrEncoding.ACTIVE.isSet(this.attributes);
     }
 
 
-    public final boolean isSet(int attr) {
-       return (attr & wmask) != 0;
+    public boolean isBGcolor() {
+        return bgColor != null || AttrEncoding.BGCOLOR.isSet(this.attributes);
+    }
+
+    public boolean isFGcolor() {
+        return fgColor != null || AttrEncoding.FGCOLOR.isSet(this.attributes);
     }
 
     /**
      * Use this to get at the FG palette index embedded in an attr.
      */
-    public static int foregroundColor(int attr) {
-       final int cx = FGCOLOR.get(attr);
-       if (cx == 0)
-           return PAL_FG;
-       else
-           return cx-1;
-    } 
+    public Color getFGcolor(Color[] palette) {
+        if (fgColor != null) {
+            return fgColor;
+        }
+        final int cx = AttrEncoding.FGCOLOR.get(this.attributes);
+        if (cx == 0) {
+            return palette[PAL_FG];
+        } else {
+            return palette[cx - 1];
+        }
+    }
 
     /**
      * Use this to get at the BG palette index embedded in an attr.
      */
-    public static int backgroundColor(int attr) {
-       final int cx = BGCOLOR.get(attr);
-       if (cx == 0)
-           return PAL_BG;
-       else
-           return cx-1;
+    public Color getBGColor(Color[] palette) {
+        if (bgColor != null) {
+            return bgColor;
+        }
+        final int cx = AttrEncoding.BGCOLOR.get(this.attributes);
+        if (cx == 0) {
+            return palette[PAL_BG];
+        } else {
+            return palette[cx - 1];
+        }
     }
 
-    /*
-     * Read-modify-write utility for setting bitfields in 'attr'.
-     */
-    @SuppressWarnings("AssignmentToMethodParameter")
-    public static int setAttribute(int attr, int value) {
-       switch (value) {
-           case 0:
-               // Reset all attributes
-               attr = 0;
-               break;
-           case 5:             // slow blink
-           case 6:             // fast blink
-               // Attr.BLINK
-               // FALLTHRU
-           case 1:
-               attr = DIM.clear(attr);
-               attr = BRIGHT.set(attr);
-               break;
-           case 2:
-                // Faint - not implemented
-               attr = BRIGHT.clear(attr);
-               attr = DIM.set(attr);
-               break;
-            case 3:
-                // Italic - not supported
-                break;
-           case 4:
-               attr = UNDERSCORE.set(attr);
-               break;
-           case 7:
-               attr = REVERSE.set(attr);
-               break;
-           case 8:
-               attr = HIDDEN.set(attr);
-               break;
-
-           case 9:
-               // Term specific
-               attr = ACTIVE.set(attr);
-               break;
-
-           // turn individual attributes off (dtterm specific?)
-           case 25:
-               // blinking off
-               // FALLTHRU
-           case 22:
-               attr = DIM.clear(attr);
-               attr = BRIGHT.clear(attr);
-               break;
-           case 24:
-               attr = UNDERSCORE.clear(attr);
-               break;
-           case 27:
-               attr = REVERSE.clear(attr);
-               break;
-           case 28:
-               attr = HIDDEN.clear(attr);
-               break;
-
-           // default color
-           case 39:
-               attr = FGCOLOR.clear(attr);
-               break;
-           case 49:
-               attr = BGCOLOR.clear(attr);
-               break;
-
-
-           // ANSI fg
-           case 30:
-           case 31:
-           case 32:
-           case 33:
-           case 34:
-           case 35:
-           case 36:
-           case 37:
-
-           // ANSI bg
-           case 40:
-           case 41:
-           case 42:
-           case 43:
-           case 44:
-           case 45:
-           case 46:
-           case 47:
-
-           // custom fg
-            case 50:
-           case 51:
-           case 52:
-           case 53:
-           case 54:
-           case 55:
-           case 56:
-           case 57:
-
-           // custom bg (was: 60-67)
-           case 58:
-           case 59:
-           case 60:
-           case 61:
-           case 62:
-           case 63:
-           case 64:
-           case 65:
-
-           // bright fg
-           case 90:
-           case 91:
-           case 92:
-           case 93:
-           case 94:
-           case 95:
-           case 96:
-           case 97:
-
-           // bright bg
-           case 100:
-           case 101:
-           case 102:
-           case 103:
-           case 104:
-           case 105:
-           case 106:
-           case 107:
-               try {
-                   Info i = map[value];
-                   if (i != null)
-                       attr = i.field.set(attr, i.pindex+1);
-               } catch (ArrayStoreException x) {
-                   // silently ignore unrecognized attribute
-                   break;
-               }
-               break;
-
-           default:
-               // silently ignore unrecognized attribute
-               break;
-       } 
-       return attr;
+    public Attr setAttribute(List<Integer> parameterList) {
+        int attributesUpdated = this.attributes;
+        Color fgColorUpdated = this.fgColor;
+        Color bgColorUpdated = this.bgColor;
+
+        // Loop over the parameter list as multiple operations can be combined
+        // into a single call
+        List<Integer> workList = new ArrayList<>(parameterList);
+        if (workList.isEmpty()) {
+            workList.add(0); // No parameter implies reset
+        }
+        while (!workList.isEmpty()) {
+            Integer value = workList.removeFirst();
+            switch (value) {
+                case 0 -> {
+                    // Reset all attributes
+                    attributesUpdated = 0;
+                    fgColorUpdated = null;
+                    bgColorUpdated = null;
+                }
+                case 1, 5, 6 -> { // bold/bright // slow blink // fast blink 
(blink is not supported)
+                    attributesUpdated = 
AttrEncoding.DIM.clear(attributesUpdated);
+                    attributesUpdated = 
AttrEncoding.BRIGHT.set(attributesUpdated);
+                }
+                case 2 -> {
+                    // Faint - not implemented
+                    attributesUpdated = 
AttrEncoding.BRIGHT.clear(attributesUpdated);
+                    attributesUpdated = 
AttrEncoding.DIM.set(attributesUpdated);
+                }
+                case 3 -> {
+                    // Italic - not supported
+                }
+                case 4 -> {
+                    attributesUpdated = 
AttrEncoding.UNDERSCORE.set(attributesUpdated);
+                }
+                case 7 -> {
+                    attributesUpdated = 
AttrEncoding.REVERSE.set(attributesUpdated);
+                }
+                case 8 -> {
+                    attributesUpdated = 
AttrEncoding.HIDDEN.set(attributesUpdated);
+                }
+                case 9 -> {
+                    // Term specific
+                    attributesUpdated = 
AttrEncoding.ACTIVE.set(attributesUpdated);
+                }
+                case 22, 25 -> { // reset bold/dim // reset blink (not 
supported)
+                    attributesUpdated = 
AttrEncoding.DIM.clear(attributesUpdated);
+                    attributesUpdated = 
AttrEncoding.BRIGHT.clear(attributesUpdated);
+                }
+                case 24 -> {
+                    attributesUpdated = 
AttrEncoding.UNDERSCORE.clear(attributesUpdated);
+                }
+                case 27 -> {
+                    attributesUpdated = 
AttrEncoding.REVERSE.clear(attributesUpdated);
+                }
+                case 28 -> {
+                    attributesUpdated = 
AttrEncoding.HIDDEN.clear(attributesUpdated);
+                }
+                case 39 -> { // default color
+                    attributesUpdated = 
AttrEncoding.FGCOLOR.clear(attributesUpdated);
+                    fgColorUpdated = null;
+                }
+                case 49 -> { // default color
+                    attributesUpdated = 
AttrEncoding.BGCOLOR.clear(attributesUpdated);
+                    bgColorUpdated = null;
+                }
+                case 30, 31, 32, 33, 34, 35, 36, 37, // ANSI fg
+                     40, 41, 42, 43, 44, 45, 46, 47, // ANSI bg
+                     50, 51, 52, 53, 54, 55, 56, 57, // custom fg
+                     58, 59, 60, 61, 62, 63, 64, 65, // custom bg (was: 60-67)
+                     90, 91, 92, 93, 94, 95, 96, 97, // bright fg
+                     100, 101, 102, 103, 104, 105, 106, 107 -> { // bright bg
+                    try {
+                        Info i = map[value];
+                        if (i != null) {
+                            attributesUpdated = i.field.set(attributesUpdated, 
i.pindex + 1);
+                            if (i.field == AttrEncoding.BGCOLOR) {
+                                fgColorUpdated = null;
+                            } else if (i.field == AttrEncoding.BGCOLOR) {
+                                bgColorUpdated = null;
+                            }

Review Comment:
   something wrong here: both if check BG. First was probably meant for FG.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to