001/* 002 * JDrupes Builder 003 * Copyright (C) 2026 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.builder.core.console; 020 021/// Provides ANSI escape sequences. 022/// 023@SuppressWarnings({ "PMD.ShortClassName" }) 024public final class Ansi { 025 private static final String PREFIX = "\u001B["; 026 027 private Ansi() { 028 // Make javadoc happy 029 } 030 031 /// Cursor to start of line. 032 /// 033 /// @return the string 034 /// 035 public static String cursorToSol() { 036 return PREFIX + "G"; 037 } 038 039 /// Cursor up. 040 /// 041 /// @param lines the lines 042 /// @return the string 043 /// 044 public static String cursorUp(int lines) { 045 return PREFIX + lines + "A"; 046 } 047 048 /// Cursor down. 049 /// 050 /// @param lines the lines 051 /// @return the string 052 /// 053 public static String cursorDown(int lines) { 054 return PREFIX + lines + "B"; 055 } 056 057// /// Sets the scroll range. 058// /// 059// /// @param first the first line 060// /// @param last the last line 061// /// @return the string 062// /// 063// public static String scrollRange(int first, int last) { 064// return PREFIX + (first + 1) + ";" + (last + 1) + "r"; 065// } 066// 067// /// Clears the scroll range. 068// /// 069// /// @return the string 070// /// 071// public static String scrollAll() { 072// return PREFIX + "r"; 073// } 074// 075// /// Scroll one line up. 076// /// 077// /// @return the string 078// /// 079// public static String scrollUp() { 080// return PREFIX + "1S"; 081// } 082 083 /// Clear the current line. 084 /// 085 /// @return the string 086 /// 087 public static String clearLine() { 088 return PREFIX + "2K"; 089 } 090 091 /// Hide cursor. 092 /// 093 /// @return the string 094 /// 095 public static String hideCursor() { 096 return PREFIX + "?25l"; 097 } 098 099 /// Show the cursor. 100 /// 101 /// @return the string 102 /// 103 public static String showCursor() { 104 return PREFIX + "?25h"; 105 } 106 107 /// Sets a color. 108 /// 109 /// @param color the color 110 /// @return the string 111 /// 112 public static String color(Color color) { 113 return PREFIX + color.code() + "m"; 114 } 115 116 /// Reset attributes. 117 /// 118 /// @return the string 119 /// 120 public static String resetAttributes() { 121 return PREFIX + "0m"; 122 } 123 124 /// Color arguments for [#color]. 125 /// 126 @SuppressWarnings("PMD.FieldNamingConventions") 127 public enum Color { 128 /// The color red. 129 Red(31), 130 /// The color green. 131 Green(32), 132 /// The color yellow. 133 Yellow(33), 134 /// The color blue. 135 Blue(34), 136 /// The color magenta. 137 Magenta(35), 138 /// The color cyan. 139 Cyan(36); 140 141 private final int code; 142 143 Color(int code) { 144 this.code = code; 145 } 146 147 /// Code. 148 /// 149 /// @return the int 150 /// 151 public int code() { 152 return code; 153 } 154 } 155 156}