Code Monkey home page Code Monkey logo

termynal's Introduction

termynal.js: A lightweight and modern animated terminal window

Typing animations are nothing new and Termynal isn't particularly revolutionary. I wrote it because I needed a modern and lightweight version with minimal JavaScript and without messy, nested setTimeout calls. Most of the existing libraries rely on JavaScript for both the rendering, styling and animation, or even require jQuery. This is inconvenient, especially if you're using the animation as part of your software's documentation. If a user has JavaScript disabled, they will only see a blank window.

Termynal uses async and await, which is now supported pretty much across all major browsers. Termynal lets you write all input and output in plain HTML, and all styling in plain CSS. Non-JS users will still see the complete code, just no animation. The width and height of the terminal window is read off the original container. This means you won't have to worry about sizing or layout reflows. Termynal also comes with a flexible HTML API, so you can use it without having to write a single line of JavaScript yourself.

termynal

termynal2

Examples

Usage

First, you need to create a container. Each container should have a unique class or ID that tells Termynal where to find the lines to animate. Terminal will find the lines via their data-ty attribute and will then animate their text content. Apart from that, it won't mess with your markup – so you're free to add additional styling and attributes.

<div id="termynal" data-termynal>
    <span data-ty="input">pip install spaCy</span>
    <span data-ty="progress"></span>
    <span data-ty>Successfully installed spacy</span>
</div>

When you include termynal.js, you can specify the container(s) as the data-termynal-container attribute. To initialise Termynal for more than one container, simply add the selectors separated by a |, for example #termynal1|#termynal2.

<script src="termynal.js" data-termynal-container="#termynal"></script>

You also need to include the stylesheet, termynal.css in your site's <head>:

<link rel="stylesheet" href="termynal.css">

That's it!

Customising Termynal

On each container, you can specify a number of settings as data attributes, and overwrite the animation delay after a line on each individual element.

<div id="termynal" data-ty-startDelay="600" data-ty-cursor="">
    <span data-ty="input"> pip install spacy</span>
    <span data-ty data-ty-delay="250">Installing spaCy...</span>
</div>

If you don't want to use the HTML API, you can also initialise Termynal in JavaScript. The constructor takes two arguments: the query selector of the container, and an optional object of settings.

var termynal = new Termynal('#termynal', { startDelay: 600 })

The following settings are available:

Name Type Default Description
prefix string ty Prefix to use for data attributes.
startDelay number 600 Delay before animation, in ms.
typeDelay number 90 Delay between each typed character, in ms.
lineDelay number 1500 Delay between each line, in ms.
progressLength number 40 Number of characters displayed as progress bar.
progressChar string '█' Character to use for progress bar.
cursor string '▋' Character to use for cursor.
noInit boolean false Don't initialise the animation on load. This means you can call Termynal.init() yourself whenever and however you want.
lineData Object[] null Dynamically load lines at instantiation.

Prompts and animations

Each <span> within the container represents a line of code. You can customise the way it's rendered and animated via its data attributes. To be rendered by Termynal, each line needs at least an empty data-ty attribute.

data-ty: display and animation style

Value Description Example
- Simple output, no typing. <span data-ty>Successfuly installed spacy</span>
input Simple prompt with user input and cursor <span data-ty="input">pip install spacy</span>
progress Animated progress bar <span data-ty="progress"></span>

data-ty-prompt: prompt style

The prompt style specifies the characters that are displayed before each line, for example, to indicate command line inputs or interpreters (like >>> for Python). By default, Termynal displays a $ before each user input line.

Attributes Output
data-ty="input" $ hello world
data-ty="input" data-ty-prompt="~" ~ hello world
data-ty="input" data-ty-prompt=">>>" >>> hello world
data-ty="input" data-ty-prompt=">" > hello world
data-ty="input" data-ty-prompt="▲" ▲ hello world
data-ty="input" data-ty-prompt="(.env)" (.env) hello world
data-ty="input" data-ty-prompt="~/user >" ~/user > hello world

You can also use custom prompts for non-animated output.

To make prompts easy to customise and style, they are defined as :before pseudo-elements. Pseudo-elements are not selectable, so the user can copy-paste the commands and won't have to worry about stray $ or >>> characters.

You can change the style by customising the elements in termynal.css, or add your own rules for specific elements only.

/* Default style of prompts */
[data-ty="input"]:before,
[data-ty-prompt]:before {
    margin-right: 0.75em;
    color: var(--color-text-subtle);
}

/* Make only >>> prompt red */
[data-ty-prompt=">>>"]:before {
    color: red;
}

data-ty-progressPercent: set max percent of progress

Attributes Output
data-ty="progress" ████████████████████████████████████████ 100%
data-ty="progress" data-ty-progressPercent="81" █████████████████████████████████ 83%

