Skip to content

FimastGDOld/ColorSide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ColorSide

Colorful terminal control, amazing inputs and Random features!

Install

npm install colorside@latest

Setup

import ColorSide from 'colorside';

const cs: any = new ColorSide("en"); // language if instructions: 'ru' OR 'en'
cs.use(/* class */); // Using package classes

Note

Если вы из России, по возможности купите мне поесть :)
Карта: 5599002097457313

Build

npm run compile

First call

import ColorSide from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console) // Using class cs.console

cs.console.log("My first log!"); // like console.log

Output (cs.console)

import ColorSide from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);

cs.console.log("It's a basic log"); // Basic log like console.log
cs.console.warn("Warn??"); // Warn message with yellow text foreground
cs.console.error("Error!"); // Error message with red text foreground
cs.console.fatal("FATAL ERROR!!!") // Fatal error message with red text background and black foreground

Demo 1:
Demo 1

Text coloring

  • cs.fg.color() - foreground color
  • cs.fg.bright.color() - foreground bright color
  • cs.bg.color() - background color
  • cs.bg.bright.color() - background bright color
  • cs.format.style() - bold / underlined text

import ColorSide from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);

cs.console.log(cs.fg.bright.green(cs.format.bold("Green bright bold text")));
// likewise cs.format.underlined

ANSI256 colors (xterm-256color)

- colorside@^1.2.3

import { ANSI256 } from 'colorside/ansi256';

ANSI256.get.fg("#hex"); // returns only ansi256-color code. need to ANSI.reset at the end
ANSI256.get.bg("#hex"); // like get.fg, but background

ANSI256.replace.fg("#hex", "Some Text"); // returns a fg color wrapper only for text from the 2nd argument
ANSI256.replace.bg("#hex", "Some Text"); // like replace.fg, but background

Demo 2:
Demo 2

Clear console

import ColorSide from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);

cs.console.clear(); // clear screen

Process freezing

The application does not close spontaneously

import ColorSide from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);

cs.console.freeze();
cs.console.warn("Only Ctrl-C will kill app");

// Later, you can unfreeze the process automatically
// cs.console.unfreeze();

Sync sleep

Like setTimeout, but sync

import ColorSide from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);

cs.console.log("Downloading...");
cs.console.sleepSync(1000); // 1s
cs.console.log("Success!");

Input (cs.input)

Input prompts using inquirer API

Basic input

import ColorSide from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input); // Using class cs.input

async function main(): Promise<void> {
    const name = await cs.input.readline("Enter your name: "); // Basic input and nothing else
    cs.console.log(`Your name: ${cs.fg.bright.magenta(name)}`);
}

main();

Demo 3:
Demo 3

Advanced input (text)

import ColorSide, { ANSI } from 'colorside';
// { ANSI } import required!

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input);

async function main(): Promise<void> {
    const name = await cs.input.readtext({
        message: /*(string)*/ "Enter your name:", // <- [No space after ':']
        
        /* optional params */
        // messageColor: /*(string)*/ ANSI.fg.green, - message color
        inputColor: /*(string)*/ ANSI.fg.bright.cyan, // input color
        // prefix: /*(null | string)*/ '>', - custom prefix. null = prefix disabled
        // finishPrefix: /*(boolean)*/ true, - show final prefix when input readed
        default: /*(string)*/ "John", // default name
        validate: (val) => (val.toLowerCase() !== "adolf") || "Sorry, we don't support such names" // input validation
    });
    cs.console.log(`Your name: ${cs.fg.bright.magenta(name)}`);
}

main();

Demo 4:
Demo 4

Advanced input (number)

import ColorSide, { ANSI } from 'colorside';
// { ANSI } import required!

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input);

async function main(): Promise<void> {
    const name = await cs.input.readnumber({
        message: /*(string)*/ "Enter your name:", // <- [No space after ':']
        
        /* optional params */
        // messageColor: /*(string)*/ ANSI.fg.green, - message color
        inputColor: /*(string)*/ ANSI.fg.bright.cyan, // input color
        // prefix: /*(null | string)*/ '>', - custom prefix. null = prefix disabled
        // finishPrefix: /*(boolean)*/ true, - show final prefix when input readed
        default: /*(string)*/ "42", // default name
        validate: (val) => (Number(val > 100)) ? "Very big number!" : true // input validation
    });
    cs.console.log(`Your number: ${cs.fg.bright.magenta(name)}`);
}

main();

Demo 5:
Demo 5

Advanced input (list)

import ColorSide, { ANSI } from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input);

