1.2.55 • Published 5 months ago

fancy-printer v1.2.55

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

🌟 Fancy Printer 🌟

A fancy logger with a lot of customizations and blazingly fast speed!

No dependencies! Just one file!

npm.io npm

🤩 Don't forget to star the project on GitHub! 🤩

🌟 Table Of Contents 🌟


📩 Installation 📩

npm install fancy-printer

✨ Fanciness continues on Web ✨

<script src="./node_modules/fancy-printer/index.js"></script>

<script>
    printer.log("Hello, world!");
</script>
  • Note: Web doesn't have features like logging to a file because it is not possible.

🔧 Usage 🔧

✨ Creating a printer ✨

const printer = require("fancy-printer");

const options = {
    // some options...
};
const newPrinter = printer.create(options);
newPrinter.log("Hello, world!");
// OR just use it normally
printer.log("Hello, world!");

✨ Default tags ✨

printer.pass("Passed!");
printer.fail("Failed!");
printer.error("An error occurred!");
printer.warn("Something might go wrong!");
printer.info("This is a message!");
printer.debug("Check the line 5!");
printer.notice("Attention please!");
printer.log("An original log!");
printer.ready("I am ready!");
printer.assert(5 % 2 === 0, "5 is not divisible by 2!");

printer.tag("pass", "This worked as well!");

npm.io


✨ Creating tags ✨

printer.addTag("test", "HEY!", "", "#bb7373", "#ffff00");
printer.tag("test", "Hello, world!");

npm.io


✨ Formatting & Using/adding components & Changing the chr ✨

const DEFAULT_FORMAT = "%namespace%date %time %tag %text"; // this is the default format
printer.options.namespace = "My namespace"
// OR
const newPrinter = printer.namespace("My new namespace!");

printer.options.namespace = "";
printer.setFormat("%date %time %tag %text");
printer.info("Hello, world!");
printer.setFormat("%date %time %tag > %text");
printer.info("Hello, world!");
printer.setFormat("%date %time %tag %2plus2 %text");
printer.addComponent("2plus2", () => {
    return 2 + 2;
});
printer.info("Hello, world!");

printer.setCharacter("!");
printer.setFormat("!date !time !tag !2plus2 !text");
printer.info("Hello, world!");

printer.setFormat("!stack or just [ !filename:!line:!column ] !date !time !tag !2plus2 !text");
printer.info("Hello, world!");

npm.io


✨ Making the printer global ✨

printer.makeGlobal();

// some file.js where printer nor Printer is defined
printer.info("test");
Printer.static.info("test");
printer.makeGlobal(true);

// Now you can use the static printer from `console`
console.info("test");

✨ Logging to a specific file ✨

printer.addFile("./myFile.txt");

✨ Logging to a file periodically

printer.makeLoggerFile();
// which makes a "logs" folder and puts a unique file in it to log into
// OPTIONAL:
printer.makeLoggerFile({
    folder: "./myFolder/", // Default: logs. This is where the log files will be saved in.
    format: "my log %DD-%MM-%YYYY.txt", // Default: log-DD-MM-YYYY.log. The format of the name of the file.
    month: "long", // the type of the %month, expects: "numeric" | "2-digit" | "long" | "short" | "narrow"
    day: "long", // the type of the %month variable, expects: "long" | "short" | "narrow"
});

✨ Formatting arguments ✨

  • Y: Year
  • M: Month
  • D: Day
  • h: Hour
  • m: Minute
  • s: Second
  • S: Millisecond
  • month Gives the name of the month
  • day Gives the name of the day

✨ Padding on formatting arguments ✨

The length of the argument determines how long it should be.

For example if it's DD and if the day is 4, it becomes 04.

If it's YY and if the year is 2023, it becomes 23. (so it cuts from the end)


✨ Logging to a file with a hash

printer.makeHashedLoggerFile();
// which makes a "logs" folder and puts a unique file in it to log into
// OPTIONAL:
printer.makeHashedLoggerFile({
    folder: "./myFolder/", // Default: logs. This is where the log files will be saved in.
    radix: 16, // default 16, max 32. This is the time encoder setting
    divide: 3, // Default: 3. Divides the current timestamp into 10^divide. For example 3 would divide it to 1000 which makes it depend on seconds.
    format: "my log %t.txt" // Default: log-%t.log. The format of the name of the file. %t will be replaced by the time
});

✨ Substitutions ✨

✨ %o, %O, %s, %v ✨

Puts the object into place by making it a string.

Note: %o and %O only works on objects. %s only works on strings. %v works on everything.

printer.log("Hello, %s!", "world");

npm.io

✨ %d, %i ✨

Puts the integer into place.

printer.log("Hello, %d!", 12.34);
printer.log("Hello, %i!", 12.34);

npm.io

✨ %f ✨

