How to apply color to text in Node.js

I am in the process of creating a web-based compiler using NodeJS. My main objective is to highlight syntax in different colors. For example, consider the following C code:

int var;
printf("%d",var);

In this code snippet, I would like the "int" and "printf" keywords to be colored differently. So far, I have implemented a feature that turns each letter red as it is typed.

<textarea rows="13" cols="150" id="code" name="code" placeholder="Type your code here" onfocus="if(this.value==''){
          this.value='#include<stdio.h>\n' +
  'int main(){\n' +
  'return 0;\n' +
  '}';
  this.style.color='green';
  }"
   onkeyup="keyup()" onkeypress="pre()" onkeydown="down()" >
 function keyup()
  {
    document.getElementById('code').style.color='green';
  }
  function pre()
  {
    document.getElementById('code').style.color='red';
  }
  function down()
  {
    document.getElementById('code').style.color='green';
  }

I would appreciate any advice on how to assign different colors for standard I/O and data types.

Thank you in advance.

Answer №1

If you're looking to add some color to your text and background, consider using the colors package.

Getting Started

npm install colors --save

How to Use

var colors = require('colors');
console.log('hello'.green); // outputs green text 

Alternatively,

var colors = require('colors/safe');
console.log(colors.green('hello')); // outputs green text 

We hope you find this information helpful.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamically load scripts in angularJs

Having multiple libraries and dependencies in my angularjs application is posing a challenge as I want to load them all using just one script, given that three apps are utilizing these dependencies. Currently, I have this custom script: var initDependenci ...

CreatePortalLink directs users to a payment URL instead of a dashboard

I am currently working on a project that utilizes the Stripe payments extension in conjunction with Firebase. The application is built using Next JS. After a user subscribes, I want to provide them with a tab where they can manage their subscription. The ...

Tips on aligning carousel images and captions next to each other

Would anyone be able to assist me in creating a carousel similar to this? I haven't been able to locate any examples or tutorials that match the design of my desired carousel. Here is the design I am referring to ...

What is the best way to integrate and utilize the jsgrid library in a React project?

I've encountered an issue with the jsgrid library while trying to integrate it into a React project. I followed the instructions on npmjs and included the necessary libraries in the project. https://i.sstatic.net/yfjMH.png Here is my code snippet: ...

Modifying HTML elements with JavaScript - a practical guide

I'm trying to dynamically add the variable x to an existing HTML tag. The goal is to update the image tag <img id="Img" src="IMG/.jpg"/> by appending the variable x at the end of its id and source: <script> var images ...

Express-session causing issue with saving cookies

Currently, I am utilizing express-session to store cookies in my browser for authentication purposes. To achieve this, I am sending a post request from the frontend (built with React) using axios to the backend and then employing User.register to save the ...

Selling beyond webpack's confines

Today I had an interesting thought and couldn't find much information on it, so I decided to share some strange cases and how I personally resolved them (if you have a better solution, please feel free to comment, but in the meantime, this might be he ...

Styling images and text in CSS within a jQuery UI autocomplete widget

I am currently using an autocomplete widget that displays text along with images. The results I have right now are not meeting my requirements. I want to customize the appearance of my results so that the words 'test' and either 'Federico&a ...

Explore the complete route URL in ExpressJS

Is there a way to retrieve the URL of a route in an Express JS application? http://localhost:9310/api/arsys/v1/entry/SimpleForm5/?fields=Request ID,Assigned To,Status,Submitter,Create Date&q=Status=New&offset=5&sort=Create Date.desc I am look ...

Displaying various charts in a single view without the need for scrolling in HTML

How can I display the first chart larger and all subsequent charts together in one window without requiring scrolling? This will eventually be viewed on a larger screen where everything will fit perfectly. Any recommendations on how to achieve this? Here ...

Error: The connect function is not recognized for the mongoose module

Currently, I'm facing an issue while attempting to establish a connection with my MongoDB database. The mongoose module has been imported in the following manner: import mongoose from "mongoose"; Upon trying to connect to the MongoDB databa ...

Disabling multiple textboxes in an array when any one of them has a value entered

Is there a way to automatically disable all text boxes if any one of them has a value? I have an array of cost types and their associated costs. If a cost is entered for any type, all other text boxes for cost types should be disabled. If no cost is ente ...

Conflict in the naming and scoping of IE7 and IE8

While reviewing some code, I encountered a problem in Internet Explorer 7 and 8. Despite the constructor executing properly when stepped through, upon returning I received an error stating "Object does not support this property or method." How frustrating! ...

What are some ways to incorporate texture into objects using three.js?

Having trouble adding texture to my central sphere (earth) without the object disappearing. Can someone provide guidance on where I might be making a mistake? Your help is appreciated. For reference, here is the link to my jsbin http://jsbin.com/cabape/edi ...

Comparison Between Angular and Web API in Regards to Racing Condition with Databases

Currently, I am working on an Angular service that iterates through a list and makes web API calls to add or modify records in the database. The service operates on a small record set with a maximum of 10 records to update. After the loop completes, Angula ...

Tips for collapsing all rows in an HTML table by using the Ctrl+F command

I have a table in HTML with a few rows. Check out the demo. I am looking to expand all collapsed rows when the user hits "Ctrl + F" on the keyboard. Currently, I am using the following code snippet to expand/collapse the necessary rows. $('#resultDe ...

Ways to change object to string in javascript

My object has the following structure: Object {Dry Aged Ribeye(medium): "1" , Dry Aged Ribeye(rare): "1" , Dry Aged Ribeye(well): "1" , favorite_tables: "{"dc76e9f0c0006e8f919e0c515c66dbba3982f785":[]}" } I am trying to insert this into My ...

Encountering issues when trying to access a property after setting an EJS variable in

Whenever this code runs, an interesting type error occurs within the if statement. It says: Cannot read property product.thumbgallery1 of undefined. var urlArray= []; var product = '<%- product %>'; console.dir(product); ...

Ways to Achieve the Following with JavaScript, Cascading Style Sheets, and Hypertext

Can someone help me convert this image into HTML/CSS code? I'm completely lost on how to do this and don't even know where to start searching for answers. Any assistance would be greatly appreciated. Thank you in advance. ...

Are you looking to convert this JSON using underscore or any other JavaScript utility tool?

Looking at this JSON data: [{"target": "mydata.12.2", "datapoints": [[763.7, 1368821100], [762.1, 1368821160], [223.11, 1368821220], [886.54, 1368821280], [112.3, 1368821340]]}] I need to strip the first key and square brackets so it appears like this: ...