• Stars
    star
    4,440
  • Rank 9,652 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A react renderer for blessed.

react-blessed

A React custom renderer for the blessed library.

This renderer should currently be considered as experimental, is subject to change and will only work with React's latest version (17.x.x, using Fiber).

demo

Summary

Installation

You can install react-blessed through npm:

# Be sure to install react>=17.0.0 & blessed>=0.1.81 before
npm install blessed react

# Then just install `react-blessed`
npm install react-blessed

Demo

For a quick demo of what you could achieve with such a renderer you can clone this repository and check some of the examples:

git clone https://github.com/Yomguithereal/react-blessed
cd react-blessed
npm install

# To see which examples you can run:
npm run demo

# Then choose one to run:
npm run demo animation

Usage

Rendering a basic application

import React, {Component} from 'react';
import blessed from 'blessed';
import {render} from 'react-blessed';

// Rendering a simple centered box
class App extends Component {
  render() {
    return (
      <box top="center"
           left="center"
           width="50%"
           height="50%"
           border={{type: 'line'}}
           style={{border: {fg: 'blue'}}}>
        Hello World!
      </box>
    );
  }
}

// Creating our screen
const screen = blessed.screen({
  autoPadding: true,
  smartCSR: true,
  title: 'react-blessed hello world'
});

// Adding a way to quit the program
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
  return process.exit(0);
});

// Rendering the React app using our screen
const component = render(<App />, screen);

Nodes & text nodes

Any of the blessed widgets can be rendered through react-blessed by using a lowercased tag title.

Text nodes, on the other hand, will be rendered by applying the setContent method with the given text on the parent node.

Refs

As with React's DOM renderer, react-blessed lets you handle the original blessed nodes, if you ever need them, through refs.

class CustomList extends Component {
  componentDidMount() {

    // Focus on the first box
    this.refs.first.focus();
  }

  render() {
    return (
      <element>
        <box ref="first">
          First box.
        </box>
        <box ref="second">
          Second box.
        </box>
      </element>
    );
  }
}

Events

Any blessed node event can be caught through a on-prefixed listener:

class Completion extends Component {
  constructor(props) {
    super(props);

    this.state = {progress: 0, color: 'blue'};

    const interval = setInterval(() => {
      if (this.state.progress >= 100)
        return clearInterval(interval);

      this.setState({progress: this.state.progress + 1});
    }, 50);
  }

  render() {
    const {progress} = this.state,
          label = `Progress - ${progress}%`;

    // See the `onComplete` prop
    return <progressbar label={label}
                        onComplete={() => this.setState({color: 'green'})}
                        filled={progress}
                        style={{bar: {bg: this.state.color}}} />;
  }
}

Classes

For convenience, react-blessed lets you handle classes looking like what react-native proposes.

Just pass object or an array of objects as the class of your components likewise:

// Let's say we want all our elements to have a fancy blue border
const stylesheet = {
  bordered: {
    border: {
      type: 'line'
    },
    style: {
      border: {
        fg: 'blue'
      }
    }
  }
};

class App extends Component {
  render() {
    return (
      <element>
        <box class={stylesheet.bordered}>
          First box.
        </box>
        <box class={stylesheet.bordered}>
          Second box.
        </box>
      </element>
    );
  }
}

You can of course combine classes (note that the given array of classes will be compacted):

// Let's say we want all our elements to have a fancy blue border
const stylesheet = {
  bordered: {
    border: {
      type: 'line'
    },
    style: {
      border: {
        fg: 'blue'
      }
    }
  },
  magentaBackground: {
    style: {
      bg: 'magenta'
    }
  }
};

class App extends Component {
  render() {

    // If this flag is false, then the class won't apply to the second box
    const backgroundForSecondBox = this.props.backgroundForSecondBox;

    return (
      <element>
        <box class={[stylesheet.bordered, stylesheet.magentaBackground]}>
          First box.
        </box>
        <box class={[
          stylesheet.bordered,
          backgroundForSecondBox && stylesheet.magentaBackground
        ]}>
          Second box.
        </box>
      </element>
    );
  }
}

Using blessed forks

Because blessed is not actively maintained in quite a while, you might want to use one of it's forks. To do that, import createBlessedRenderer function instead:

import React, {Component} from 'react';
import blessed from 'neo-blessed';
import {createBlessedRenderer} from 'react-blessed';

const render = createBlessedRenderer(blessed);

Using the devtools

react-blessed can be used along with React's own devtools for convenience. To do so, just install react-devtools in your project and all should work out of the box when running the Electron app, as soon as a react-blessed program is running on one of your shells.

Roadmap

  • Full support (meaning every tags and options should be handled by the renderer).
  • react-blessed-contrib to add some sugar over the blessed-contrib library (probably through full-fledged components).

Faq

  • <list/> : To enable interactions, add mouse={ true } and/or keys={ true }