Puts the floating number into place.

printer.log("Hello, %f!", 12.34);

npm.io

✨ %c ✨

Adds styling to the rest of the text. Uses CSS syntax.

printer.log("Hello, %cthis is red!%c and now it's blue!", "color: red", "color: blue");

npm.io

PropertyDefaultExpected typeDescription
background or background-colorNoneColorThe background color of the text
colorNoneColorThe color of the text
font-weightnormalnormal | bold | bolder | A positive integerThe boldness of the text
text-decorationnoneunderline | line-through | linethrough | strike-through | strikethroughThe decorations of the text. More than one can be used by separating with space
padding0A positive integerThe amount of white space to add on both sides
font-stylenormalnormal | italic | obliqueThe style of the text.

✨ Reading input ✨

const {inline} = printer;

(async () => {
    inline.log("Type something: ");
    const something = await printer.readLine();
    printer.warn("You entered: %s", something);

    inline.log("Press a key: ");
    const key = await printer.readKey();
    inline.print(key + "\n");
    printer.warn("You pressed: %s", key);

    inline.log("Enter your password: ");
    const pass = await printer.readPassword({character: "*"}); // Character is "*" by default.
    printer.warn("You entered: %s", pass);

    const list = ["an apple", "a grape", "a watermelon", "a piano!"];
    inline.log("Select something: ");
    const selection = await printer.readSelection(list);
    printer.warn("You entered: %s", list[selection]);

    const list2 = ["audi", "ford", "lamborghini", "beans"];
    inline.log("Select something: ");
    const selection2 = await printer.readSelectionListed(list2);
    printer.warn("You entered: %s", list2[selection2]);
})();

npm.io


✨ Utilities ✨

printer.print("Hello, world!"); // No substitution or formatting will be used and won't break line.

printer.printLine("Hello, world!"); // No substitution or formatting will be used and will break line.
printer.println("Hello, world!");

printer.println(printer.substitute("Hello,%c world!", "color: red")); // Manual substitution

printer.print("Hello! You!")
printer.backspace(5); // Erases 5 characters from the text written.
printer.cursorUp(5); // Moves 5 up.
printer.cursorRight(5); // Moves 5 right
printer.cursorDown(5); // Moves 5 down
printer.cursorLeft(5); // Moves 5 left

printer.clear(); // Clears the console (it's not included in the screenshot)

npm.io

✨ Fast Styling ✨

  • NOTE: This feature is disabled by default! This first line will enable it:
printer.options.styleSubstitutionsEnabled = true;

printer.info(
    "&0This is black",
    "&1This is blue",
    "&2This is green",
    "&3This is cyan",
    "&4This is red",
    "&5This is purple",
    "&6This is gold",
    "&7This is gray",
    "&8This is bold gray",
    "&9This is light blue",
    "&aThis is light green",
    "&bThis is cyan",
    "&cThis is light red",
    "&dThis is pink",
    "&eThis is yellow",
    "&fThis is white",
    "&lThis is bold",
    "&mThis is strike-through",
    "&nThis is underlined",
    "&oThis is italic",
    "&rThis will reset the styling",
    "&tBack to the 'info' tag's text color"
);

printer.addStyle("h", "color: red; background: yellow");

printer.info("my own styling starts &hnow! yay!");

npm.io


✨ Presets ✨

✨ Inline Preset ✨

This preset basically stops putting a line break at the end of logs.

Can be achieved by doing printer.options.newLine = false or just using the existing preset:

const {inline} = printer;

inline.log("Hello, ");
inline.print("world!");

npm.io

✨ Raw Preset ✨

This preset basically removes tags, dates and time from the format.

Can be achieved by doing printer.setFormat("%text") or just using the existing preset:

const {raw} = printer;

raw.log("Hello, world!");
raw.log("%cNeeds some coloring!", "color: red");

npm.io

✨ Brackets Preset ✨

This preset is a custom preset requested by a user.

Can be achieved by using the preset:

const {brackets} = printer;

brackets.pass("Passed!");
brackets.fail("Failed!");
brackets.error("An error occurred!");
brackets.warn("Something might go wrong!");
brackets.info("This is a message!");
brackets.debug("Check the line 5!");
brackets.notice("Attention please!");
brackets.log("An original log!");
brackets.ready("I am ready!");
brackets.assert(5 % 2 === 0, "5 is not divisible by 2!");

npm.io

🌟 HTML Preset 🌟

This preset is for web.

Makes the log result a html content and puts it into the document.

<script>
    const {html} = printer;

    html.updateBodyStyle(document.body);

    html.pass("Passed!");
    html.fail("Failed!");
    html.error("An error occurred!");
    html.warn("Something might go wrong!");
    html.info("This is a message!");
    html.debug("Check the line 5!");
    html.notice("Attention please!");
    html.log("An original log!");
    html.ready("I am ready!");
    html.assert(5 % 2 === 0, "5 is not divisible by 2!");

    html.options.htmlOut = a => printer.warning(a);

    html.log("test!");
    printer.notice("^^^ The text up there has been logged by using html.options.htmlOut! ^^^");
