Initial version of python interactive console #2
authorOleg Shpynov <oleg.shpynov@jetbrains.com>
Thu, 4 Mar 2010 10:04:31 +0000 (13:04 +0300)
committerOleg Shpynov <oleg.shpynov@jetbrains.com>
Thu, 4 Mar 2010 10:04:31 +0000 (13:04 +0300)
platform/platform-impl/src/com/intellij/execution/process/ColoredOutputTypeRegistry.java [new file with mode: 0644]
platform/platform-impl/src/com/intellij/execution/process/ConsoleHighlighter.java [new file with mode: 0644]
platform/platform-impl/src/com/intellij/execution/process/ConsoleHistoryModel.java [new file with mode: 0644]

diff --git a/platform/platform-impl/src/com/intellij/execution/process/ColoredOutputTypeRegistry.java b/platform/platform-impl/src/com/intellij/execution/process/ColoredOutputTypeRegistry.java
new file mode 100644 (file)
index 0000000..222eec9
--- /dev/null
@@ -0,0 +1,119 @@
+package com.intellij.execution.process;
+
+import com.intellij.execution.ui.ConsoleViewContentType;
+import com.intellij.openapi.components.ServiceManager;
+import com.intellij.openapi.editor.colors.EditorColorsManager;
+import com.intellij.openapi.editor.colors.TextAttributesKey;
+import com.intellij.openapi.editor.markup.EffectType;
+import com.intellij.openapi.editor.markup.TextAttributes;
+import com.intellij.openapi.util.Key;
+import org.jetbrains.annotations.NonNls;
+
+import java.awt.*;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author yole
+ */
+public class ColoredOutputTypeRegistry {
+  public static ColoredOutputTypeRegistry getInstance() {
+    return ServiceManager.getService(ColoredOutputTypeRegistry.class);
+  }
+
+  private final Map<String, Key> myRegisteredKeys = new HashMap<String, Key>();
+
+  private final TextAttributesKey[] myAnsiColorKeys = new TextAttributesKey[] {
+    ConsoleHighlighter.OUT, ConsoleHighlighter.RED, ConsoleHighlighter.GREEN, ConsoleHighlighter.YELLOW, ConsoleHighlighter.BLUE,
+    ConsoleHighlighter.MAGENTA, ConsoleHighlighter.CYAN
+  };
+
+  /*
+    Description
+     0 Cancel all attributes except foreground/background color
+     1 Bright (bold)
+     2 Normal (not bold)
+     4 Underline
+     5 Blink
+     7 Reverse video
+     8 Concealed (don't display characters)
+     30        Make foreground (the characters) black
+     31        Make foreground red
+     32        Make foreground green
+     33        Make foreground yellow
+     34        Make foreground blue
+     35        Make foreground magenta
+     36        Make foreground cyan
+     37        Make foreground white
+
+     40        Make background (around the characters) black
+     41        Make background red
+     42        Make background green
+     43        Make background yellow
+     44        Make background blue
+     45        Make background magenta
+     46        Make background cyan
+     47        Make background white (you may need 0 instead, or in addition)
+
+     see full doc at http://www.linux-mag.com/downloads/2003-09/power/escape_sequences.html
+  */
+
+  public Key getOutputKey(@NonNls String attribute) {
+    final Key key = myRegisteredKeys.get(attribute);
+    if (key != null) {
+      return key;
+    }
+    final String completeAttribute = attribute;
+    if (attribute.startsWith("\u001B[")) {
+      attribute = attribute.substring(2);
+    }
+    else if (attribute.startsWith("[")) {
+      attribute = attribute.substring(1);
+    }
+    if (attribute.endsWith("m")) {
+      attribute = attribute.substring(0, attribute.length()-1);
+    }
+    if (attribute.equals("0")) {
+      return ProcessOutputTypes.STDOUT;
+    }
+    TextAttributes attrs = new TextAttributes();
+    final String[] strings = attribute.split(";");
+    for (String string : strings) {
+      int value;
+      try {
+        value = Integer.parseInt(string);
+      }
+      catch (NumberFormatException e) {
+        continue;
+      }
+      if (value == 4) {
+        attrs.setEffectType(EffectType.LINE_UNDERSCORE);
+      }
+      else if (value == 1) {
+        attrs.setFontType(Font.BOLD);
+      }
+      else if (value >= 30 && value <= 36) {
+        attrs.setForegroundColor(EditorColorsManager.getInstance().getGlobalScheme().getAttributes(myAnsiColorKeys [value-30]).getForegroundColor());
+      }
+      else if (value == 37) {
+        attrs.setForegroundColor(Color.WHITE);
+      }
+      else if (value == 90) {
+        // black, high intensity
+        attrs.setForegroundColor(EditorColorsManager.getInstance().getGlobalScheme().getAttributes(ConsoleHighlighter.GRAY).getForegroundColor());
+      }
+      else if (value >= 91 && value < 96) {
+        // TODO separate colors for high intensity?
+        attrs.setForegroundColor(EditorColorsManager.getInstance().getGlobalScheme().getAttributes(myAnsiColorKeys [value-90]).getForegroundColor());
+      }
+    }
+    if (attrs.getEffectType() == EffectType.LINE_UNDERSCORE) {
+      attrs.setEffectColor(attrs.getForegroundColor());
+    }
+    Key newKey = new Key(completeAttribute);
+    ConsoleViewContentType contentType = new ConsoleViewContentType(completeAttribute, attrs);
+    ConsoleViewContentType.registerNewConsoleViewType(newKey, contentType);
+    myRegisteredKeys.put(completeAttribute, newKey);
+    return newKey;
+  }
+}
diff --git a/platform/platform-impl/src/com/intellij/execution/process/ConsoleHighlighter.java b/platform/platform-impl/src/com/intellij/execution/process/ConsoleHighlighter.java
new file mode 100644 (file)
index 0000000..5b23d7c
--- /dev/null
@@ -0,0 +1,54 @@
+package com.intellij.execution.process;
+
+import com.intellij.openapi.editor.HighlighterColors;
+import com.intellij.openapi.editor.colors.TextAttributesKey;
+import com.intellij.openapi.editor.markup.TextAttributes;
+import org.jetbrains.annotations.NonNls;
+
+import java.awt.*;
+
+/**
+ * @author oleg
+ */
+public class ConsoleHighlighter {
+  @NonNls static final String OUT_ID = "CONSOLE_OUTPUT";
+  @NonNls static final String RED_ID = "CONSOLE_RED_OUTPUT";
+  @NonNls static final String GREEN_ID = "CONSOLE_GREEN_OUTPUT";
+  @NonNls static final String YELLOW_ID = "CONSOLE_YELLOW_OUTPUT";
+  @NonNls static final String BLUE_ID = "CONSOLE_BLUE_OUTPUT";
+  @NonNls static final String MAGENTA_ID = "CONSOLE_MAGENTA_OUTPUT";
+  @NonNls static final String CYAN_ID = "CONSOLE_CYAN_OUTPUT";
+  @NonNls static final String GRAY_ID = "CONSOLE_GRAY_OUTPUT";
+
+  public static final TextAttributes OUT_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+  public static final TextAttributes RED_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+  public static final TextAttributes GREEN_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+  public static final TextAttributes YELLOW_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+  public static final TextAttributes BLUE_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+  public static final TextAttributes MAGENTA_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+  public static final TextAttributes CYAN_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+  public static final TextAttributes GRAY_DEFAULT_ATTRS = HighlighterColors.TEXT.getDefaultAttributes().clone();
+
+  static {
+    OUT_DEFAULT_ATTRS.setBackgroundColor(new Color(230, 230, 230));
+    RED_DEFAULT_ATTRS.setForegroundColor(Color.RED);
+    GREEN_DEFAULT_ATTRS.setForegroundColor(new Color(0, 128, 0));
+    YELLOW_DEFAULT_ATTRS.setForegroundColor(new Color(255, 204, 0));
+    BLUE_DEFAULT_ATTRS.setForegroundColor(Color.BLUE);
+    MAGENTA_DEFAULT_ATTRS.setForegroundColor(Color.MAGENTA);
+    CYAN_DEFAULT_ATTRS.setForegroundColor(Color.CYAN.darker());
+    GRAY_DEFAULT_ATTRS.setForegroundColor(Color.GRAY.darker());
+  }
+
+  public static final TextAttributesKey OUT = TextAttributesKey.createTextAttributesKey(OUT_ID, OUT_DEFAULT_ATTRS);
+  public static final TextAttributesKey RED = TextAttributesKey.createTextAttributesKey(RED_ID, RED_DEFAULT_ATTRS);
+  public static final TextAttributesKey GREEN = TextAttributesKey.createTextAttributesKey(GREEN_ID, GREEN_DEFAULT_ATTRS);
+  public static final TextAttributesKey YELLOW = TextAttributesKey.createTextAttributesKey(YELLOW_ID, YELLOW_DEFAULT_ATTRS);
+  public static final TextAttributesKey BLUE = TextAttributesKey.createTextAttributesKey(BLUE_ID, BLUE_DEFAULT_ATTRS);
+  public static final TextAttributesKey MAGENTA = TextAttributesKey.createTextAttributesKey(MAGENTA_ID, MAGENTA_DEFAULT_ATTRS);
+  public static final TextAttributesKey CYAN = TextAttributesKey.createTextAttributesKey(CYAN_ID, CYAN_DEFAULT_ATTRS);
+  public static final TextAttributesKey GRAY = TextAttributesKey.createTextAttributesKey(GRAY_ID, GRAY_DEFAULT_ATTRS);
+
+  private ConsoleHighlighter() {
+  }
+}
\ No newline at end of file
diff --git a/platform/platform-impl/src/com/intellij/execution/process/ConsoleHistoryModel.java b/platform/platform-impl/src/com/intellij/execution/process/ConsoleHistoryModel.java
new file mode 100644 (file)
index 0000000..440e43e
--- /dev/null
@@ -0,0 +1,102 @@
+package com.intellij.execution.process;
+
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.EmptyAction;
+import com.intellij.util.PairProcessor;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @author Gregory.Shrago
+ */
+public class ConsoleHistoryModel {
+
+  public static final int DEFAULT_MAX_SIZE = 20;
+
+  private int myHistoryCursor;
+  private int myMaxHistorySize = DEFAULT_MAX_SIZE;
+  private final LinkedList<String> myHistory = new LinkedList<String>();
+
+
+  public void addToHistory(final String statement) {
+    synchronized (myHistory) {
+      myHistoryCursor = -1;
+      myHistory.remove(statement);
+      if (myHistory.size() >= myMaxHistorySize) {
+        myHistory.removeLast();
+      }
+      myHistory.addFirst(statement);
+    }
+  }
+
+  public List<String> getHistory() {
+    synchronized (myHistory) {
+      return new ArrayList<String>(myHistory);
+    }
+  }
+
+  public int getMaxHistorySize() {
+    synchronized (myHistory) {
+      return myMaxHistorySize;
+    }
+  }
+
+  public void setMaxHistorySize(final int maxHistorySize) {
+    synchronized (myHistory) {
+      myMaxHistorySize = maxHistorySize;
+    }
+  }
+
+  @Nullable
+  public String getHistoryNext() {
+    synchronized (myHistory) {
+      if (myHistoryCursor < myHistory.size()-1) {
+        return myHistory.get(++myHistoryCursor);
+      }
+      else {
+        if (myHistoryCursor == myHistory.size() - 1) myHistoryCursor++;
+        return null;
+      }
+    }
+  }
+
+  @Nullable
+  public String getHistoryPrev() {
+    synchronized (myHistory) {
+      if (myHistoryCursor > 0) {
+        return myHistory.get(--myHistoryCursor);
+      }
+      else {
+        if (myHistoryCursor == 0) myHistoryCursor--;
+        return null;
+      }
+    }
+  }
+
+  public boolean hasHistory(final boolean next) {
+    synchronized (myHistory) {
+      return next? myHistoryCursor <= myHistory.size() - 1 : myHistoryCursor >= 0;
+    }
+  }
+
+  public static AnAction createHistoryAction(final ConsoleHistoryModel model, final boolean next, final PairProcessor<AnActionEvent,String> processor) {
+    final AnAction action = new AnAction(null, null, null) {
+      @Override
+      public void actionPerformed(final AnActionEvent e) {
+        processor.process(e, next ? model.getHistoryNext() : model.getHistoryPrev());
+      }
+
+      @Override
+      public void update(final AnActionEvent e) {
+        e.getPresentation().setEnabled(model.hasHistory(next));
+      }
+    };
+    EmptyAction.setupAction(action, next? "Console.History.Next" : "Console.History.Previous", null);
+    return action;
+  }
+
+}