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

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.

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

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.