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 * Utility class for escape sequences.
025 */
026@InterfaceAudience.Private
027public final class EscapeSequences {
028
029  private EscapeSequences() {
030  }
031
032  public static String clearAll() {
033    return "\033[0;37;40m\033[2J";
034  }
035
036  public static String setTitle(String title) {
037    return "\033]2;" + title + "\007";
038  }
039
040  public static String cursor(boolean on) {
041    if (on) {
042      return "\033[?25h";
043    }
044    return "\033[?25l";
045  }
046
047  public static String moveCursor(int column, int row) {
048    return String.format("\033[%d;%dH", row + 1, column + 1);
049  }
050
051  public static String clearRemainingLine() {
052    return "\033[0;37;40m\033[K";
053  }
054
055  public static String color(Color foregroundColor, Color backgroundColor, boolean bold,
056    boolean reverse, boolean blink, boolean underline) {
057
058    int foregroundColorValue = getColorValue(foregroundColor, true);
059    int backgroundColorValue = getColorValue(backgroundColor, false);
060
061    StringBuilder sb = new StringBuilder();
062    if (bold && reverse && blink && !underline) {
063      sb.append("\033[0;1;7;5;");
064    } else if (bold && reverse && !blink && !underline) {
065      sb.append("\033[0;1;7;");
066    } else if (!bold && reverse && blink && !underline) {
067      sb.append("\033[0;7;5;");
068    } else if (bold && !reverse && blink && !underline) {
069      sb.append("\033[0;1;5;");
070    } else if (bold && !reverse && !blink && !underline) {
071      sb.append("\033[0;1;");
072    } else if (!bold && reverse && !blink && !underline) {
073      sb.append("\033[0;7;");
074    } else if (!bold && !reverse && blink && !underline) {
075      sb.append("\033[0;5;");
076    } else if (bold && reverse && blink) {
077      sb.append("\033[0;1;7;5;4;");
078    } else if (bold && reverse) {
079      sb.append("\033[0;1;7;4;");
080    } else if (!bold && reverse && blink) {
081      sb.append("\033[0;7;5;4;");
082    } else if (bold && blink) {
083      sb.append("\033[0;1;5;4;");
084    } else if (bold) {
085      sb.append("\033[0;1;4;");
086    } else if (reverse) {
087      sb.append("\033[0;7;4;");
088    } else if (blink) {
089      sb.append("\033[0;5;4;");
090    } else if (underline) {
091      sb.append("\033[0;4;");
092    } else {
093      sb.append("\033[0;");
094    }
095    sb.append(String.format("%d;%dm", foregroundColorValue, backgroundColorValue));
096    return sb.toString();
097  }
098
099  private static int getColorValue(Color color, boolean foreground) {
100    int baseValue;
101    if (foreground) {
102      baseValue = 30;
103    } else { // background
104      baseValue = 40;
105    }
106
107    switch (color) {
108      case BLACK:
109        return baseValue;
110
111      case RED:
112        return baseValue + 1;
113
114      case GREEN:
115        return baseValue + 2;
116
117      case YELLOW:
118        return baseValue + 3;
119
120      case BLUE:
121        return baseValue + 4;
122
123      case MAGENTA:
124        return baseValue + 5;
125
126      case CYAN:
127        return baseValue + 6;
128
129      case WHITE:
130        return baseValue + 7;
131
132      default:
133        throw new AssertionError();
134    }
135  }
136
137  public static String normal() {
138    return "\033[0;37;40m";
139  }
140}