</script>

npm.io


✨ Logging Options ✨

KeyDefaultExpected typeDescription
format%date %time %tag %textstringThe formatting
substitutionsEnabledtruebooleanWhether the substitutions should work
styleSubstitutionsEnabledfalsebooleanWhether the color substitutions should work
componentsEnabledtruebooleanWhether the components should work
newLinetruebooleanWhether the logger should print the text with a line break at the end
namespace""stringThe text for %namespace tag
stylingEnabledtruebooleanWhether the stylings like colors or decorations should work
stdoutnullWriteStream or nullThe main output stream for the printer
stdinnullReadStream or nullThe main input stream for the printer
htmlOutnullElement or Function or nullIf it's an element, adds to that element's innerHTML. If it's a function runs it.
alwaysRGBfalsebooleanWhether basic colors should be processed as RGBs.
paletteName"default"stringThe custom palette's name.
disabledTags[]string[]The tags that won't be handled.
defaultBackgroundColorNoneColor(string)The default text background color for the printer
tagColorNoneColor(string)The default text color for the tags
tagBoldtruebooleanWhether the tag component is bold or not
tagItalicfalsebooleanWhether the tag component is italic or not
tagUnderlinefalsebooleanWhether the tag component is underlined or not
tagStrikethroughfalsebooleanWhether the tag component is struck-through or not
tagPadding2numberThe padding of the tag component
dateColorNoneColor(string)The text color of the date component
dateBackgroundColorblueBrightColor(string)The text background color of the date
dateBoldtruebooleanWhether the date component is bold or not
dateItalicfalsebooleanWhether the date component is italic or not
dateUnderlinefalsebooleanWhether the date component is underlined or not
dateStrikethroughfalsebooleanWhether the date component is struck-through or not
datePadding1numberThe padding of the date component
dateOptions.localeMatcherundefined"best fit" or "lookup" or undefinedDocs
dateOptions.weekdayundefined"long" or "short" or "narrow" or undefinedDocs
dateOptions.eraundefined"long" or "short" or "narrow" or undefinedDocs
dateOptions.yearundefined"numeric" or "2-digit" or undefinedDocs
dateOptions.month"short""numeric" or "2-digit" or "long" or "short" or "narrow" or undefinedDocs
dateOptions.day"numeric""numeric" or "2-digit" or undefinedDocs
dateOptions.hourundefined"numeric" or "2-digit" or undefinedDocs
dateOptions.minuteundefined"numeric" or "2-digit" or undefinedDocs
dateOptions.secondundefined"numeric" or "2-digit" or undefinedDocs
dateOptions.timeZoneNameundefined"short" or "long" or "shortOffset" or "longOffset" or "shortGeneric" or "longGeneric" or undefinedDocs
dateOptions.formatMatcherundefined"best fit" or "basic" or undefinedDocs
dateOptions.hour12undefinedboolean or undefinedDocs
dateOptions.timeZoneundefinedstring or undefinedDocs
timeColorNoneColor(string)The text color of the time component
timeBackgroundColorblueColor(string)The text background color of the time
timeBoldtruebooleanWhether the time component is bold or not
timeItalicfalsebooleanWhether the time component is italic or not
timeUnderlinefalsebooleanWhether the time component is underlined or not
timeStrikethroughfalsebooleanWhether the time component is struck-through or not
timePadding1numberThe padding of the time component
timeDatefalsebooleanWhether the time component has the date in it
timeHourtruebooleanWhether the time component has the hours in it
timeMinutetruebooleanWhether the time component has the minutes in it
timeSecondtruebooleanWhether the time component has the seconds in it
timeMillisecondfalsebooleanWhether the time component has the milliseconds in it
timeMillisecondLength3numberThe maximum length of the millisecond part of the time component
uptimeColorNoneColor(string)The text color of the uptime component
uptimeBackgroundColorblueColor(string)The text background color of the uptime
uptimeBoldtruebooleanWhether the uptime component is bold or not
uptimeItalicfalsebooleanWhether the uptime component is italic or not
uptimeUnderlinefalsebooleanWhether the uptime component is underlined or not
uptimeStrikethroughfalsebooleanWhether the uptime component is struck-through or not
uptimePadding1numberThe padding of the uptime component
uptimeDatefalsebooleanWhether the uptime component has the date in it
uptimeHourtruebooleanWhether the uptime component has the hours in it
uptimeMinutetruebooleanWhether the uptime component has the minutes in it
uptimeSecondtruebooleanWhether the uptime component has the seconds in it
uptimeMillisecondtruebooleanWhether the uptime component has the milliseconds in it
uptimeMillisecondLength2numberThe maximum length of the millisecond part of the uptime component
groupColorNoneColor(string)The text color of the group
groupBackgroundColorNoneColor(string)The text background color of the group
namespaceColorNoneColor(string)The text color of the namespace component
namespaceBackgroundColorblueColor(string)The text background color of the namespace
namespaceBoldtruebooleanWhether the namespace component is bold or not
namespaceItalicfalsebooleanWhether the namespace component is italic or not
namespaceUnderlinefalsebooleanWhether the namespace component is underlined or not
namespaceStrikethroughfalsebooleanWhether the namespace component is struck-through or not
namespacePadding1numberThe padding of the namespace component
filenameColorNoneColor(string)The text color of the filename component
filenameBackgroundColorblueColor(string)The text background color of the filename
filenameBoldtruebooleanWhether the filename component is bold or not
filenameItalicfalsebooleanWhether the filename component is italic or not
filenameUnderlinefalsebooleanWhether the filename component is underlined or not
filenameStrikethroughfalsebooleanWhether the filename component is struck-through or not
filenamePadding1numberThe padding of the filename component
lineColorNoneColor(string)The text color of the line component
lineBackgroundColorblueColor(string)The text background color of the line
lineBoldtruebooleanWhether the line component is bold or not
lineItalicfalsebooleanWhether the line component is italic or not
lineUnderlinefalsebooleanWhether the line component is underlined or not
lineStrikethroughfalsebooleanWhether the line component is struck-through or not
linePadding1numberThe padding of the line component
columnColorNoneColor(string)The text color of the column component
columnBackgroundColorblueColor(string)The text background color of the column
columnBoldtruebooleanWhether the column component is bold or not
columnItalicfalsebooleanWhether the column component is italic or not
columnUnderlinefalsebooleanWhether the column component is underlined or not
columnStrikethroughfalsebooleanWhether the column component is struck-through or not
columnPadding1numberThe padding of the column component
stackColorNoneColor(string)The text color of the stack component
stackBackgroundColorblueColor(string)The text background color of the stack
stackBoldtruebooleanWhether the stack component is bold or not
stackItalicfalsebooleanWhether the stack component is italic or not
stackUnderlinefalsebooleanWhether the stack component is underlined or not
stackStrikethroughfalsebooleanWhether the stack component is struck-through or not
stackPadding1numberThe padding of the stack component
1.2.55

