Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
SuperSamir

Differently colored text in the same line

Question

I've figured out how to change the color of the text, however I would like to make it so that there are multiple different colors in the same line. For example, instead of "Hello world!" being just blue, it could have "Hello" be blue and "world!" be red.

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0

I assume you are currently using the term lib for writing stuff on the screen?

For full control over the screen, you need to interact with the gnu component directly. You then need to also specify the x and y coordinate where your text starts.

But it's easy to write a function that works exactly like term.write but allows you to specify the colors of each part of the string.

local component = require "component"
local term = require "term"
local unicode = require "unicode"

local gpu = component.gpu

local function advancedWrite(s, c) -- s is the string, c is its color
  local x, y = term.getCursor()
  local max_x, max_y = gpu.getResolution
  local len = unicode.len(s)
  local old_fg = gpu.setForeground(c)
  gpu.set(x, y, s)
  gpu.setForeground(old_fg)
  if x + len >= max_x then
    x = 1
    y = y + 1
  else
    x = x + len
  end
  term.setCursor(x, y)
  if x + len >= max_x then
    return advancedWrite(unicode.sub(s, max_x-x), c)
  end
end

This is a really simple and inefficient approach. I haven't tested it yet but I will once I'm back at my computer. I will also post a proper function later, if that is the type of solution you're looking for.

Share this post


Link to post
Share on other sites
  • 0

Ok, as promised, here is a more advanced function.

Instead of giving it one string and specify its color, you mark parts of the string and specify foreground (and also background) color by using a xml/html like syntax.

If you want to change on part of the string, put it between <color fg=hex, bg=hex>your string goes here</color>.

Hex is the color code starting with 0x (like 0xFFFFFF).

You don't have to specify both, background and foreground, just set the color you like to change; if you set both, use a comma (,) as delimiter.

local component = require "component"
local term = require "term"
-------------------------------------------------------------------------------
local gpu = component.gpu

local adWrite = {}

function adWrite.write(s)
  local cbg, cfg = gpu.getBackground(), gpu.getForeground()
  local i = 1
  s:gsub('()<color(.-)>(.-)</color>()', function(i1, tag, content, i2)
    local bg, fg
    if not i1 then return end
    term.write(s:sub(i, i1-1))
    tag = tag:gsub('%s', "")  -- remove whites paces
    for colortype, color in tag:gmatch '([bfg][bfg])=(0[xX]%x+)' do
      if colortype == "bg" and color then
        bg = tonumber(color)
      elseif colortype == "fg" and color then
        fg = tonumber(color)
      end
    end
    if bg and bg ~= cbg then
      gpu.setBackground(bg)
    end
    if fg and fg ~= color then
      gpu.setForeground(fg)
    end
    term.write(content)
    gpu.setBackground(cbg)
    gpu.setForeground(cfg)
    i = i2
  end)
  if i <= #s then
    term.write(s:sub(i, #s))
  end
end

-------------------------------------------------------------------------------
return adWrite

Here is an example:

 

screenshot.png

Share this post


Link to post
Share on other sites
  • 0
On 8/23/2019 at 12:25 AM, payonel said:

you can also use vt100 codes for color


io.write("\27[31m", "hello ", "\27[33m", "world", "\27[m")

 

Does this method also allow using gray as a color?

Share this post


Link to post
Share on other sites
  • 0

go ahead and read about vt100 codes. openos supports the 8 color scheme (8 for background, 8 for foreground), i.e. codes 30 through 37. 37 is gray (and 47 for background)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.