Creating a scrollable/rewritable terminal/stdout interface with Node.js - javascript

When I print to my terminal in Node.js, I use console.log to output a new line of text. However, there are some applications that dynamically update the same lines of console output, instead of just adding new ones. A popular example of this is the ASCII loading bar that appears in wget or scp.
There are some applications that fill the entire terminal window with dynamic output. ngrok is an example of this:
This application updates the information above dynamically.
Are there frameworks that will help me create a similar dynamic output in Node.js? Does this have a name?

Well maybe this is too much for what you need.. but I'm going to give you some options of tools that you can use..
A better option to just use a console.log It would be using colors instead:
https://www.npmjs.com/package/color
https://www.npmjs.com/package/chalk
If you need prompt questions to the user:
https://www.npmjs.com/package/inquirer
For create command lines, you have:
https://www.npmjs.com/package/commander
For terminal interfaces you have these:
https://github.com/chjj/blessed
Then on the top of that one you have this one:
https://github.com/Yomguithereal/react-blessed
Those are amazing tools.. I'm using them for my project and they just rock. There are some others.. tell if you need more hehe

You can try this module: ansi.js

Related

How to get the current foreground application in electron(Javascript)

How can I detect if, for example, a browser is currently open?
I need this in my electron-application.
ty :)
I've found nothing like this online.
I've only found how I can check which window is open from the windows I have in my own application, but I need to know, what else is opened.
It should be something like this:
if(Application.isOpen('Google Chrome'){}
Unless someone has built a specific electron api to do this (which I can't find), then from electron...no. However, the beauty of electron being built with node.js, means that any node module should be able to do the job for you.
For example, ps-list should be able to get you all currently running processes.
psList().then(processes => {
console.log(processes)
})
Which gives a list for me, including:
Just be aware that you need node access from the electron thread attempting to use this lib.
This can easily be abstracted to do a name search in the list for you to get your desired functionality.
You can use find-process in case you need to search by given name, name pattern or pid.

Postman format console log output

I'm using the open source version of Postman
I have a console log output in the Pre-Request script for each of my API calls, which just outputs the title of the call.
Is there a way of formatting the console log output in postman to make it stand out in the console log? I want to either enlarge or bold the text or make it a different colour so it is clear in the console log.
It doesn't seem to be possible, though console output gets some different colors for error codes or strings.
javascript string formatting doesn't work either. You should output some visible strings like you would do in normal consoles like:
console.log("******************************");
console.log("********* MY TEST ********");
console.log("******************************");
sorry, can't see a better way
Alexandre
In node.js there is by using escape sequence:
console.log('\x1b[36m%s\x1b[34m%s\x1b[0m', 'I am cyan','i am blue');
So you should be able to if running Newman as a library within Node. And there is a way to provide colors for browser based console.log; however, not in the desktop application. I too would much appreciate some simple formatting to help my log events to stand out from the rest. But I have resorted to using something similar to a-joly's response above
console.log("******************************");
console.log("********* MY TEST ********");
console.log("******************************");
to help with this issue.
There are other formatting styles capable within Node's console.log as well, they can be viewed here:

What is the best parctice for debugging production javascript?

I have a Javascript SPA app, based on React, that it is about to go live.
In the app itself I log all Javascript exceptions and save them on the server for further debugging.
As my app will be minified I was wondering how I am to debug the stack traces I will get when a bug is hit.
I came across stacktracejs which looks promising, but the documentation looks a bit thin. So I was wondering if there is something better out there.
Just to clarify, coming from C world myself, I am essentially asking what is the equivalent to "GDB", where I can load the core a binary on it and start debugging.
You could use a library like source-map (If you can run nodejs on your server).
There you would load your source-map for the given file:
var smc = new SourceMapConsumer(rawSourceMap);
Then you would need to parse your stack trace extracting all line and column numbers. Those information you then can use to retrieve the original position.
console.log(smc.originalPositionFor({
line: 1,
column: 2
}));

Creating terminal help text in JS

I am using Gulp to create a bunch of tasks for a project I have and when you type gulp I would like to show some instructions on the terminal to show people the commands they can Run and what they do.
I didn't want to use console.log because it blends in and I wanted to give bolds and styles to the lettering.
I was searching for a way to do that but I couldn't find any that worked properly, does anyone know?
Examples of people that have this is Yeoman and Foundation for Apps CLI
If you need to avoid using console.log you can use the underlying standard output, accessible in node through process.stdout
https://nodejs.org/api/process.html#process_process_stdout
The example provided in that link is the actual definition of console.log in node:
console.log = function(d) {
process.stdout.write(d + '\n');
};
For colouring and styling your strings you could use cli-color or chalk.
you can either use gulp-help, which gives you ability to provided even details to be printed for a given task... or use gulp-task-linking which prints tasks as main tasks & sub-tasks
visit the link to know all the options that they provide...

How would I solve a coding puzzle with Javascript?

There is a website called Gild.com that has different coding puzzles/challenges for users to do. They can be completed in wide array of languages including Javascript. I am interested in solving these puzzles in Javascript, but I am unsure of the following:
How am I supposed to access the input file which is supposed to be passed as an argument?
How am I supposed to output the result?
My understanding of Javascript is that it is run from within an HTML page and that output really is only in the form of placing values in the HTML, modifying the DOM, etc. For that reason it is not clear to me how Javascript can be used for solving these types of problems. Can someone who has used Gild before or has some insights into my question suggest how to proceed?
An example of a problem would be: the given input file contains a positive integer, find the sum of all prime numbers smaller than that integer and output it.
EDIT: Some of the solutions below involve using external resources, but on Gild, I am supposed to put my solution in their editor and then submit it that way, like the following picture shows:
In other words, I don't think my solution can have access to Node.js or other external resources.
Edit: Here are some interesting articles that I have found that I think are the answer to my question:
http://www.phpied.com/installing-rhino-on-mac/
http://www.phpied.com/javascript-shell-scripting/
I haven't spent much time on Gild, but I do a lot of similar types of problems on Project Euler. I think the best way to go is probably Node.js.
If you're not familiar, Node is basically server-side JavaScript that runs in Google's V8 engine. Installing it on your own Mac/Windows machine takes about 2 minutes. It's also really fast (considering it's JavaScript).
And you'd use it like this:
var fs = require('fs'); // the filesystem module
var contents = fs.readFileSync('theFile.txt', 'utf-8');
// Do stuff with the file contents...
Everything after those first two lines can be done with the same JS you'd write in the browser, right down to calling console.log() to spit out the answer.
So, if you wrote your script in a file on your desktop called getprimes.js, you'd open up your terminal and enter node ~/Desktop/getprimes.js (assuming you're on a Mac)
If you're:
on a Mac,
planning to do a lot of these puzzles, and
willing to pay $10, then
I highly recommend CodeRunner. It encapsulates runtimes for a variety of languages — from C to JavaScript — and lets you quickly build and run any sort of one-off code. Just hack together your code, ⌘R, and the results are printed right there in the same window.
I haven't used any file-based JavaScript in CodeRunner, but I imagine kennis's suggestions would apply. To output your results:
console.log(...)
Easy as pie!

Categories