DEV Community

Cover image for Day 1: React in Terminal ๐ŸŽจ
Valeria
Valeria

Posted on

4 1

Day 1: React in Terminal ๐ŸŽจ

Ho-ho-ho! The time is finally here and, following the suggested format, the first gift I'd like to share with you is this amazing gem: https://term.ink/.

This library allows you to write terminal interfaces using React!

It works great with node, but the easiest way to try it out and avoid setting up TypeScript is by using Deno.

Install dependencies:

deno add npm:ink npm:react
Enter fullscreen mode Exit fullscreen mode

Create a script, e.g. main.tsx:

import React, { useState, useEffect } from "npm:react";
import { render, Text } from "npm:ink";

const Counter = () => {
  const [counter, setCounter] = useState<number>(0);

  useEffect(() => {
    const timer = setInterval(() => {
      setCounter((previousCounter: number) => previousCounter + 1);
    }, 100);

    return () => {
      clearInterval(timer);
    };
  }, []);

  return <Text color="green">{counter} tests passed</Text>;
};

render(<Counter />);
Enter fullscreen mode Exit fullscreen mode

And run with deno run -A main.tsx:

Spinner showing X tests passed

But that's not all! How about something like this?

Counter with background

And the code for it is:

import React, { useState, useEffect } from "npm:react";
import { render, Text, Box } from "npm:ink";

const Counter = () => {
  const [counter, setCounter] = useState<number>(0);

  useEffect(() => {
    const timer = setInterval(() => {
      setCounter((previousCounter: number) => previousCounter + 1);
    }, 100);

    return () => {
      clearInterval(timer);
    };
  }, []);

  return (
    <Box borderStyle="double" margin={2} padding={2}>
      <Text backgroundColor="green">{counter}</Text> 
      <Text> tests passed</Text>
    </Box>
  );
};

render(<Counter />);
Enter fullscreen mode Exit fullscreen mode

Good luck with your next command line interface app!

Liked the content and would love to have more of it all year long?

Buy Me A Coffee

Image of Timescale

๐Ÿš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsโ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post โ†’

Top comments (1)

Collapse
 
pengeszikra profile image
Peter Vivo โ€ข

Good to know this way still exists.

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay