beautiful_terminal

1.7.6 • Public • Published

beautiful_terminal package

The beautiful_terminal package is designed to enhance (color/modify) text in your command line application that does not support a graphical user interface (GUI).

Advantages =>

  • You can use any version of node js to use this package.
  • using this package is not difficult.
  • The syntax is not difficult.
  • And almost all terminals (especially modern ones) support it.
  • This package supports absolutely all colors.

installation =>

npm install beautiful_terminal

license

ISC

how to write code correctly =>

You can write the code in 2 ways

  1. Either you choose specific functions that you will use =>
const { _0m } = require("beautiful_terminal");
console.log(_0m);
  1. Or you define a variable/constant and use it to select specific functions =>
const bt = require("beautiful_terminal");
console.log(bt._0m);

I'll use the first method in the examples, but you can do it however you want.

colors =>

  • If you want to remove all decorations/colors from the text, use this => _0m

  • If you want to colorize the text normally, simply select the palette.<color>[0]. But if you want to colorize the font background, you can do the same, but in the array, it won't be 0, it will be 1. The Palette object contains several basic colors such as black, white, red, green, blue, yellow, orange, pink, cyan and gray.

  • However, if you're not satisfied with selecting from the basic color palette, you can create your own colors using the rgb(<r>,<g>,<b>) function or use the hexcolor("#<hex>") function. If you want to apply it to the background as well, just use rgb(<r>,<g>,<b>,<true or 1>), and the same applies to hexcolor!.

Here is a 2 simple examples of how to use colors:

const { palette, rgb, hexcolor, _0m } = require("beautiful_terminal");
console.log(palette.gray[0] + "Hello " + rgb(255, 235, 10) + "Java" + hexcolor("#000000") + palette.yellow[1] + "Script" + _0m + "!");

Here is second example:

const { palette, rgb, hexcolor, _0m } = require("beautiful_terminal");

const greenColor = rgb(43, 255, 5);
const redColor = hexcolor("#ff0000");
console.log(greenColor);
async function main(){
    console.log("Hecking started...");
    for(let i=0;i<=100;i+=10){  
        await new Promise((resolve) => { 
            setTimeout(resolve,1000);
        });
        console.log("hacking FBI "+ redColor + i + "%" + greenColor);
        if(i === 100){
            console.log("hecking successful");
        }
    }
}main();
  • If you only needed to remove the background from the text and keep its color, you can't use _0m (you can, but you'd have to re-enable the color and other features, which would make your syntax much longer), because it remove everything, and that's exactly why why you have a remove object available to resey exactly what you need. For example, if you only need to remove the background color, you can do it as remove.background. And remove.color when removing the text color.

here is an example of how to use remove:

const { _0m, palette, remove } = require("beautiful_terminal");

const redColor = palette.red[0];
const blueBgColor = palette.blue[1];

console.log(_0m + blueBgColor + redColor + "Hello" + remove.background + " JavaScript" + _0m);

decorations =>

  • Even if the colors are nice and are enough to decorate your application, sometimes it is not the same and you would need to decorate your text even more interestingly. For this purpose, I have created an object called decor which contains the following features such as bold font bold, italic font italic, underlined font underline, strikethrough font strikethrough, translucent font (dim) dim, baking contrast inverse and text hiding hidden.

  • This object behaves similarly to the colors decor.underline[0] BUT with the difference that the 2nd element in the array serves to cancel the given decoration.

Here is a 2 simple examples of how to use decorations:

you can combine it

const { _0m, decor, palette } = require("beautiful_terminal");

console.log(_0m + palette.red[0] + "Hello " + decor.bold[0] + "Java" + decor.bold[1] + decor.strikethrough[0] + "Script" + _0m + "!");
console.log(decor.bold[0] + decor.italics[0] + decor.underline[0] + "Hello JavaScript!" + _0m);

Here is second example:

const { _0m, decor, palette } = require("beautiful_terminal");
const readline = require("readline");

const redColor = palette.red[0];
const yellowColor = palette.yellow[0];
const pinkColor = palette.pink[0];
const greenColor = palette.green[0]
const ulFont = decor.underline[0];
const rmulFont = decor.underline[1];
const italicsFont = decor.italics[0];

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question(`do you like ${yellowColor}${ulFont}bananas${_0m}: ${greenColor}${italicsFont}`,(data) => {
    if(data === "yes"){
        console.log(_0m + "I " + pinkColor +"love " + redColor + "<3 " + _0m + "you!");
    } else {
        console.log(_0m + redColor + ulFont + "ERROR" + rmulFont + " someting wrong :(" + _0m);
    }
    rl.close();
});
  • If you need to remove all decorations at once but at the same time you need to keep the color/background of the text, you can use the reset object again, reset.decor. With this object, you don't have to remove decorations one by one (dim is an exception).

here is an example of how to use remove with dekor:

const { _0m, remove, palette, decor } = require("beautiful_terminal");

const yellowColor = palette.yellow[0];
const ulFont = decor.underline[0];
const italicsFont = decor.italics[0];
const rmAllDecor = remove.decor;

console.log(_0m + yellowColor + ulFont + italicsFont + "Hello" + rmAllDecor + " JavaScript" + _0m);

gradient =>

  • Ok, you can adjust the color of the text, but would you like more?? No problem, the gradient function can improve your text even more!

  • This is what gradient(<color>,<color>,"your text",<true or 1 for background>); looks like. As you can see, 2 colors are specified. and at the end text is entered to perform the transition. when using the palette object or other functions, it doesn't matter whether you use the font color palette.red[0] or the background color palette.red[1]. Because it is specified as the last CLGUI parameter.

  • If you want the function to be applied to the background as well, just add one argument at the end (as with the rgb or hexcolor function true or 1) as you can see in the example.

