Skip to content

A Java library for making TUIs including easy inputs and text formatting

License

Notifications You must be signed in to change notification settings

KartoffelChipss/TUI4J

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TUI4J

maven build and test Release Monthly download statistics

TUI4J is a Java-based library for creating text-based user interfaces (TUIs).

Demo

Demo

You can find the code for this demo here.

Features

  • Flexible Inputs
    • Text Input
    • Multiline Text Input
    • Number Input
    • Boolean Input
    • Selection Input
    • Enter-To-Continue Input
  • Easy text formatting
    • Colors
    • Centering
    • Spacing
  • Customizable
    • Custom Input Prompts
    • Custom Input Validators

Installation

Make sure to replace 0.0.0 with the version you want to use. (You can find the latest version here)

Maven

<dependencies>
    <dependency>
        <groupId>com.github.KartoffelChipss</groupId>
        <artifactId>TUI4J</artifactId>
        <version>0.0.0</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Gradle

dependencies {
    implementation 'com.github.KartoffelChipss:TUI4J:0.0.0'
}

repositories {
    maven { url 'https://jitpack.io' }
}

API Documentation

The API documentation can be found here.

Example Usage

package org.example;

import org.strassburger.tui4j.formatting.Printer;
import org.strassburger.tui4j.input.*;
import org.strassburger.tui4j.input.validationrules.NumberValidationRules;
import org.strassburger.tui4j.input.validationrules.TextValidationRules;
import org.strassburger.tui4j.input.validationrules.ValidationRule;

public class Main {
    public static void main(String[] args) {
        Printer.println(" ");
        Printer.printCentered("&9&lWelcome to the TUI4J example application!");
        Printer.println(" ");

        String name = new TextInput()
                .setLabel("What is your name? ")
                .setInline(true)
                .read();

        String email = new TextInput()
                .setLabel("What is your email address?")
                .addValidationRules(
                        new ValidationRule<String>() {
                            @Override
                            public boolean validate(String s) {
                                return s.contains("@") && s.contains(".");
                            }

                            @Override
                            public String getErrorMessage() {
                                return "Email address must contain '@' and '.'";
                            }
                        }
                )
                .read();

        String about = new MultilineTextInput()
                .setLabel("Tell me about yourself: ")
                .addValidationRules(
                        TextValidationRules.minLength(30)
                )
                .read();

        int age = new IntegerInput()
                .setLabel("How old are you? ")
                .addValidationRules(
                        NumberValidationRules.greaterThan(0),
                        NumberValidationRules.lessThan(150)
                )
                .read();

        double height = new DoubleInput()
                .setLabel("How tall are you? ")
                .addValidationRules(
                        NumberValidationRules.greaterThan(0.0),
                        NumberValidationRules.lessThan(3.0)
                )
                .read();

        Printer.println(" ");
        Printer.println("&f&lYour Inputs:");
        Printer.printSpaceBetween("&fName", "&f" + name, "&7.");
        Printer.printSpaceBetween("&fEmail", "&f" + email, "&7.");
        Printer.printSpaceBetween("&fAge", "&f" + age, "&7.");
        Printer.printSpaceBetween("&fHeight", "&f" + height, "&7.");
        Printer.println(" ");

        boolean shouldContinue = new BooleanInput()
                .setLabel("Do you want to continue? ")
                .read();
    }
}

Color Codes

Text Colors

Code Color Preview
&0 Black &0 Black
&1 Blue &1 Blue
&2 Green &2 Green
&3 Cyan &3 Cyan
&4 Red &4 Red
&5 Magenta &5 Magenta
&6 Yellow &6 Yellow
&7 White &7 White
&8 Dark Gray &8 Dark Gray
&9 Blue &9 Bright Blue
&a Green &a Bright Green
&b Cyan &b Bright Cyan
&c Red &c Bright Red
&d Magenta &d Bright Magenta
&e Yellow &e Bright Yellow
&f White &f Bright White
&l Bold &l Bold
&n Underline &n Underline
&r Reset &r Reset

Background Colors

Code Color Preview
&x0 Black &x0 Black
&x1 Blue &x1 Blue
&x2 Green &x2 Green
&x3 Cyan &x3 Cyan
&x4 Red &x4 Red
&x5 Magenta &x5 Magenta
&x6 Yellow &x6 Yellow
&x7 White &x7 White
&x8 Dark Gray &x8 Dark Gray
&x9 Blue &x9 Bright Blue
&xa Green &xa Bright Green
&xb Cyan &xb Bright Cyan
&xc Red &xc Bright Red
&xd Magenta &xd Bright Magenta
&xe Yellow &xe Bright Yellow
&xf White &xf Bright White

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.