Coverage Summary for Class: Symbols (it.polimi.ingsw.Misc)

Class Method, % Branch, % Line, %
Symbols 80% (4/5) 100% (10/10) 95,2% (20/21)
Symbols$1 100% (1/1) 100% (1/1)
Total 83,3% (5/6) 100% (10/10) 95,5% (21/22)


1 package it.polimi.ingsw.Misc; 2  3 import it.polimi.ingsw.Model.Enums.PawnColour; 4  5 /** 6  * Symbols holds constants and methods used by the CLI. Mainly it focuses on colour management via ANSI codes 7  */ 8 public class Symbols { 9  public static final String BLACK = "\u001b[30m"; 10  public static final String WHITE = "\u001b[243m"; 11  public static final String GRAY = "\u001b[37m"; 12  public static final String BLUE = "\u001b[34m"; 13  public static final String GREEN = "\u001b[32m"; 14  public static final String PINK = "\u001b[35m"; 15  public static final String RED = "\u001b[31m"; 16  public static final String YELLOW = "\u001b[33m"; 17  18  // colour used to represent coins 19  public static final String GOLD = "\u001b[38;5;220m"; 20  21  // code used to reset terminal characters style 22  public static final String RESET = "\u001b[00m"; 23  24  // simple gray background used under all the CLI to improve visibility independently of the specific console setting 25  public static final String BACKGROUND = "\u001b[48;5;237m"; 26  27  // background used to highlight students in the entrance 28  public static final String BACKGROUND_BLUE = "\u001b[44m"; 29  public static final String BACKGROUND_GREEN = "\u001b[42m"; 30  public static final String BACKGROUND_PINK = "\u001b[45m"; 31  public static final String BACKGROUND_RED = "\u001b[41m"; 32  public static final String BACKGROUND_YELLOW = "\u001b[43m"; 33  34  // improves readability of certain icons (towers and students) 35  public static final String BOLD = "\u001b[1m"; 36  37  // used to handle no entry tiles 38  public static final String OVERLAY = "\u001b[7m"; 39  40  // icons to represent specific model entities 41  public static final String PAWN = BOLD + "■" + RESET + BACKGROUND; 42  public static final String TOWER = BOLD + "≡" + RESET + BACKGROUND; 43  public static final String COIN = GOLD + "¤" + RESET + BACKGROUND; 44  45  46  /** 47  * Method used to create a visual representation of the students using a colour and the icon. 48  * Extends the functionality of the method {@link Symbols#colour(String, String)} 49  * 50  * @param p student object used to access colour information 51  * @param message the icon or character with which the student will be represented 52  * @return a String containing the ASCII code of the colour followed by the message 53  */ 54  public static String colorizeStudent(PawnColour p, String message) { 55  String student = ""; 56  switch (p) { 57  case BLUE -> student = student + Symbols.colour(message, Symbols.BLUE); 58  case GREEN -> student = student + Symbols.colour(message, Symbols.GREEN); 59  case PINK -> student = student + Symbols.colour(message, Symbols.PINK); 60  case RED -> student = student + Symbols.colour(message, Symbols.RED); 61  case YELLOW -> student = student + Symbols.colour(message, Symbols.YELLOW); 62  } 63  return student; 64  } 65  66  /** 67  * Method used to change the style of a string. It is usually used to change the colour of the string but 68  * can be used to change other aspect of the style in general 69  * 70  * @param s the String of which we want to change the style 71  * @param colour the ANSI code with which the String style will be modified 72  * @return a String with style changed 73  */ 74  public static String colour(String s, String colour) { 75  return colour + s + Symbols.RESET + Symbols.BACKGROUND; 76  } 77  78  /** 79  * Method used to create a visual representation of the students using a colour as the background of a String. 80  * 81  * @param p student object used to access colour information 82  * @param message the icon or character with which the student will be represented 83  * @return a String containing the ASCII code of the colour followed by the message 84  */ 85  public static String colorizeBackgroundStudent(PawnColour p, String message) { 86  String student = ""; 87  switch (p) { 88  case BLUE -> 89  student = student + Symbols.BACKGROUND_BLUE + Symbols.BLACK + " " + message + " " + Symbols.RESET + Symbols.BACKGROUND; 90  case GREEN -> 91  student = student + Symbols.BACKGROUND_GREEN + Symbols.BLACK + " " + message + " " + Symbols.RESET + Symbols.BACKGROUND; 92  case PINK -> 93  student = student + Symbols.BACKGROUND_PINK + Symbols.BLACK + " " + message + " " + Symbols.RESET + Symbols.BACKGROUND; 94  case RED -> 95  student = student + Symbols.BACKGROUND_RED + Symbols.BLACK + " " + message + " " + Symbols.RESET + Symbols.BACKGROUND; 96  case YELLOW -> 97  student = student + Symbols.BACKGROUND_YELLOW + Symbols.BLACK + " " + message + " " + Symbols.RESET + Symbols.BACKGROUND; 98  99  } 100  return student; 101  } 102  103  /** 104  * Method used to remove all ANSI codes and tabulation from a String. Useful to count character within a String 105  * which has its style affected by ANSI codes 106  * 107  * @param s the message which the ANSI code will be stripped from 108  * @return a simple ASCII String 109  */ 110  public static String stripFromANSICodes(String s) { 111  return s 112  .replaceAll("(\\x9B|\\x1B\\[)[0-?]*[ -/]*[@-~]", "") // removes all ANSI control codes 113  .replaceAll("(\t)+", " "); // converts tabs to four spaces 114  } 115 }