beautyfiglet/readme.md

251 lines
8.0 KiB
Markdown
Raw Normal View History

2024-05-29 14:10:51 +02:00
# @push.rocks/beautyfiglet
A Node.js module for creating figlet text displays.
2024-05-29 14:10:51 +02:00
## Install
To install `@push.rocks/beautyfiglet`, you should have Node.js and npm installed on your machine. Once you have these prerequisites, you can install the package via npm by running the following command in your terminal:
2024-05-29 14:10:51 +02:00
```sh
npm install @push.rocks/beautyfiglet
```
Alternatively, you can include it as a dependency in your `package.json` file:
2024-05-29 14:10:51 +02:00
```json
{
"dependencies": {
"@push.rocks/beautyfiglet": "^1.0.7"
2024-05-29 14:10:51 +02:00
}
}
```
Then execute:
2024-05-29 14:10:51 +02:00
```sh
npm install
```
## Usage
`@push.rocks/beautyfiglet` is a versatile Node.js module that lets you generate figlet-style ASCII art easily. Below, we will explore its functionalities with detailed examples, encompassing various scenarios to showcase how you can leverage this module in your projects.
2024-05-29 14:10:51 +02:00
### Basic Initialization
2024-05-29 14:10:51 +02:00
Begin by importing the `BeautyFiglet` class into your TypeScript file. You can access its methods to create ASCII art from text strings.
2024-05-29 14:10:51 +02:00
```typescript
import { BeautyFiglet } from '@push.rocks/beautyfiglet';
2024-05-29 14:10:51 +02:00
// A simple demonstration of using the standard export
console.log('Welcome to BeautyFiglet!'); // Outputs: Welcome to BeautyFiglet!
2024-05-29 14:10:51 +02:00
```
### Rendering Text with Figlet
2024-05-29 14:10:51 +02:00
Figlet is immensely popular for turning strings into ASCII banners. The following code illustrates how to convert text into a figlet display using `BeautyFiglet`.
2024-05-29 14:10:51 +02:00
```typescript
(async () => {
const figletText = await BeautyFiglet.renderDefault('Hello, World!');
console.log(figletText);
2024-05-29 14:10:51 +02:00
})();
```
### Customizing Font
2024-05-29 14:10:51 +02:00
`BeautyFiglet` not only supports different versions of text layouts, but it also allows you to specify fonts, making your output unique.
2024-05-29 14:10:51 +02:00
```typescript
(async () => {
const customFiglet = await BeautyFiglet.renderText('Beautiful Text', 'Ghost');
console.log(customFiglet);
2024-05-29 14:10:51 +02:00
})();
```
### Fetching Available Fonts
2024-05-29 14:10:51 +02:00
Understanding what fonts are available can help in making aesthetic decisions for your text.
2024-05-29 14:10:51 +02:00
```typescript
(async () => {
const fontsList = await BeautyFiglet.listFonts();
console.log('Available Fonts:');
console.log(fontsList.join(', '));
})();
2024-05-29 14:10:51 +02:00
```
### Using Custom Layouts
2024-05-29 14:10:51 +02:00
In addition to fonts, the layout of the text can be altered. You can specify both horizontal and vertical layouts.
2024-05-29 14:10:51 +02:00
```typescript
import figlet from 'figlet';
const renderCustomLayout = (text: string, font: string, hLayout: string, vLayout: string): Promise<string> => {
2024-05-29 14:10:51 +02:00
return new Promise((resolve, reject) => {
figlet.text(text, { font, horizontalLayout: hLayout, verticalLayout: vLayout }, (err, data) => {
if (err) reject(`Error: ${err.message}`);
else resolve(data);
2024-05-29 14:10:51 +02:00
});
});
};
// Example usage
2024-05-29 14:10:51 +02:00
(async () => {
try {
const customLayoutText = await renderCustomLayout('Creative Layout', 'Ghost', 'full', 'full');
console.log(customLayoutText);
2024-05-29 14:10:51 +02:00
} catch (error) {
console.error(error);
}
})();
```
### Synchronous Text Rendering
2024-05-29 14:10:51 +02:00
For situations requiring synchronous execution such as small scripts and testing scenarios, `textSync` comes in handy.
2024-05-29 14:10:51 +02:00
```typescript
import { figlet } from '@push.rocks/beautyfiglet.plugins';
2024-05-29 14:10:51 +02:00
const artwork = figlet.textSync('Synchronicity!', {
font: 'Standard',
2024-05-29 14:10:51 +02:00
horizontalLayout: 'default',
verticalLayout: 'default'
});
console.log(artwork);
2024-05-29 14:10:51 +02:00
```
### Enhancing Output with Color
2024-05-29 14:10:51 +02:00
To add more flair to your text displays, consider integrating popular color-coordinating libraries like `chalk`.
2024-05-29 14:10:51 +02:00
```typescript
import figlet from 'figlet';
import chalk from 'chalk';
(async () => {
const colorizedText = await BeautyFiglet.renderText('Color Me Beautiful', 'Standard');
console.log(chalk.magentaBright(colorizedText));
2024-05-29 14:10:51 +02:00
})();
```
### Robust Error Handling
2024-05-29 14:10:51 +02:00
Error handling is paramount in any application. This becomes even more pronounced when fonts or integrations might fail.
2024-05-29 14:10:51 +02:00
```typescript
(async () => {
try {
const result = await BeautyFiglet.renderText('Error Test', 'NonExistentFont');
console.log(result);
2024-05-29 14:10:51 +02:00
} catch (error) {
console.error(`Caught an error: ${error}`);
2024-05-29 14:10:51 +02:00
}
})();
```
### Web Server Integration
2024-05-29 14:10:51 +02:00
The module can effectively serve dynamically generated ASCII text over HTTP. Here's a simple example using Express:
2024-05-29 14:10:51 +02:00
```typescript
import express from 'express';
import { BeautyFiglet } from '@push.rocks/beautyfiglet';
2024-05-29 14:10:51 +02:00
const app = express();
app.get('/api/art/:text', async (req, res) => {
const textToRender = req.params.text;
2024-05-29 14:10:51 +02:00
try {
const asciiArt = await BeautyFiglet.renderDefault(textToRender);
res.send(`<pre>${asciiArt}</pre>`);
2024-05-29 14:10:51 +02:00
} catch (error) {
res.status(500).send(`Error: ${error.message}`);
2024-05-29 14:10:51 +02:00
}
});
app.listen(4000, () => {
console.log('Server running at http://localhost:4000/');
2024-05-29 14:10:51 +02:00
});
```
### Command-Line Interface (CLI)
2024-05-29 14:10:51 +02:00
Expand the utility of `BeautyFiglet` by creating a CLI tool for generating ASCII art directly from the command line.
2024-05-29 14:10:51 +02:00
```typescript
#!/usr/bin/env node
import { BeautyFiglet } from '@push.rocks/beautyfiglet';
import { Command } from 'commander';
const program = new Command();
program.version('1.0.7');
2024-05-29 14:10:51 +02:00
program
.option('-t, --text <text>', 'Text to render')
.option('-f, --font <font>', 'Font for rendering', 'Standard')
.action(async (cmd) => {
try {
const asciiArt = await BeautyFiglet.renderText(cmd.text, cmd.font);
console.log(asciiArt);
} catch (error) {
console.error(`Error: ${error}`);
}
});
2024-05-29 14:10:51 +02:00
program.parse(process.argv);
```
Running this tool involves these steps:
2024-05-29 14:10:51 +02:00
1. Save the script as `beautyfiglet-cli.ts`.
2. Ensure it is marked as executable:
2024-05-29 14:10:51 +02:00
```sh
chmod +x ./beautyfiglet-cli.ts
2024-05-29 14:10:51 +02:00
```
3. Link via npm:
2024-05-29 14:10:51 +02:00
```sh
npm link
2024-05-29 14:10:51 +02:00
```
4. Execute with:
2024-05-29 14:10:51 +02:00
```sh
beautyfiglet-cli --text "Hello World" --font "Ghost"
2024-05-29 14:10:51 +02:00
```
This command-line example showcases flexibility and enhanced usability for developers integrating the module seamlessly into their workflow.
### Summary of Best Practices
The extensive usage of `@push.rocks/beautyfiglet` demonstrates the importance of proper setup and thorough understanding of available options. From simple implementations to elaborate, colorful arrangements, this module empowers developers to unleash creativity with text in aesthetic and programmable ways.
Delve deeper into font choices and optional configurations to fully capitalize on this versatile tool for crafting meaningful ASCII text displays, all the while ensuring continuous learning of new patterns and module updates. With vigilant error management and the integration of expressive styles, your applications can achieve a blend of functionality and flair unmatched by basic textual representation.
This concludes the elaborate coverage of the `@push.rocks/beautyfiglet` module and its extensive capabilities within Node.js projects.
2024-05-29 14:10:51 +02:00
## 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.