async function main(): Promise<void> {
    const name = await cs.input.readlist({
        message: /*(string)*/ "Select OS:",
        choices: [
            { name: 'Windows', value: 'Windows' },
            { name: 'Linux (it\'s a base)', value: 'Linux'},
            { name: 'Mac', value: 'Mac', disabled: true}
        ],
        
        /* optional params */
        // messageColor: /*(string)*/ ANSI.fg.green, - message color
        // prefix: /*(null | string)*/ '>', - custom prefix. null = prefix disabled
        // finishPrefix: /*(boolean)*/ true, - show final prefix when input readed
        // default: /*(string)*/ "value", - default name
        // validate: (val) => (condition) || else - input validation
        // instructions: (boolean) false, - hide input instructions 
        /* CUSTOM HIGHLIGHTING
        selectColors: [
            {
                keys: ["Admin"],
                color: ANSI.fg.bright.red
            }
        ],
        answerColor: "auto" / ANSI.color (string) // What will be the final color of the answer: auto - uses indexes from selectColor
        */
    });
    cs.console.log(`OS selected: ${cs.fg.bright.green(name)}`);
}

main();

Demo 6:
Demo 6

Advanced input (checkbox)

import ColorSide, { ANSI } from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input);

async function main(): Promise<void> { 
    const name = await cs.input.readcheckbox({
        message: /*(string)*/ "Select OS:",
        choices: [
            { name: 'Windows', value: 'Windows', description: 'Base user OS' },
            { name: 'Linux', value: 'Linux', description: 'Base server OS' },
            { name: 'Mac', value: 'Mac', description: 'Not OS' }
        ],
        
        /* optional params */
        // messageColor: /*(string)*/ ANSI.fg.green, - message color
        // prefix: /*(null | string)*/ '>', - custom prefix. null = prefix disabled
        // finishPrefix: /*(boolean)*/ true, - show final prefix when input readed
        // default: /*(string)*/ "value", - default name
        // validate: (val) => (condition) || else - input validation
        // instructions: (boolean) false - hide input instructions 
    });
    cs.console.log(`OS selected: ${cs.fg.bright.green(name)}`);
/* CUSTOM HIGHLIGHTING supports already like in readlist */
}

main();

Demo 7:
Demo 7

Advanced input (password)

import ColorSide, { ANSI } from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input);

async function main(): Promise<void> {
    const name = await cs.input.readpassword({
        message: /*(string)*/ "Enter your pass:",
        
        /* optional params */
        // messageColor: /*(string)*/ ANSI.fg.green, - message color
        inputColor: /*(string)*/ ANSI.fg.bright.cyan, // input color
        // prefix: /*(null | string)*/ '>', - custom prefix. null = prefix disabled
        // finishPrefix: /*(boolean)*/ true, - show final prefix when input readed
        //default: /*(string)*/ - default name
        validate: (val) => (val === "ForeverHost") || "Incorrect password" // input validation
    });
    cs.console.log(`Your pass: ${cs.fg.bright.magenta(name)}`);
}

main();

Demo 8:
Demo 8

Advanced input (confirm)

import ColorSide, { ANSI } from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input);

async function main(): Promise<void> {
    const name = await cs.input.readconfirm({
        message: /*(string)*/ "Install package?",
        
        /* optional params */
        // messageColor: /*(string)*/ ANSI.fg.green, - message color
        inputColor: /*(string)*/ ANSI.fg.bright.cyan, // input color
        // prefix: /*(null | string)*/ '>', - custom prefix. null = prefix disabled
        // finishPrefix: /*(boolean)*/ true, - show final prefix when input readed
        // default: /*(string)*/ - default name
        // validate: (val) => (condition) || else - input validation
    });
    cs.console.log(`Install package: ${cs.fg.bright.magenta(name)}`);
}

main();

Demo 9:
Demo 9

Advanced input (toggle)

import ColorSide, { ANSI } from 'colorside';

const cs: any = new ColorSide("ru");
cs.use(cs.console);
cs.use(cs.input);

async function main(): Promise<void> {
    const name = await cs.input.readtoggle({
        message: /*(string)*/ "Install package?",
        active: "Yes",
        inactive: "No",
        /* optional params */
        // messageColor: /*(string)*/ ANSI.fg.green, - message color
        // STRING ONLY prefix: /*(string)*/ '>', - custom prefix. null = prefix disabled
        // default: /*(string)*/ - default name
        // validate: (val) => (condition) || else - input validation
        /* CUSTOM HIGHLIGHTING
        selectColors: {
            active: ANSI.color,
            inactive: ANSI.color 
        }
        answerColor: "auto" / ANSI.color (string) // What will be the final color of the answer: auto - uses indexes from selectColor
        */
        
        /** @returns true / false */
    });
    cs.console.log(`Install package: ${cs.fg.bright.magenta(name)}`);
}

main();

Demo 10:
Demo 10

Random module

import ColorSide, { Random, ANSI } from 'colorside';

Random.Int(1, 7); // Random int in range
Random.Float(1, 7); // Random float in range
Random.String(['A-Z', 'a-z', '0-9', /* custom symbols */ '&_₽*$+=%'], 8 /* length */); // Random string value

About

Amazing colorful library for Typescript

Resources

License

Stars

Watchers

Forks

Packages

No packages published