npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-terminal-kit

v0.2.0

Published

The React Terminal Kit package provides a terminal-like user interface for React applications. It allows you to integrate a fully functional terminal component into your React project, enabling users to interact with the application through a command-line

Downloads

30

Readme

React Terminal Kit

npm version Build Status License: MIT


Overview

The React Terminal Kit package provides a terminal-like user interface for React applications. It allows you to integrate a fully functional terminal component into your React project, enabling users to interact with the application through a command-line interface.

Installation

To install the React Terminal Kit package, use npm or yarn:

npm install react-terminal-kit --save
# or
yarn add react-terminal-kit

Usage

  1. Import the Terminal component into your React component.

    import React from "react";
    import Terminal, {
      TerminalCommands,
      TerminalUtils,
    } from "react-terminal-kit";
  2. Add the Terminal component to your JSX, providing the necessary props.

    const commands: TerminalCommands = {
      echo: {
        description: "Print the provided text",
        callback: (utils: TerminalUtils, args: string[]) => {
          utils.displayOutput(args.join(" "));
        },
      },
    };
    
    const MyComponent = () => {
      return (
        <div style={{ width: "500px", height: "300px" }}>
          {/* Your other components */}
          <Terminal
            prefix="root"
            prompt="$"
            cursor="block"
            theme="dark"
            commands={commands}
          />
        </div>
      );
    };

Note: The Terminal component, by default, takes 100% width and height. To control the dimensions, wrap it inside a div element with a specified width and height.

This ensures that the terminal is displayed within the specified dimensions and fits seamlessly into your application layout. If you have specific layout instructions or considerations, you can further customize this section based on your needs.

Try it on CodeSandbox Edit react-terminal-kit-usage

Terminal Props

| Property | Type | Default Value | Description | | ---------------- | ----------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------ | | prefix | string | "" | A string representing the prefix displayed before the prompt. Defaults to an empty string. | | prompt | string | $ | A string representing the prompt displayed at the beginning of each command line. Defaults to $. | | cursor | string | 'block' | A string representing the cursor type. Options include 'block', 'underline', and 'bar'. Defaults to 'block'. | | theme | string | 'dark' | A string representing the color theme. Options include 'light' and 'dark'. Defaults to 'dark'. | | welcomeMessage | ReactNode or string | undefined | Content to be displayed at the top whenever the component is mounted. Can be a string or a ReactNode. Defaults to undefined. | | commands | object | (Required) | An object where keys are command triggers and values are objects containing description and callback properties. |

commands Object Properties

  • description: A short description of the command.
  • callback: A function to be executed when the command is entered. It receives two parameters: utils and args.

Default Commands

In addition to customizing commands using the commands prop, React Terminal Kit comes with a set of default commands that serve fundamental purposes. These default commands are included out of the box and should not be overwritten in your custom configurations.

| Command | Description | Callback Function | | ------- | ---------------------------- | ------------------------------------------------------ | | help | List all available commands. | Displays a list of available commands in the terminal. | | clear | Clear the terminal. | Clears the terminal screen. |

These default commands provide essential functionalities and are designed to work seamlessly with the React Terminal Kit package. Avoid overwriting these commands to ensure proper operation of the terminal component.


Utility Functions

The utils parameter passed to the command callbacks contains the following utility functions:

  1. displayOutput(args: DisplayOutputArgs): Displays text in the terminal output.

  2. clearScreen(): Clears the terminal screen.

  3. input(prompt: string, options: InputOptions): Promise<string>: Awaits user input.

  4. password(prompt: string, options: PasswordOptions): Promise<string>: Awaits user input with masking (password).

  5. confirm(prompt: string, options: BooleanOptions): Promise<boolean>: Awaits user choice (Yes/No).

  6. select(prompt: string, options: SelectOptions): Promise<number>: Awaits user choice from a list of options and returns the index of chosen option.

  7. openFullScreen(path: string): Opens a full-screen output with a specified path.

  8. closeFullScreen(): Closes the currently open full-screen output.

  9. startLoading(options?: LoadingProps): Promise<void>: Initiates a loading indicator with an optional text and type.

  10. stopLoading(args?: StopLoadingArgs): Stops the ongoing loading indicator. The args parameter is optional and can include additional options for stopping the loading indicator.