Contribution

Contributions are obviously welcome.

Be sure to add unit tests if relevant and pass them all before submitting your pull request.

# Installing the dev environment
git clone [email protected]:Yomguithereal/react-blessed.git
cd react-blessed
npm install

# Running the tests
npm test

License

MIT

More Repositories

1

baobab

JavaScript & TypeScript persistent and optionally immutable data tree with cursors.
JavaScript
3,148
star
2

mnemonist

Curated collection of data structures for the JavaScript/TypeScript language.
JavaScript
2,261
star
3

talisman

Straightforward fuzzy matching, information retrieval and NLP building blocks for JavaScript.
JavaScript
700
star
4

baobab-react

React integration for Baobab.
JavaScript
310
star
5

kotatsu

Straightforward command line tool to setup a development environment for modern JavaScript.
JavaScript
261
star
6

clj-fuzzy

A handy collection of algorithms dealing with fuzzy strings and phonetics.
Clojure
260
star
7

pandemonium

Typical random-related functions for JavaScript and TypeScript.
JavaScript
149
star
8

obliterator

Higher order iterator library for JavaScript and TypeScript.
JavaScript
53
star
9

mtgnode

A realtime web application for Magic the Gathering.
JavaScript
49
star
10

gexf

Gexf library for JavaScript
JavaScript
34
star
11

decypher

A handful of cypher utilities for Node.js
JavaScript
33
star
12

fog

A fuzzy matching & clustering library for python.
Python
25
star
13

colback

JavaScript asynchronous paradigm shift in the blink of an eye.
JavaScript
17
star
14

dolman

Light express app wrapper to develop an API comfortably.
JavaScript
16
star
15

vimeo-srt

A simplistic jQuery plugin to display srt subtitles along with an embedded vimeo video.
JavaScript
10
star
16

furuikeya

Procedural Haiku generation with Twitter API 1.1
Python
10
star
17

mtgparser

A compilation of handy parsers for popular Magic the Gathering deck formats.
JavaScript
9
star
18

agent-smith

Simple neo4jmyadmin powered by sigma.js
JavaScript
8
star
19

courses

Miscellaneous pages needing to be served.
CSS
8
star
20

takoyaki

Fuzzy clustering interface prototype.
JavaScript
7
star
21

recettes

Livre de recettes en format Markdown.
7
star
22

djax-client

A straightforward services client powered by djax.
JavaScript
7
star
23

ebbe

Collection of typical helper functions for python.
Python
6
star
24

deku-hmr-example

Simple example of HMR setup for deku.
JavaScript
5
star
25

sigma-experiments

Various experiments related to sigma v2.
TypeScript
5
star
26

clj-cmudict

Clojure wrapper for the CMU Pronouncing Dictionary.
Clojure
5
star
27

rhetorical

JavaScript library dealing with figures of speech, word play and literature.
JavaScript
4
star
28

colifrapy

Command Line Framework for Python
Python
4
star
29

yomguithereal.github.io

A blog.
HTML
4
star
30

levenshtein-lte1

A very fast JavaScript implementation of Levenshtein distance for the k <= 1 case.
JavaScript
3
star
31

helpers

Miscellaneous helper functions for JavaScript.
JavaScript
3
star
32

sabretache

A javascript library to discover similarities in html documents.
JavaScript
2
star
33

react-utilities

Some helpful utilities to work with React.
JavaScript
2
star
34

sigma-visualizer

A minimalist sigma usage.
JavaScript
2
star
35

plickle

Create, parse and execute Gherkin-like DSLs quick and easy.
JavaScript
2
star
36

baobab-deku

Simple helpers to use Baobab along with deku
JavaScript
2
star
37

StreamlineD3

Providing developers with a simple way to create dynamic D3.js visualizations with live data.
JavaScript
2
star
38

phylactery

Curated collection of data structures for Python.
Python
2
star
39

eslint-config

Just an eslint config.
JavaScript
1
star
40

npm-gexf-dependencies

Basic tool for converting the results of npm ls --json into a gexf file.
JavaScript
1
star
41

modular

Some files linked to modular synths.
1
star
42

sandcrawler-logger

A logger plugin for sandcrawler.
JavaScript
1
star
43

Yomguithereal

1
star
44

special-agent

Thin wrapper around a compilation of common user agent strings that one can query easily through tags.
JavaScript
1
star
45

python-daj

Read and write data without further ado.
Python
1
star
46

persil

Threadsafe & persistent python collections relying on SQLite.
Python
1
star
47

scuttlebutt-sigma

An attempt at visualizing scuttlebutt networks using sigma.js.
JavaScript
1
star
48

symlarjs

calculate and verify similarity between strings
JavaScript
1
star
49

afscrap

Aufeminin.com scraper for social researches.
JavaScript
1
star