This commit is contained in:
Anas Elgarhy 2022-06-15 20:20:37 +02:00
parent 68e4c79239
commit fca9e36b35
2 changed files with 119 additions and 41 deletions

View file

@ -6,7 +6,12 @@
### Features:
- Support text color.
- Support background color.
- Support 256 colors (RGB).
- Support ANSI colors.
- Support indexed colors.
- Support text style.
- Lightweight and fast.
- Easy to use.
### How to add this library into your project
**Step 1**. Add the dependency
@ -17,7 +22,6 @@
<version>0.1.5</version>
</dependency>
```
**Step 2**. run this command `mvn install`
#### Gradle:
**Step 1**. Add the JitPack repository to your build file<br>
@ -37,43 +41,30 @@ allprojects {
}
```
### Usage:
```java
ConsoleManager manager = new DefaultConsoleManager(); // Create a new console manager
manager.setTextColor(TextColor.LIGHT_RED); // Set text color
manager.setBackgroundColor(BackgroundColor.DARK_BLUE); // Set background color
manager.println("Hello World!"); // Print text
```
<img src="./Screenshots/1.png">
## Usage:
```java
ConsoleManager manager = new DefaultConsoleManager(); // Create a new console manager
manager.setTextColor(TextColor.LIGHT_RED); // Set text color
manager.setBackgroundColor(BackgroundColor.DARK_BLUE); // Set background color
manager.setTextStyle(TextStyle.ITALIC); // Set text style
## Requirements for development:
- Maven
- jdk 17
- IntelliJ IDEA (not required but recommended)
manager.println("Hello World!"); // Print text
```
<img src="./Screenshots/2.png">
## TODO
- [ ] Add Tests
- [ ] Add Formatter
- [ ] Add Documentation
- [ ] Add more examples
```java
public class Example1 {
public static void main(String[] args) {
ConsoleManager manager = new DefaultConsoleManager();
### Available in
manager.print("Hello", BackgroundColor.DARK_YELLOW, TextColor.DARK_WHITE, TextStyle.BOLD, TextStyle.ITALIC);
manager.print(", ", TextColor.LIGHT_GREEN);
manager.print("I'm ", TextColor.DARK_YELLOW);
manager.print("Anas", TextColor.LIGHT_BLUE, TextStyle.DOUBLE_UNDERLINE);
manager.println(" :D", TextColor.LIGHT_CYAN);
manager.println("\tFrom", TextColor.LIGHT_RED, TextStyle.ITALIC);
manager.print("Eg", BackgroundColor.LIGHT_RED);
manager.print("y", BackgroundColor.DARK_WHITE);
manager.print("pt", BackgroundColor.DARK_BLACK);
}
}
```
<img src="./Screenshots/3.png">
[![GitHub](https://img.shields.io/badge/GitHub-Main%20repo-brightgreen?style=for-the-badge&logo=GitHub)](https://github.com/Anas-Elgarhy/jpwd)
[![GitLab](https://img.shields.io/badge/GitLab-Mirror%20repo-brightgreen?style=for-the-badge&logo=GitLab)](https://gitlab.com/java-utils1/jpwd)
[![BitBucket](https://img.shields.io/badge/BitBucket-Mirror%20repo-brightgreen?style=for-the-badge&logo=BitBucket)](https://bitbucket.org/anas_elgarhy/jpwd)
[![Codeberg](https://img.shields.io/badge/Codeberg-Mirror%20repo-brightgreen?style=for-the-badge&logo=Codeberg)](https://codeberg.org/java-utils/jpwd)
#### License: MIT
[![Quality gate](https://sonarcloud.io/api/project_badges/quality_gate?project=Anas-Elgarhy_jpwd)](https://sonarcloud.io/summary/new_code?id=Anas-Elgarhy_jpwd)
[![SonarCloud](https://sonarcloud.io/images/project_badges/sonarcloud-black.svg)](https://sonarcloud.io/summary/new_code?id=Anas-Elgarhy_jpwd)
![License: GPL-3.0](https://img.shields.io/badge/License-GPL%203.0-blue.svg)

View file

@ -12,18 +12,39 @@ public class ColoredString {
private TextColor backgroundColor;
private ArrayList<TextStyle> styles;
/**
* Create a new empty ColoredString object
*/
public ColoredString() {
this(null);
}
/**
* Create a new ColoredString object with the given string
* @param str The string to be colored
* @param styles The styles to be applied to the string (optional)
*/
public ColoredString(String str, TextStyle... styles) {
this(str, (TextColor) null, styles);
}
/**
* Create a new ColoredString object with the given string and foreground color
* @param str The string to be colored
* @param foregroundColor The foreground color to be applied to the string
* @param styles The styles to be applied to the string (optional)
*/
public ColoredString(String str, TextColor foregroundColor, TextStyle... styles) {
this(str, foregroundColor, null, styles);
}
/**
* Create a new ColoredString object with the given string and foreground and background colors
* @param str The string to be colored
* @param foregroundColor The foreground color to be applied to the string
* @param backgroundColor The background color to be applied to the string
* @param styles The styles to be applied to the string (optional)
*/
public ColoredString(String str, TextColor foregroundColor, TextColor backgroundColor, TextStyle... styles) {
strBytes = str != null ?
str.getBytes(StandardCharsets.UTF_8) : null;
@ -36,10 +57,23 @@ public class ColoredString {
}
}
/**
* Create a new ColoredString object with the given string and foreground color as string of name of the color or hexadecimal color code
* @param str The string to be colored
* @param foregroundColor The foreground color to be applied to the string as string of name of the color or hexadecimal color code
* @param styles The styles to be applied to the string (optional)
*/
public ColoredString(String str, String foregroundColor, TextStyle... styles) {
this(str, foregroundColor, null, styles);
}
/**
* Create a new ColoredString object with the given string and foreground and background colors as string of name of the color or hexadecimal color code
* @param str The string to be colored
* @param foregroundColor The foreground color to be applied to the string as string of name of the color or hexadecimal color code
* @param backgroundColor The background color to be applied to the string as string of name of the color or hexadecimal color code
* @param styles The styles to be applied to the string (optional)
*/
public ColoredString(String str, String foregroundColor, String backgroundColor, TextStyle... styles) {
this(str,
TextColor.Factory.fromString(foregroundColor),
@ -47,35 +81,67 @@ public class ColoredString {
styles);
}
/**
* Set the foreground color (text color)
* @param textColor The foreground color to be applied to the string
*/
public void setForegroundColor(TextColor textColor) {
this.foregroundColor = textColor;
}
public void setForegroundColor(String color) {
this.setForegroundColor(TextColor.Factory.fromString(color));
/**
* Set the foreground color (text color) as string of name of the color or hexadecimal color code
* @param textColor The foreground color to be applied to the string as string of name of the color or hexadecimal color code
*/
public void setForegroundColor(String textColor) {
this.setForegroundColor(TextColor.Factory.fromString(textColor));
}
/**
* Set the background color
* @param backgroundColor The background color to be applied to the string
*/
public void setBackgroundColor(TextColor backgroundColor) {
this.backgroundColor = backgroundColor;
}
public void setBackgroundColor(String color) {
this.setBackgroundColor(TextColor.Factory.fromString(color));
/**
* Set the background color as string of name of the color or hexadecimal color code
* @param backgroundColor The background color to be applied to the string as string of name of the color or hexadecimal color code
*/
public void setBackgroundColor(String backgroundColor) {
this.setBackgroundColor(TextColor.Factory.fromString(backgroundColor));
}
/**'
* Get the foreground color (text color)
* @return The foreground color, null if not set
*/
public TextColor getForegroundColor() {
return foregroundColor;
}
/**
* Get the background color
* @return The background color, null if not set
*/
public TextColor getBackgroundColor() {
return backgroundColor;
}
/**
* Get the string as byte array
* @return The string as byte array
*/
public byte[] getBytes() {
return strBytes;
}
/**
* Get the string with the applied styles and colors as byte array
* @return The string with the applied styles and colors as byte array
*/
public byte[] getColoredBytes() {
if (strBytes == null) {
return null;
@ -83,10 +149,18 @@ public class ColoredString {
return toString().getBytes(StandardCharsets.UTF_8);
}
/**
* Set the string
* @param str The string to be colored
*/
public void setStr(String str) {
strBytes = str.getBytes(StandardCharsets.UTF_8);
}
/**
* Add style to the string
* @param style The style to be applied to the string
*/
public void addStyle(TextStyle style) {
if (styles == null) {
styles = new ArrayList<>();
@ -94,6 +168,10 @@ public class ColoredString {
styles.add(style);
}
/**
* Remove style from the string
* @param style The style to be removed from the string
*/
public void removeStyle(TextStyle style) {
if (styles == null) {
return;
@ -102,6 +180,10 @@ public class ColoredString {
}
/**
* Get the string with the applied styles and colors
* @return The string with the applied styles and colors
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
@ -143,6 +225,11 @@ public class ColoredString {
return sb.toString();
}
/**
* Get the string without the applied styles and colors
* @return The string without the applied styles and colors
*/
public String toNormalStringString() {
return strBytes == null || strBytes.length < 1 ?
null : new String(strBytes, StandardCharsets.UTF_8);