DisplayOutputArgs | InputOptions | PasswordOptions | BooleanOptions | SelectOptions | FullScreenOutput | Text | LoadingProps | StopLoadingArgs

DisplayOutputArgs

Represents the arguments for the displayOutput function, allowing developers to customize the content and styling of a single line in the terminal window.

  • Each displayOutput() call will only print 1 line to the terminal window:

    • Ensures that each call focuses on presenting a distinct piece of information.
  • The array of DisplayOutputArgs is used to style different sections of a single line:

    • Allows for granular control over the appearance of each part of the displayed content.
  • The dev can pass in a ReactNode as an input as well, and that will be displayed as-is on the window:

    • Provides flexibility to pass a ReactNode as content, seamlessly integrating with React components. A <Text/> component is available for this use case to match terminal styles seamlessly.
  • If a normal string is passed, it'll be displayed with default styling:

    • Enables quick display of plain text without specifying additional styling options.

InputOptions

Represents the options for the input function, allowing developers to customize the behavior of the input function. | Property | Type | Default | Description | | -------------- | --------------------- | --------------------- | -------------------------------------------------------------------------------- | | allowEmpty | boolean (optional) | true | Allow empty input. If set to true, the user can submit an empty input. | | maxRetries | number (optional) | 3 | Maximum number of retries allowed. If set to 0, retries are infinite, and allowEmpty must be false. | | retryMessage | string (optional) | NA | Message displayed on retry. | | errorMessage | string (optional) | NA | Message displayed on error. | | validator | (input: string) => boolean (optional) |NA | Validator function to check the validity of the input. Should return true if valid, false otherwise. |

PasswordOptions

Represents the options for the password function, allowing developers to customize the behavior of the password function.

| Property | Type | Default | Description | | ------------------------------ | -------------------- | ------- | ------------------------------------------------------------- | | mask | boolean (optional) | true | Mask the user's input with asterisks (*) to keep it private. | | Inherits from InputOptions |

BooleanOptions

Represents the options for the choice function, allowing developers to customize the behavior of the choice function.

| Property | Type | Default | Description | | --------- | -------------------- | ------------- | ----------------------------------------- | | true | string | Yes | Text to display for a true confirmation. | | false | string | No | Text to display for a false confirmation. | | default | boolean (optional) | Not specified | Default selection |

SelectOptions

Represents the options for the select function, allowing developers to customize the behavior of the select function.

| Property | Type | Default | Description | | --------- | ------------------- | ------------- | -------------------------------- | | options | string[] | Not specified | Array of options to choose from. | | default | number (optional) | Not specified | Default selection index |

LoadingProps

Represents the options for the startLoading function, allowing developers to customize the behavior of the loading indicator.

| Property | Type | Default Value | Description | | -------- | ----------------------------- | -------------- | --------------------------------------------------------- | | text | string | "Loading..." | The text to be displayed alongside the loading indicator. | | type | LoadingType | "bar" | The type of loading indicator to be displayed. |

LoadingType

| Type | Symbols | | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | bar | ["\|", "/", "-", "\"] | | bubbles | ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"] | | breathe | [" () ", " ( ) ", "( )", " ( ) "] | | metro | ["[ ]", "[= ]", "[== ]", "[=== ]", "[ ===]", "[ ==]", "[ =]"] | | modern-metro | ["[▱▱▱▱▱▱▱]", "[▰▱▱▱▱▱▱]", "[▰▰▱▱▱▱▱]", "[▰▰▰▱▱▱▱]", "[▰▰▰▰▱▱▱]", "[▰▰▰▰▰▱▱]", "[▰▰▰▰▰▰▱]", "[▰▰▰▰▰▰▰]", "[▱▰▰▰▰▰▰]", "[▱▱▰▰▰▰▰]", "[▱▱▱▰▰▰▰]", "[▱▱▱▱▰▰▰]", "[▱▱▱▱▱▰▰]", "[▱▱▱▱▱▱▰]"] | | vertical | ["▁", "▃", "▄", "▅", "▆", "▇", "▆", "▅", "▄", "▃"] | | horizontal | ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "▊", "▋", "▌", "▍"] | | semi-circle | ["◐", "◓", "◑", "◒"] | | arrow | ["▹▹▹▹▹", "▸▹▹▹▹", "▹▸▹▹▹", "▹▹▸▹▹", "▹▹▹▸▹", "▹▹▹▹▸"] | | clock | ["🕛", "🕐", "🕑", "🕒", "🕓", "🕔", "🕕", "🕖", "🕗", "🕘", "🕙", "🕚"] | | bounce | ["(● )", "( ● )", "( ● )", "( ● )", "( ● )", "( ●)", "( ● )", "( ● )", "( ● )", "( ● )"] | | firework | ["⢀", "⠠", "⠐", "⠈", "*", "*", " "] |

