8.9 KiB
# @push.rocks/beautyfiglet
figlet display in nodejs
## Install
To install `@push.rocks/beautyfiglet`, you need to have Node.js and npm installed on your machine. Then, you can install it via npm:
```sh
npm install @push.rocks/beautyfiglet
Alternatively, you can add it as a dependency in your package.json
:
{
"dependencies": {
"@push.rocks/beautyfiglet": "^1.0.3"
}
}
Then run:
npm install
Usage
The @push.rocks/beautyfiglet
package is designed to allow you to display figlet text in your nodejs application. Below are instructions and examples on how to use it efficiently in your projects. We will cover all functionalities and illustrate several practical scenarios.
Basic Usage
First, you'll need to import the package. Here's an example of basic usage:
import * as beautyfiglet from '@push.rocks/beautyfiglet';
console.log(beautyfiglet.standardExport); // Outputs: Hi there! :) This is an exported string
Creating Figlet Text
Let’s say you want to display custom figlet text. Follow these steps:
- Import
figlet
from the figlet package, which@push.rocks/beautyfiglet
might internally use. - Use the
figlet
function to create ASCII art.
Here is a simple example:
import figlet from 'figlet';
figlet('Hello World', function(err, data) {
if (err) {
console.error('Something went wrong...');
console.dir(err);
return;
}
console.log(data);
});
Using Promises
To work with promises and async/await, you can wrap the figlet
method within a Promise:
import figlet from 'figlet';
const renderFiglet = (text: string): Promise<string> => {
return new Promise((resolve, reject) => {
figlet(text, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
// Usage example
(async () => {
try {
const figletText = await renderFiglet('Hello World');
console.log(figletText);
} catch (error) {
console.error(error);
}
})();
Customizing Fonts
Figlet allows for a variety of fonts to customize the output text. Here is how you can specify a font:
import figlet from 'figlet';
const renderFiglet = (text: string, font: string): Promise<string> => {
return new Promise((resolve, reject) => {
figlet.text(text, { font }, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
// Usage example
(async () => {
try {
const figletText = await renderFiglet('Hello World', 'Ghost');
console.log(figletText);
} catch (error) {
console.error(error);
}
})();
Available Fonts
To see the list of available fonts, you can use figlet.fonts
method. Here’s how to get a list of all fonts:
import figlet from 'figlet';
figlet.fonts((err, fonts) => {
if (err) {
console.error(err);
return;
}
console.log(fonts);
});
Text Layout Options
You can also customize text layout with horizontalLayout
and verticalLayout
options:
import figlet from 'figlet';
const renderFiglet = (text: string, font: string, hLayout: string, vLayout: string): Promise<string> => {
return new Promise((resolve, reject) => {
figlet.text(text, { font, horizontalLayout: hLayout, verticalLayout: vLayout }, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
// Usage example
(async () => {
try {
const figletText = await renderFiglet('Hello World', 'Ghost', 'full', 'default');
console.log(figletText);
} catch (error) {
console.error(error);
}
})();
Synchronous Usage
To generate Figlet text synchronously, use figlet.textSync
method:
import figlet from 'figlet';
const figletText = figlet.textSync('Hello World', {
font: 'Ghost',
horizontalLayout: 'default',
verticalLayout: 'default'
});
console.log(figletText);
Styling and Coloring
While figlet
itself doesn’t support colored text directly, you can use other npm packages like chalk
to add colors to figlet text. Here’s an example:
import figlet from 'figlet';
import chalk from 'chalk';
const renderFiglet = (text: string, font: string): Promise<string> => {
return new Promise((resolve, reject) => {
figlet.text(text, { font }, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
// Usage example
(async () => {
try {
const figletText = await renderFiglet('Hello World', 'Ghost');
console.log(chalk.blue(figletText));
} catch (error) {
console.error(error);
}
})();
Error Handling
Handling errors properly is crucial for robust applications. Here’s an example of how you can handle errors comprehensively:
import figlet from 'figlet';
const renderFiglet = (text: string, font: string): Promise<string> => {
return new Promise((resolve, reject) => {
figlet.text(text, { font }, (err, data) => {
if (err) {
reject(new Error(`Error generating figlet text: ${err.message}`));
} else {
resolve(data);
}
});
});
};
// Usage example
(async () => {
try {
const figletText = await renderFiglet('Hello World', 'InvalidFontName');
console.log(figletText);
} catch (error) {
console.error(error.message); // Output: Error generating figlet text: Font not found
}
})();
Integration with Web Servers
You can integrate @push.rocks/beautyfiglet
with a Node.js web server like Express. Here’s an example:
import express from 'express';
import figlet from 'figlet';
const app = express();
const renderFiglet = (text: string, font: string): Promise<string> => {
return new Promise((resolve, reject) => {
figlet.text(text, { font }, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
app.get('/figlet/:text', async (req, res) => {
const { text } = req.params;
try {
const figletText = await renderFiglet(text, 'Standard');
res.send(`<pre>${figletText}</pre>`);
} catch (error) {
res.status(500).send(error.message);
}
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});
CLI Tool Example
You can also create a command-line tool using @push.rocks/beautyfiglet
. Here’s an example using Node.js:
#!/usr/bin/env node
import figlet from 'figlet';
import { program } from 'commander';
program
.version('1.0.0')
.description('A simple Node CLI for generating Figlet text')
.option('-t, --text <text>', 'Text to render as Figlet')
.option('-f, --font <font>', 'Font to use', 'Standard');
program.parse(process.argv);
const options = program.opts();
figlet.text(options.text, { font: options.font }, (err, data) => {
if (err) {
console.error('Something went wrong...');
console.error(err.message);
return;
}
console.log(data);
});
To use this script as a CLI tool:
- Save the script as
figlet-cli.ts
. - Add a line to your
package.json
to register this as a bin command:
"bin": {
"figlet-cli": "./path/to/figlet-cli.ts"
}
- Make sure
figlet-cli.ts
is executable:
chmod +x ./path/to/figlet-cli.ts
- Then you can run:
npm link
figlet-cli --text "Hello World"
This concludes the extensive usage documentation for the @push.rocks/beautyfiglet package, showcasing multiple ways to generate ASCII art using figlet in Node.js. Explore the various options and configurations to best fit your project's needs.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.