data-ty-cursor: display a cursor

Each line set to data-ty="input" will be rendered with an animated cursor. Termynal does this by adding a data-ty-cursor attribute, and removing it when the line animation has completed (after the delay specified as lineDelay). The value of the data-ty-cursor sets the cursor style – by default, a small unicode block is used: . You can set a custom cursor character in the global settings, or overwrite it on a particular line:

<div id="#termynal" data-termynal data-ty-cursor="|">
    <span data-ty="input">Animated with cursor |</span>
    <span data-ty="input" data-ty-cursor="">Animated with cursor ▋</span>
</div>

You can also change the cursor style and animation in termynal.css:

[data-ty-cursor]:after {
    content: attr(data-ty-cursor);
    font-family: monospace;
    margin-left: 0.5em;
    -webkit-animation: blink 1s infinite;
            animation: blink 1s infinite;
}

Dynamically loading lines

Lines can be dynamically loaded by passing an array of line data objects, using the attribute suffixes, as a property of the settings object.

var termynal = new Termynal('#termynal',
    {
        lineData: [
            { type: 'input', value: 'pip install spacy' },
            { value: 'Are you sure you want to install \'spaCy\'?' },
            { type: 'input',  typeDelay: 1000, prompt: '(y/n)', value: 'y' },
            { delay: 1000, value: 'Installing spaCy...' }
        ]
    }
)

termynal's People

Contributors

ines avatar patmigliaccio avatar xhdix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

termynal's Issues

Javascript not working in sphinx

I tried your code in making use of the terminal code-block for a documentation using sphinx.

I added termynal.js and termynal.css files _static/js and _static/css resppectively, and included its path to sphinx by editing conf.py file:

html_css_files = [ 'css/termynal.css' ]
html_js_files = [ 'js/termynal.js' ]

CSS is working fine and resulted in : image
but I can find Javascript is not working.

Is there any extension to make Javascript work in sphinx!?

appending new lines

Heya, am trying to append new lines to termynal after initializing, but am only able to restart the whole animation with new lines.

Am using angular, and the js api:

// Looks like this
ngOnInit() {
  this.termynal = new Termynal('#termynal', { ...defaultOptions , lineData });
  this.termynal.init();
  // So far so good...

}

newLogginngInfo(){
// Now i need to be able to append new lines, witouth removing previous lines/ restarting
//Tried few things already, mixing lineData with manually adding lines on the html file with a loop
}

Will digg around myself for a bit, but if have any suggestions, i would appriciate it... 👍

Progress bar doesn't seem to be rendering.

Hey there, I have some html code within a markdown file that uses termynal. I've linked both the JS file and the css files at the top of the markdown file.

My HTML:

<div id="termynal" data-termynal data-ty-typeDelay="40" data-ty-lineDelay="700">
    <span data-ty="input">uvicorn main:py --reload</span>
    <span data-ty="progress"></span>
    <span data-ty><span style="color: green;">INFO:</span>     Started server process [21748]</span>
    <span><span style="color: green;">INFO:</span>     Waiting for application startup.</span>
    <span data-ty><span style="color: green;">INFO:</span>     Application startup complete.</span>
    <span><span style="color: green;">INFO:</span>     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)</span>
</div>

The progress bar doesn't seem to be actually animating at all.
image

how to use in VueJs

hi
i dont understand how to use it my VueJS component
can you explain this procedure?

How can I listen the end of animation ?

Hello everyone ! I have just discovered this incredible JS library but I can't find how can I listen the end of the animation to show another content. Is there any way to do that ?

Feed terminal lines dynamically instead of statically

I think this would actually improve the usability of your library tremendously. I tried inspecting the original code and actually managed to get a manual line feed but the issue is there is no fallback so I cannot insert a new line or add a line in the queue.

Just a mess

Please add exported files, not possible to use this as dependency in a modern system

why there is no replay?

Online source of the script/css

Thanks for this great script.

I am running it on my iOS/iPadOS devices and loving it.

Do you plan to publish the scripts via npm or add them as a online resource?

That would be a great help.

Sincerely
Felix

Display the windows termiynal with full width

The default value is a static value equal to 750px.
Is there a possibility to set dynamically the width value from the directive?

[data-termynal] {
width: 750px;
max-width: 100%;

Syntax Error in termynal.min.js

In the Google Chrome console I see the following error:

Uncaught SyntaxError: Unexpected token 'if'

The error appears near if(g>z){break;}}}} in the minified js file. However, I can't find this if statement anywhere in the non-minified version. It seems there is a mismatch between the two.

Install package via npm

Is it possible to install this package via npm? If not, will this support be added soon? Thank you :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.