For a more extensive collection of loading animations, you can explore the bash_loading_animations repository on GitHub.

StopLoadingArgs

Represents the options for the stopLoading function, allowing developers to customize the output after stopping the loading indicator.

| Property | Type | Default Value | Description | | --------- | -------- | ------------------------------ | ------------------------------------------------------- | | message | string | <text> succeeded \| failed | The message to be displayed after stopping the loading. | | status | string | NA | The exit status of the loading indicator. |


Available Components

FullScreenOutput

The FullScreenOutput component is designed to display full-screen content within the terminal. It provides a way to present extensive information or interactive content in a dedicated full-screen view.

FullScreenOutput Props

| Property | Type | Default | Description | | ---------------- | ----------------- | ------------- | ------------------------------------------------------------------------- | | unmountOnExit | boolean | false | If set to true, the component will unmount when exiting full-screen. | | path(required) | string | Not specified | A string representing the path or identifier for the full-screen content. | | children | React.ReactNode | Not specified | The content to be displayed within the full-screen output. |

Note: Ensure that you use the FullScreenOutput component directly inside the Terminal component and pass any custom components as children to it. The Terminal component will only render the FullScreenOutput component if it is a direct child.

Example:

import Terminal, { FullScreenOutput } from "react-terminal-kit";

<Terminal prefix="root" prompt="$" cursor="block" theme="dark">
  <FullScreenOutput path="example-path" unmountOnExit>
    {/* Your custom content here */}
  </FullScreenOutput>
</Terminal>;

Text

The Text component allows you to display plain text within the terminal. It is useful for rendering simple textual content with terminal styling.

Text Props

| Property | Type | Default | Description | | ----------- | -------------------------------- | ------------- | ------------------------------------------------------------------- | | variant | OutputVariant | Not specified | The text variant, defining the styling of the text. | | color | OutputColor | Not specified | The color variant, defining the color of the text. | | children | React.ReactNode | Not specified | The content to be displayed within the Text component. | | id | string (optional) | Not specified | The HTML id attribute for additional styling or targeting. | | className | string (optional) | Not specified | Additional CSS classes to apply to the Text component. | | href | string (optional) | Not specified | If provided, the Text component will render as an anchor (<a>). | | style | React.CSSProperties (optional) | Not specified | Inline styles to apply to the Text component. |

Example:

import Terminal, {
  Text,
  TerminalCommands,
  TerminalUtils,
} from "react-terminal-kit";

const commands: TerminalCommands = {
  echo: {
    description: "Print the provided text",
    callback: (utils: TerminalUtils, args: string[]) => {
      utils.displayOutput(<Text>{args.join(" ")}</Text>);
    },
  },
};

const MyComponent = () => {
  return (
    <div style={{ width: "500px", height: "300px" }}>
      {/* Your other components */}
      <Terminal
        prefix="root"
        prompt="$"
        cursor="block"
        theme="dark"
        commands={commands}
      />
    </div>
  );
};

Issues

If you encounter any issues or have feature requests, please open an issue on the GitHub repository:

License

The React Terminal Kit package is released under the MIT License. See the full license text here:

Author

Thank you for using React Terminal Kit! This package is maintained by Siddharth Mittal.

Contact

If you have any questions, feedback, or if you encounter issues, feel free to reach out. Your input is valuable, and we appreciate your interest in React Terminal Kit.

Happy coding!