Here is a simple example of how to use gradient:

const { _0m, rgb, hexcolor, gradient, palette } = require("beautiful_terminal");

console.log(gradient(hexcolor("#0516ff"),rgb(5, 255, 22),"Hello JavaScript!"));

console.log(gradient(palette.red[1],palette.black[0],"Hello JavaScript!"));

const pinkToYellow = gradient(rgb(255, 255, 0),hexcolor("#ff00ee"),"THIS FUNCTION IS AMAZING!!!",true);

console.log("How the gradient function works =>" + palette.white[0] + " Hi how are you. " + pinkToYellow + _0m);

command line GUI =>

  • This package should mainly be for improving the command line interface (CLI) and I would like to create something like a command line GUI as part of this package. It will not be a full-fledged GUI, but rather a universal and rather limited one. It's not finished yet, but I'd like to push it further and expand the capabilities of this function.

how to use it =>

  • It works in the that you call a function called CLGUI() (command line graphic user interface) and write all the parameters you need into it =>
CLGUI(
  ["<text>","<text>","<text>",<...],
  <color or decoration>,
  "<text separator>",
  "<button to move left>",
  "<button to move right>",
  "<button with which you can interact with the text>",
  "<button to move to the original position>",
  [<function for the element in the first array>,<...]);
  • for example, if you don't want to be able to move left, you enter null or "" in the parameter for the button to use for left movement.

here is an example to use:

const { CLGUI, decor, palette, hexcolor, _0m } = require("beautiful_terminal");
const fs = require("fs");
const os = require("os");

function timeInfo(){
    const currentTime = new Date();
    const hours = currentTime.getHours();
    const minutes = currentTime.getMinutes();
    const seconds = currentTime.getSeconds();
    console.log(`time is: ${hours}:${minutes}:${seconds}`);
}

function OSinfo(){
    console.log("os: " + hexcolor("#05ff7e") + os.platform() + _0m);  
    console.log("version: " + hexcolor("0000ff") + os.release() + _0m);
    console.log("CPU architecture: " + hexcolor("ff0000") + os.arch() + _0m);
}

function exit(){
    console.log("Press ENTER for end.")
}

CLGUI(
  ["info about time","info about os","exit","click here"],
  decor.underline[0] + hexcolor("#ff00cc"),
  "|",
  "a",
  "d",
  " ",
  "r",
  [timeInfo, OSinfo, exit]);
  • From version 1.6.5 you can also put in the parameters of the CLGUI function functions that must be waited for.

Here is an example of a useful application:

const { CLGUI, decor, palette, hexcolor, _0m } = require("beautiful_terminal");
const readline = require("readline");
const fs = require("fs");
const path = require("path");

// input function
function question(prompt) {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  return new Promise((resolve, reject) => {
    rl.question(prompt, (answer) => {
      rl.close();
      resolve(answer);
    });
  });
}

// colors
const mainColor = hexcolor("#84ff00");
const errColor = palette.red[0] + decor.bold[0];

// show path function
let currentDirectory = process.cwd();
function showPath(){
  console.log("Current Directory: " + mainColor + currentDirectory + _0m);
}

// read directory function
function dirRead(){
  return new Promise((resolve, reject) => {
    fs.readdir(currentDirectory,(err,data)=>{
      if(err){
      console.log(errColor + "ERROR something wrong!" + _0m);
        console.log(err);
        reject();
      } else {
        console.log(mainColor + data.join(_0m + ", " + mainColor)); 
        resolve();
      }
    });
  });
}

// create file function
async function createFile(){
  await dirRead();
  const name = await question(_0m + "Enter a file name: " + mainColor);
  fs.writeFile(currentDirectory + "/" + name,"",(err)=>{
     if(err){
         console.log(errColor + "ERROR something wrong!" + _0m);
         console.log(err);
         
     } else {
         console.log(_0m + "file is created!");         
     }
});   
}

//delete file function
async function deleteFile(){
  await dirRead();
  const name = await question(_0m + "Enter a file name: " + mainColor);
  fs.unlink(currentDirectory + "/" + name,(err)=>{
   if(err){
       console.log(errColor + "ERROR something wrong!" + _0m);
       console.log(err);
         
   } else {
     console.log(_0m + "file is delete!");         
   }
  });    
}

// cd function
async function changeDir() { 
  console.log("Current Directory: " + mainColor + currentDirectory + "/" + _0m);
  const input = await question(_0m + "Enter a directory name: " + mainColor);
  if (input === "..") {
    currentDirectory = path.resolve(currentDirectory, "..");
  } else {
   currentDirectory = path.resolve(currentDirectory, input);      
  }
  console.log(_0m + "Current Directory: " + mainColor + currentDirectory + _0m);
}

//read file function
async function fileRead(){
  await dirRead();
  const name = await question(_0m + "Enter a file name: " + mainColor); 
  console.log(_0m + "-------------------------" + hexcolor("#ea73ff"));
  await new Promise((resolve) => {
    const readStream = fs.createReadStream(currentDirectory + "/" + name,"utf8");
  
    readStream.on("data",(chunk)=>{
      console.log(chunk);
      resolve();
    });
  });
  console.log(_0m + "-------------------------");
}

//"main" CLGUI function
CLGUI(
  [" show path "," create file "," delete file "," change path "," read file "],
  decor.inverse[0],
  "",
  "a",
  "d",
  " ",
  "r",
  [showPath, createFile, deleteFile, changeDir, fileRead]);
    
  • try to create functions to create and delete folders yourself

Package Sidebar

Install

npm i beautiful_terminal

Weekly Downloads

1

Version

1.7.6

License

ISC

Unpacked Size

19.2 kB

Total Files

3

Last publish

Collaborators

  • joel1bartok