GithubHelp home page GithubHelp logo

00mjk / jfmt Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 797 KB

๐Ÿ”ฌ This is a simple Java library that allows you to format strings more easily.

License: MIT License

Java 97.43% Makefile 0.68% Shell 1.89%

jfmt's Introduction

JFMT - Java Formatter

Logo


Table of Contents


Demo

Demo

To run the Demo, make sure you have Java and Maven installed.
Then, open a terminal and type these commands:
git clone https://github.com/0l1v3rr/jfmt.git
cd jfmt
bash demo.sh

Formatter

The Formatter.stringf() method is a function, which provides you the ability to format strings more easily.
It supports Entities, Symbols, and Format Characters.

Entities and symbols

Click here to expand

&n: Line break
&N: Double line break
&t: Current time in a default format
&d: Current date in a default format

$c: Copyright (ยฉ)
$r: Registered trademark (ยฎ)
$e: Element of (โˆˆ)
$p: N-ary product (โˆ)
$s: N-ary summation (โˆ‘)
$x: Plus-or-minus sign (ยฑ)
$2: Superscript two (ยฒ)
$3: Superscript three (ยณ)
$tm: Trademark (โ„ข)
$Ua: Upwards Arrow (โ†‘)
$Da: Downwards Arrow (โ†“)
$La: Leftwards Arrow (โ†)
$Ra: Rightwards Arrow (โ†’)

Format Characters

Click here to expand

%v: Value in the default (.toString()) format
%s: String in a default format
%S: String in uppercase
%q: String in a double quote format. ("text")
%b: Boolean (true/false)
%B: Boolean in uppercase
%d: A round number (if you provide a float or a double, it'll split off everything after the '.')
%o: Number in binary - You must provide it in decimal
%i: Number in decimal - You must provide it in binary
%f: A float or a double number
%f<n>: A float or a double number rounded to n decimal places. (E.g.: %f3)
%r: String in a reversed format
%R: String in a reversed & uppercase format
%t: java.util.Date format - default is "yyyy-MM-dd"

Note: Assign the appropriate type of variable to the characters, otherwise, it will not be formatted.
For example:

String result = Formatter.stringf("This is a boolean: %b", "true");
//Result:
//This is a boolean: %b
//Because the passed value is not a boolean but a string.
//To make it work, you want to use it like this:
String result = Formatter.stringf("This is a boolean: %b", true);

You can modify the default date and time format:

Formatter.setDateFormat("dd-MM-yyyy");
Formatter.setTimeFormat("hh:mm:ss");
Formatter.stringf("Hi %s! Date: &d, Time: &t", "John");
// Result: "Hi John! Date: 03-12-2021, Time: 10:04:43" (ofc with the current date and time)

Simple Formatter

The SimpleFormatter's stringf() method is easier to use, and it's faster than the Formatter.stringf(), but it knows less.
It does not support entities and characters.
If you want to use this method, you pass the arguments like this: "{0}, {1}"
The 0th argument is the first argument, you pass.
You can use it multiple times.
Example:

String result = SimpleFormatter.stringf("{0} is {1} years old.", "John", 17);
// Result:
// John is 17 years old.

See more examples here


Colorizing the CLI Text

// Method 1: Custom Colored Output
CustomColorFormat ccf = new CustomColorFormat(TColor.BLACK, BgColor.BRIGHT_RED, TDecoration.UNDERLINE);
ccf.println("This is a black underline text with bright red background.");

// Method 2: Simple Formatting
System.out.println(BackgroundColor.red("This is a text with red background."));
System.out.println(BackgroundColor.red(TextColor.yellow("This is a yellow text with red background.")));

// Method 3: Colorize.colorf() function
System.out.println(Colorize.colorf("{t:yellow}This is a yellow text."));
System.out.println(Colorize.colorf("{t:bgreen}This is a green text. {reset}This is a text in default format."));
System.out.println(Colorize.colorf("{bg:bred}This is a text with bright red background."));

// Method 4: Colorize.string() function
System.out.println(Colorize.string("This is a red text.", TColor.RED));
System.out.println(Colorize.string("This is a red text with black background.", TColor.RED, BgColor.BLACK));
System.out.println(Colorize.string("This is a red, underline text with black background.", TColor.RED, BgColor.BLACK, TDecoration.UNDERLINE));

See more examples here


256 Color Support

// In the constructor, you must provide a number between 0 and 255.
Color256 t = new Text256(200);
t.println("Text");
// Output: Text
// With the ANSI color code of 200

// You can modify the code like this:
t.setCode(201);
t.println("Text2");
// Output: Text2
// With the ANSI color code of 201

// You can use it as background as well:
Color256 t = new Background256(1);
t.println("Text");
// Output: Text
// With the background of code 1

// Random color:
t.random("This is a random colored text.");

// Example:
Color256 t = new Text256(0);
for(int i = 0; i < 256; i++) {
    t.print(i+"   ");
    t.setCode(i);
}

RGB Colors

// RGB text
RgbColor rgb = new RgbText(255, 0, 0);
rgb.println("Text");

// Rainbow text:
RgbText.rainbow("Text");
// Random RGB text:
RgbText.random("Text");

// RGB background
RgbColor rgb = new RgbBackground(255, 0, 0);
rgb.println("Text");

Contributing

If you want to contribute to this project:

  1. Star this repository
  2. Open an Issue. There are some Issue Templates available.
  3. Open a Pull Request, where describe what you did and why.

Thanks for contributing! (:

License

This library is licensed under the terms of the MIT License

jfmt's People

Contributors

0l1v3rr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.