Git Product home page Git Product logo

simple-console's Introduction

Simple Console

Simple Console is nice clean command-line interface for the web. Check out the demo!

Features

  • Light and dark styles

  • Easy rich HTML output

  • Command history accessible with up/down arrow keys, saved to localStorage

  • Command history menu for mobile accessibility

  • Doesn't create a duplicate history entry if you re-enter the last command

  • Lets you delete history entries with Shift+Delete

  • Automatically scrolls down to new entries - unless you scroll up (good in case there are a lot of messages being logged and you want to view earlier output!)

  • Includes aria attributes, although the accessibility still needs work!

  • Vanilla JavaScript and CSS with no external dependencies

  • MIT licensed

Limitations

  • Delivered as two separate files (CSS and JS).
    (It'd be nicer if it was a single JS file, and especially if it was a Web Component! Wanna help out?)

  • The CSS uses some generic selectors like error and success that are likely to conflict.

  • The console.log, and console.warn etc. methods only accept a single argument; they don't match the browser console API - which doesn't have a standard specification, by the way, it's just some de facto shared methods between browsers.

Usage

Download simple-console.css and simple-console.js, and include in the <head>:

<link rel="stylesheet" href="simple-console.css">

and anywhere before you use SimpleConsole but probably in the <body>:

<script src="simple-console.js"></script>

Example

var con = new SimpleConsole({
	placeholder: "Enter JavaScript",
	handleCommand: function(command){
		try {
			con.log(eval(command));
		} catch(error) {
			con.error(error);
		}
	},
	autofocus: true, // if the console is to be the primary interface of the page
	storageID: "app-console", // or e.g. "simple-console-#1" or "workspace-1:javascript-console"
});

// add the console to the page
document.body.appendChild(con.element);

// show any uncaught errors (errors may be unrelated to the console usage)
con.handleUncaughtErrors();

See demo.js for a more complete example (altho not that complete).

Page Setup

You should probably include a charset and viewport like in the demo.

You can position the console on the page the same way you might any div.

To make the console take up the entire page, use:

html,
body {
	height: 100%;
	margin: 0;
	display: flex;
	flex: 1;
}

For a Quake-style dropdown console, see the Tilde Dropdown Console Example.

Dark Mode

The dark styles take effect when the console element or any parent contains the class dark.

You could add a theme switcher like so:

var toggleDarkMode = function() {
	if (console.element.classList.contains("dark")) {
		console.element.classList.remove("dark");
	} else {
		console.element.classList.add("dark");
	}
};
var button = console.addButton(toggleDarkMode);
button.textContent = "โ—";
button.setAttribute("title", "Toggle dark theme");
button.setAttribute("aria-label", "Toggle dark theme");

API

new SimpleConsole(options)

Creates a console instance.

Note: The SimpleConsole object is referred to as console below, but you should probably give it a different name so it doesn't conflict with the global console object.

options.handleCommand(command) is called when the user hits Enter. You can handle the input however you want. It's recommended that you catch errors and log them with console.error. Other logging methods are documented below.

options.outputOnly specifies that there should be no input. You must specify either outputOnly or handleCommand.

options.placeholder is strongly recommended especially with the default input styling as there is very little visual indication of the input (when it's not focused).

options.autofocus should be used within an application where the console is the primary interface.

options.storageID should be used to separate the command history of different consoles. It's used as a localStorage key prefix.

console.element

You must use this to add the console to the page, e.g. document.body.appendChild(console.element)

console.input

The console's <input> element. Can be used to add controls/widgets i.e. console.input.parentElement.appendChild(widget)

console.addButton(action)

Adds a button to the right of the console's input area and returns the button element.

action should be a function.

console.addPopupButton(updatePopup)

Adds a button with a popup to the right of the console's input area and returns the button element.

updatePopup(popupElement) should update the contents of the popup.

Use addPopupMenuButton instead if the popup's contents are a standard menu.

console.addPopupMenuButton(getItems)

Adds a button with a standard popup menu to the right of the console's input area and returns the button element.

getItems() should return an array of items, with each item either of the form {label, action} or {type: "divider"}.

console.handleUncaughtErrors()

Sets up a window.onerror event listener which logs any uncaught errors to the console.

console.log(content)

Logs the given text or element to the console.

console.logHTML(html)

Logs the given HTML to the console.

console.error(content)

Logs the given error message (or element) to the console.

console.warn(content)

Logs the given warning message (or element) to the console.

console.info(content)

Logs the given info message (or element) to the console.

console.success(content)

Logs the given success message (or element) to the console.

console.getLastEntry()

Returns the last logged entry as an HTMLDivElement for further manipulation.

console.clear()

Clears the console.

TODO

  • Support multiple arguments to log, warn etc.

  • Distinguish error/success/warning messages for screen readers (maybe with cue-before from the CSS Speech Module)

  • Solarized and retro themes

  • Position menus better?

Packaging

  • Rename project because "simple-console" is taken on npm ("cute-console" maybe?)

  • This seems like it would be an ideal candidate for a Web Component!

Input

  • Multiline input (i.e. textarea)

  • Autocomplete (aria-autocomplete="inline")

  • Syntax highlighting

  • Should probably just let you use your own input component

FIXME

  • Fix duplicate reading of aria-label and placeholder by some screen readers

  • Fix input styling in Firefox with font: inherit and something else to make stuff line up perfectly

License

MIT-licensed, see LICENSE

simple-console's People

Contributors

1j01 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

simple-console's Issues

Avoid potential CSS selector conflict

The CSS uses some generic selectors like error and success that are likely to conflict.

Possible solutions:

Scoping selectors to .simple-console * (as opposed to prefixing the classes) is not an option, at least for .dark and .light, which are user-applied classes which should work applying them outside of .simple-console; and those are also part of the API currently, whereas other selectors are not, so maybe they should stay unprefixed.

[Feature request] hide input

Hi,

It would be cool to have the ability to hide the input so that it just acts as an output console. I used your library and hid the input with css, it does the job but a function/option in the API to do so would be really cool.

[Feature request]

Hi,

I wished there was a success function beside log/warn/error, similar to warn and error but with a green background.

Thanks for the library it does a nice job.

Console messages have no minimum height

Messages should have min-height of probably 1em, so that if you enter just whitespace they don't get squished up and mess up the layout.

Noticing this with Ooplie's console. Less noticeable with the simple-console demo because it outputs "undefined" in that case.

Make it a web component

I don't have experience with web components, but I think this project should be an ideal candidate for being a web component.

I effing love this!

Today I wrote a super simple console logger that embeds in my webapp (not yet published) because I got tired of making calls to the real console and opening it. Sometimes you just need the very basics. I call it microconsole. If you like the name you are welcome to it. It's really just an output box with very limited functionality. Then a thought came to me. I wonder if others have thought of this so I did a google search and there was surprising number of them out there.

More on my microconsole. It only accepts inputs via a function. The input function which I call microlog(...args) takes full advantage of functional programming with rest/spread operators. That is really helpful. Anyway I just wanted to say that I may embed your console in my page or at the very least examine it to see how the gears turn. Good luck I will definitely bookmark this.

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.