summaryrefslogtreecommitdiff
path: root/tutorials/java/src/phpdbg/ui/JTerminalPane.java
blob: eb93891688e8816c363922a178bc06f9c4d35d63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package phpdbg.ui;

/**
 *
 * @author krakjoe
 */
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Color;

public class JTerminalPane extends JTextPane {
  
  private static final Color D_Black   = Color.getHSBColor( 0.000f, 0.000f, 0.000f );
  private static final Color D_Red     = Color.getHSBColor( 0.000f, 1.000f, 0.502f );
  private static final Color D_Blue    = Color.getHSBColor( 0.667f, 1.000f, 0.502f );
  private static final Color D_Magenta = Color.getHSBColor( 0.833f, 1.000f, 0.502f );
  private static final Color D_Green   = Color.getHSBColor( 0.333f, 1.000f, 0.502f );
  private static final Color D_Yellow  = Color.getHSBColor( 0.167f, 1.000f, 0.502f );
  private static final Color D_Cyan    = Color.getHSBColor( 0.500f, 1.000f, 0.502f );
  private static final Color D_White   = Color.getHSBColor( 0.000f, 0.000f, 0.753f );
  private static final Color B_Black   = Color.getHSBColor( 0.000f, 0.000f, 0.502f );
  private static final Color B_Red     = Color.getHSBColor( 0.000f, 1.000f, 1.000f );
  private static final Color B_Blue    = Color.getHSBColor( 0.667f, 1.000f, 1.000f );
  private static final Color B_Magenta = Color.getHSBColor( 0.833f, 1.000f, 1.000f );
  private static final Color B_Green   = Color.getHSBColor( 0.333f, 1.000f, 1.000f );
  private static final Color B_Yellow  = Color.getHSBColor( 0.167f, 1.000f, 1.000f );
  private static final Color B_Cyan    = Color.getHSBColor( 0.500f, 1.000f, 1.000f );
  private static final Color B_White   = Color.getHSBColor( 0.000f, 0.000f, 1.000f );
  private static final Color cReset    = Color.getHSBColor( 0.000f, 0.000f, 0.000f );
  
  private Color colorCurrent    = cReset;
  private String remaining = "";
  
  public JTerminalPane() {
      super();
  }
  
  public void append(Color c, String s) {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    AttributeSet aset = sc.addAttribute(
            SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
    setCaretPosition(getDocument().getLength());
    setCharacterAttributes(aset, false);
    replaceSelection(s);
  }

  public void appendANSI(String s) {
    int aPos = 0;
    int aIndex;
    int mIndex;
    String tmpString;
    boolean stillSearching;
    
    String addString = remaining + s;
    
    remaining = "";

    if (addString.length() > 0) {
      aIndex = addString.indexOf("\u001B");
      if (aIndex == -1) {
        append(colorCurrent,addString);
        return;
      }

      if (aIndex > 0) {
        tmpString = addString.substring(0,aIndex);
        append(colorCurrent, tmpString);
        aPos = aIndex;
      }

      stillSearching = true;
      while (stillSearching) {
        mIndex = addString.indexOf("m",aPos);
        if (mIndex < 0) {
          remaining = addString.substring(aPos,addString.length());
          stillSearching = false;
          continue;
        }
        else {
          tmpString = addString.substring(aPos,mIndex+1);
          colorCurrent = getANSIColor(tmpString);
        }
        aPos = mIndex + 1;

        aIndex = addString.indexOf("\u001B", aPos);

        if (aIndex == -1) {
          tmpString = addString.substring(aPos,addString.length());
          append(colorCurrent, tmpString);
          stillSearching = false;
          continue; 
        }
        tmpString = addString.substring(aPos,aIndex);
        aPos = aIndex;
        append(colorCurrent, tmpString);
      }
    }
  }

  public Color getANSIColor(String ANSIColor) {
      switch (ANSIColor) {
          case "\u001B[30m":
              return D_Black;
          case "\u001B[31m":
              return D_Red;
          case "\u001B[32m":
              return D_Green;
          case "\u001B[33m":
              return D_Yellow;
          case "\u001B[34m":
              return D_Blue;
          case "\u001B[35m":
              return D_Magenta;
          case "\u001B[36m":
              return D_Cyan;
          case "\u001B[37m":
              return D_White;
          case "\u001B[0;30m":
              return D_Black;
          case "\u001B[0;31m":
              return D_Red;
          case "\u001B[0;32m":
              return D_Green;
          case "\u001B[0;33m":
              return D_Yellow;
          case "\u001B[0;34m":
              return D_Blue;
          case "\u001B[0;35m":
              return D_Magenta;
          case "\u001B[0;36m":
              return D_Cyan;
          case "\u001B[0;37m":
              return D_White;
          case "\u001B[1;30m":
              return B_Black;
          case "\u001B[1;31m":
              return B_Red;
          case "\u001B[1;32m":
              return B_Green;
          case "\u001B[1;33m":
              return B_Yellow;
          case "\u001B[1;34m":
              return B_Blue;
          case "\u001B[1;35m":
              return B_Magenta;
          case "\u001B[1;36m":
              return B_Cyan;
          case "\u001B[1;37m":
              return B_White;
          case "\u001B[0m":
              return cReset;
          default:
              return B_White;
      }
  }
}