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: ### Features:
- Support text color. - Support text color.
- Support background color. - Support background color.
- Support 256 colors (RGB).
- Support ANSI colors.
- Support indexed colors.
- Support text style. - Support text style.
- Lightweight and fast.
- Easy to use.
### How to add this library into your project ### How to add this library into your project
**Step 1**. Add the dependency **Step 1**. Add the dependency
@ -17,7 +22,6 @@
<version>0.1.5</version> <version>0.1.5</version>
</dependency> </dependency>
``` ```
**Step 2**. run this command `mvn install`
#### Gradle: #### Gradle:
**Step 1**. Add the JitPack repository to your build file<br> **Step 1**. Add the JitPack repository to your build file<br>
@ -37,43 +41,30 @@ allprojects {
} }
``` ```
### Usage: ## 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">
```java ## Requirements for development:
ConsoleManager manager = new DefaultConsoleManager(); // Create a new console manager - Maven
manager.setTextColor(TextColor.LIGHT_RED); // Set text color - jdk 17
manager.setBackgroundColor(BackgroundColor.DARK_BLUE); // Set background color - IntelliJ IDEA (not required but recommended)
manager.setTextStyle(TextStyle.ITALIC); // Set text style
manager.println("Hello World!"); // Print text ## TODO
``` - [ ] Add Tests
<img src="./Screenshots/2.png"> - [ ] Add Formatter
- [ ] Add Documentation
- [ ] Add more examples
```java ### Available in
public class Example1 {
public static void main(String[] args) {
ConsoleManager manager = new DefaultConsoleManager();
manager.print("Hello", BackgroundColor.DARK_YELLOW, TextColor.DARK_WHITE, TextStyle.BOLD, TextStyle.ITALIC); [![GitHub](https://img.shields.io/badge/GitHub-Main%20repo-brightgreen?style=for-the-badge&logo=GitHub)](https://github.com/Anas-Elgarhy/jpwd)
manager.print(", ", TextColor.LIGHT_GREEN); [![GitLab](https://img.shields.io/badge/GitLab-Mirror%20repo-brightgreen?style=for-the-badge&logo=GitLab)](https://gitlab.com/java-utils1/jpwd)
manager.print("I'm ", TextColor.DARK_YELLOW); [![BitBucket](https://img.shields.io/badge/BitBucket-Mirror%20repo-brightgreen?style=for-the-badge&logo=BitBucket)](https://bitbucket.org/anas_elgarhy/jpwd)
manager.print("Anas", TextColor.LIGHT_BLUE, TextStyle.DOUBLE_UNDERLINE); [![Codeberg](https://img.shields.io/badge/Codeberg-Mirror%20repo-brightgreen?style=for-the-badge&logo=Codeberg)](https://codeberg.org/java-utils/jpwd)
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">
#### 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 TextColor backgroundColor;
private ArrayList<TextStyle> styles; private ArrayList<TextStyle> styles;
/**
* Create a new empty ColoredString object
*/
public ColoredString() { public ColoredString() {
this(null); 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) { public ColoredString(String str, TextStyle... styles) {
this(str, (TextColor) null, 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) { public ColoredString(String str, TextColor foregroundColor, TextStyle... styles) {
this(str, foregroundColor, null, 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) { public ColoredString(String str, TextColor foregroundColor, TextColor backgroundColor, TextStyle... styles) {
strBytes = str != null ? strBytes = str != null ?
str.getBytes(StandardCharsets.UTF_8) : 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) { public ColoredString(String str, String foregroundColor, TextStyle... styles) {
this(str, foregroundColor, null, 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) { public ColoredString(String str, String foregroundColor, String backgroundColor, TextStyle... styles) {
this(str, this(str,
TextColor.Factory.fromString(foregroundColor), TextColor.Factory.fromString(foregroundColor),
@ -47,35 +81,67 @@ public class ColoredString {
styles); styles);
} }
/**
* Set the foreground color (text color)
* @param textColor The foreground color to be applied to the string
*/
public void setForegroundColor(TextColor textColor) { public void setForegroundColor(TextColor textColor) {
this.foregroundColor = 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) { public void setBackgroundColor(TextColor backgroundColor) {
this.backgroundColor = 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() { public TextColor getForegroundColor() {
return foregroundColor; return foregroundColor;
} }
/**
* Get the background color
* @return The background color, null if not set
*/
public TextColor getBackgroundColor() { public TextColor getBackgroundColor() {
return backgroundColor; return backgroundColor;
} }
/**
* Get the string as byte array
* @return The string as byte array
*/
public byte[] getBytes() { public byte[] getBytes() {
return strBytes; 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() { public byte[] getColoredBytes() {
if (strBytes == null) { if (strBytes == null) {
return null; return null;
@ -83,10 +149,18 @@ public class ColoredString {
return toString().getBytes(StandardCharsets.UTF_8); return toString().getBytes(StandardCharsets.UTF_8);
} }
/**
* Set the string
* @param str The string to be colored
*/
public void setStr(String str) { public void setStr(String str) {
strBytes = str.getBytes(StandardCharsets.UTF_8); 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) { public void addStyle(TextStyle style) {
if (styles == null) { if (styles == null) {
styles = new ArrayList<>(); styles = new ArrayList<>();
@ -94,6 +168,10 @@ public class ColoredString {
styles.add(style); styles.add(style);
} }
/**
* Remove style from the string
* @param style The style to be removed from the string
*/
public void removeStyle(TextStyle style) { public void removeStyle(TextStyle style) {
if (styles == null) { if (styles == null) {
return; 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 @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -143,6 +225,11 @@ public class ColoredString {
return sb.toString(); return sb.toString();
} }
/**
* Get the string without the applied styles and colors
* @return The string without the applied styles and colors
*/
public String toNormalStringString() { public String toNormalStringString() {
return strBytes == null || strBytes.length < 1 ? return strBytes == null || strBytes.length < 1 ?
null : new String(strBytes, StandardCharsets.UTF_8); null : new String(strBytes, StandardCharsets.UTF_8);