001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop.terminal.impl;
019
020import org.apache.hadoop.hbase.hbtop.terminal.Color;
021import org.apache.yetus.audience.InterfaceAudience;
022
023
024/**
025 * Utility class for escape sequences.
026 */
027@InterfaceAudience.Private
028public final class EscapeSequences {
029
030  private EscapeSequences() {
031  }
032
033  public static String clearAll() {
034    return "\033[0;37;40m\033[2J";
035  }
036
037  public static String setTitle(String title) {
038    return "\033]2;" + title + "\007";
039  }
040
041  public static String cursor(boolean on) {
042    if (on) {
043      return "\033[?25h";
044    }
045    return "\033[?25l";
046  }
047
048  public static String moveCursor(int column, int row) {
049    return String.format("\033[%d;%dH", row + 1, column + 1);
050  }
051
052  public static String clearRemainingLine() {
053    return "\033[0;37;40m\033[K";
054  }
055
056  public static String color(Color foregroundColor, Color backgroundColor, boolean bold,
057    boolean reverse, boolean blink, boolean underline) {
058
059    int foregroundColorValue = getColorValue(foregroundColor, true);
060    int backgroundColorValue = getColorValue(backgroundColor, false);
061
062    StringBuilder sb = new StringBuilder();
063    if (bold && reverse && blink && !underline) {
064      sb.append("\033[0;1;7;5;");
065    } else if (bold && reverse && !blink && !underline) {
066      sb.append("\033[0;1;7;");
067    } else if (!bold && reverse && blink && !underline) {
068      sb.append("\033[0;7;5;");
069    } else if (bold && !reverse && blink && !underline) {
070      sb.append("\033[0;1;5;");
071    } else if (bold && !reverse && !blink && !underline) {
072      sb.append("\033[0;1;");
073    } else if (!bold && reverse && !blink && !underline) {
074      sb.append("\033[0;7;");
075    } else if (!bold && !reverse && blink && !underline) {
076      sb.append("\033[0;5;");
077    } else if (bold && reverse && blink) {
078      sb.append("\033[0;1;7;5;4;");
079    } else if (bold && reverse) {
080      sb.append("\033[0;1;7;4;");
081    } else if (!bold && reverse && blink) {
082      sb.append("\033[0;7;5;4;");
083    } else if (bold && blink) {
084      sb.append("\033[0;1;5;4;");
085    } else if (bold) {
086      sb.append("\033[0;1;4;");
087    } else if (reverse) {
088      sb.append("\033[0;7;4;");
089    } else if (blink) {
090      sb.append("\033[0;5;4;");
091    } else if (underline) {
092      sb.append("\033[0;4;");
093    } else {
094      sb.append("\033[0;");
095    }
096    sb.append(String.format("%d;%dm", foregroundColorValue, backgroundColorValue));
097    return sb.toString();
098  }
099
100  private static int getColorValue(Color color, boolean foreground) {
101    int baseValue;
102    if (foreground) {
103      baseValue = 30;
104    } else { // background
105      baseValue = 40;
106    }
107
108    switch (color) {
109      case BLACK:
110        return baseValue;
111
112      case RED:
113        return baseValue + 1;
114
115      case GREEN:
116        return baseValue + 2;
117
118      case YELLOW:
119        return baseValue + 3;
120
121      case BLUE:
122        return baseValue + 4;
123
124      case MAGENTA:
125        return baseValue + 5;
126
127      case CYAN:
128        return baseValue + 6;
129
130      case WHITE:
131        return baseValue + 7;
132
133      default:
134        throw new AssertionError();
135    }
136  }
137
138  public static String normal() {
139    return "\033[0;37;40m";
140  }
141}