An open API service indexing awesome lists of open source software.

https://github.com/sec-ant/ansi-qr-code

Make Qr Codes suited for the terminal
https://github.com/sec-ant/ansi-qr-code

ansi-qr-code lib qr-code typescript

Last synced: about 1 year ago
JSON representation

Make Qr Codes suited for the terminal

Awesome Lists containing this project

README

          

# @sec-ant/ansi-qr-code

Make Qr Codes suited for the terminal. This package provides functions for creating both large and small QR codes, with customizable options such as error correction level, border size, module colors etc.

![ansi-qr-code-demo](https://github.com/Sec-ant/ansi-qr-code/assets/10386119/b3c7aa16-c178-4722-adc3-6a6f9f7ce1f6)

## Install

```bash
npm i @sec-ant/ansi-qr-code
```

## Usage

First, import the `makeAnsiQrCode` and/or `makeSmallAnsiQrCode` function(s) from the package.

```typescript
import { makeAnsiQrCode, makeSmallAnsiQrCode } from "@sec-ant/ansi-qr-code";
```

You can then generate a QR code by calling the function, passing your desired text along with optional configuration options.

### Large QR Codes

Large QR Codes are generated by `makeAnsiQrCode` function. This function uses ideographic space (" ") to construct the Qr Code.

```typescript
const ansiQrCode = makeAnsiQrCode("Hello, world!", {
ecc: ECC.MEDIUM,
border: 1,
blackModuleColor: BG_BLUE,
whiteModuleColor: BG_BRIGHT_GREEN,
});

console.log(ansiQrCode.value);
```

#### Options (AnsiQrCodeOptions)

- _ecc_: `Ecc` - Determines the error correction level. Can be one of `Ecc` enum values(`Ecc.LOW`, `Ecc.MEDIUM`, `Ecc.QUARTILE`, or `Ecc.HIGH`). Default is `Ecc.LOW`.
- _border_: `number` - Specifies the border size. Default is `4`.
- _blackModuleColor_: `BgColor | Reset` - Defines the ANSI color code for the black modules of the QR code. Default is `BG_BLACK`.
- _whiteModuleColor_: `BgColor | Reset` - Defines the ANSI color code for the white modules of the QR code. Default is `BG_BRIGHT_WHITE`.

### Small QR Codes

Small QR Codes are generated by `makeSmallAnsiQrCode` function. This function uses the combinations of block elements and space as _quadrublocks_ to construct the Qr Code.

| quadrublocks | `00` | `01` | `10` | `11` |
| :----------: | :--: | :--: | :--: | :--: |
| **`00`** | ` ` | ` ▄` | `▄ ` | `▄▄` |
| **`01`** | ` ▀` | ` █` | `▄▀` | `▄█` |
| **`10`** | `▀ ` | `▀▄` | `█ ` | `█▄` |
| **`11`** | `▀▀` | `▀█` | `█▀` | `██` |

```typescript
const smallAnsiQrCode = makeSmallAnsiQrCode("Hello, world!", {
ecc: ECC.MEDIUM,
border: 4,
blackModuleColor: FG_RED,
whiteModuleColor: RESET,
});

console.log(smallAnsiQrCode.value);
```

#### Options (SmallAnsiQrCodeOptions)

- _ecc_: `Ecc` - Determines the error correction level. Can be one of `Ecc` enum values(`Ecc.LOW`, `Ecc.MEDIUM`, `Ecc.QUARTILE`, or `Ecc.HIGH`). Default is `Ecc.LOW`.
- _border_: `number` - Specifies the border size. Default is `4`.
- _blackModuleColor_: `FgColor | Reset` (Default style) or `BgColor | Reset` (Inverted style) - Defines the ANSI color code for the black modules of the QR code.
- _whiteModuleColor_: `BgColor | Reset` (Default style) or `FgColor | Reset` (Inverted style) - Defines the ANSI color code for the white modules of the QR code.

### Additional exports

The package also exports `ECC` (an Enum that determines the error correction level) and some additional ANSI terminal colors and escape codes like `RESET`, `BG_BLACK`, `BG_BRIGHT_WHITE`, and `FG_BLACK`. You can use these constants to set the module colors of your QR codes.

## License

MIT