5 months ago

1.2.41

9 months ago

1.2.42

9 months ago

1.2.40

9 months ago

1.2.45

9 months ago

1.2.46

9 months ago

1.2.43

9 months ago

1.2.44

9 months ago

1.2.49

9 months ago

1.2.47

9 months ago

1.2.48

9 months ago

1.2.52

9 months ago

1.2.53

9 months ago

1.2.50

9 months ago

1.2.51

9 months ago

1.2.54

9 months ago

1.2.38

9 months ago

1.2.39

9 months ago

1.2.19

11 months ago

1.2.20

11 months ago

1.2.23

11 months ago

1.2.24

11 months ago

1.2.21

11 months ago

1.2.22

11 months ago

1.2.27

11 months ago

1.2.28

11 months ago

1.2.25

11 months ago

1.2.26

11 months ago

1.2.29

11 months ago

1.2.30

11 months ago

1.2.31

11 months ago

1.2.34

10 months ago

1.2.32

11 months ago

1.2.33

10 months ago

1.2.36

10 months ago

1.2.37

10 months ago

1.2.18

11 months ago

1.2.17

11 months ago

1.2.16

11 months ago

1.2.15

11 months ago

1.2.14

11 months ago

1.2.13

12 months ago

1.2.12

12 months ago

1.2.11

12 months ago

1.2.10

12 months ago

1.2.9

12 months ago

1.2.82

12 months ago

1.2.81

12 months ago

1.2.8

12 months ago

1.2.7

12 months ago

1.2.6

12 months ago

1.2.5

12 months ago

1.2.4

12 months ago

1.2.3

12 months ago

1.2.2

12 months ago

1.2.1

12 months ago

1.2.0

12 months ago

1.1.2

12 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.30

12 months ago

1.0.29

12 months ago

1.0.28

12 months ago

1.0.27

12 months ago

1.0.26

12 months ago

1.0.25

12 months ago

1.0.24

12 months ago

1.0.23

12 months ago

1.0.22

12 months ago

1.0.21

12 months ago

1.0.20

12 months ago

1.0.19

12 months ago

1.0.18

12 months ago

1.0.17

12 months ago

1.0.16

12 months ago

1.0.14

12 months ago

1.0.13

12 months ago

1.0.12

12 months ago

1.0.11

12 months ago

1.0.10

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago