This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darcline's profile picture

text running

Started by darcline, 24 November 2012 - 04:54 AM
darcline #1
Posted 24 November 2012 - 05:54 AM
hello @ all i have an little problem :/

can everyone write me a programm that does a text in a monitor center and then take the textposition from left to right and this in an loope i hope some can help me -_-/>/> and sry for bad english ;)/>/>
Cruor #2
Posted 24 November 2012 - 06:42 AM
Moved to ask a pro as this is a question and not a program. Please read stickies before posting.
remiX #3
Posted 24 November 2012 - 07:00 AM
To center text on the monitor? You would do something like this:

-- To make sure there is a monitor
for num, side in ipairs(rs.getSides()) do
    if peripheral.getType(side) and peripheral.isPresent(side) then
        mon = peripheral.wrap(side)
        break
    end
end
if not mon then
    error("No monitor found")
end

-- Main part
w, h = mon.getSize()

function monWriteCen(y, text)
    l = #text
    mon.setCursorPos((w-l)/2,y)
    mon.write(text)
end

monWriteCen(3, "This is centered")

This will center the text on line 3 of the monitor.
darcline #4
Posted 24 November 2012 - 07:48 AM
ok thanks and how i can let run the text from left to right side ?
Espen #5
Posted 24 November 2012 - 08:19 AM
ok thanks and how i can let run the text from left to right side ?

Here's an example for how to make text scroll from left to right:
Spoiler
-- Writes text centered, x- and y-axis position can be offset, and the line to be written can be cleared beforehand.
local function writeCentered( _sText, _nOffsetX, _nOffsetY, _nColor, _bClearLine )
    local maxX, maxY = term.getSize()

    if _nOffsetX == null then _nOffsetX = 0 end
    local centeredX = (maxX / 2) - (string.len( _sText ) / 2) + _nOffsetX

    if _nOffsetY == null then _nOffsetY = 0 end
    local centeredY = (maxY / 2) + _nOffsetY

    term.setCursorPos( centeredX, centeredY )
    if _bClearLine then term.clearLine() end
    if _nColor and term.isColor() then term.setTextColor( _nColor ) end
    write( _sText )
    if _nColor and term.isColor() then term.setTextColor( colors.white ) end
end

-- Scrolls text form left to right.
--  _text  = Text to scroll
--  _dleay = Scroll-Delay in seconds (optional; defaults to 0.1)
--  _yPos  = Y-Position to display marquee (optional; defaults to center)
local function marquee( _text, _delay, _yPos )
    local nScrollTime = _delay or 0.1 -- Scroll-Time in seconds
    local counter = 0
    local sMarquee = _text
    local xMax, yMax = term.getSize()
    yPos = _yPos or (yMax / 2)

    local scrollTimer = os.startTimer( nScrollTime )
    while true do
        local sEvent, sPar = os.pullEvent()

        if sEvent == "char" and string.lower(sPar) == "q" then
            print()
            break
        end

        if sEvent == "timer" and sPar == scrollTimer then
            -- If the marquee has vanished completely into the right side, reset everything.
            if counter > (xMax + string.len(_text)) then
                counter = string.len(_text) + 1
                sMarquee = _text
            end

            -- (Re-)Set the cursor to the beginning of the line.
            term.setCursorPos(1, yPos)

            -- Pad left side with whitespaces if _text has been completely scrolled out from the left side.
            if counter >= string.len(_text) then
                sMarquee = " "..sMarquee
            end

            -- Wrap marquee text around if it reaches the end of the screen.
            if string.len(sMarquee) > xMax then
                sMarquee = string.sub(sMarquee, -1) .. string.sub(sMarquee, 2, string.len(sMarquee) - 1)
            end

            -- Write the marquee.
            if counter ~= 0 then
                write( string.sub(sMarquee, 0 - counter) )
            end

            -- Reset counter.
            counter = counter + 1
            scrollTimer = os.startTimer( nScrollTime )
        end
    end
end

term.clear()
marquee( "Press Q to quit." )

It's pretty limited though and I believe it can be done in a much more streamlined or at least more elegant way, but I think it's a good starting point and might give you some ideas.

Cheers ;)/>/>
darcline #6
Posted 24 November 2012 - 08:49 AM
hm thats bad :/ i have an error he says in line 21 is missing de code <eof> whats this ?
Espen #7
Posted 24 November 2012 - 09:04 AM
hm thats bad :/ i have an error he says in line 21 is missing de code <eof> whats this ?
eof means "end of file".

But about who's code are you talking, remiX's or mine?^^
darcline #8
Posted 24 November 2012 - 09:07 AM
i used yours not remixs
Espen #9
Posted 24 November 2012 - 10:19 AM
i used yours not remixs
Hmm, then I don't know what's wrong. Just copied and pasted exactly what I posted to try and see if something got messed up while posting.
But everything works fine for me. Did you copy and paste everything? Also what CC version are you using, if you don't mind my asking?
remiX #10
Posted 24 November 2012 - 10:25 AM
Oh is that what he meant haha, thought he meant he just wanted to center text ;)/>/> Your code works for me Espen.
darcline #11
Posted 25 November 2012 - 03:03 AM
what i wanted is a script that center a text and scrolls a text form left to right in a loop :D/>/> :P/>/>

ahm i have copy the script exectli 1 to 1 and its running but under the text that is running is standing press when the text is running one times from left to right
Doyle3694 #12
Posted 25 November 2012 - 04:18 AM
how defuq would you center and scroll the text at the same time?
darcline #13
Posted 25 November 2012 - 04:23 AM
also again :D/>/> the text is center and is running from left to right
the only bug is that when its running one again dann is under the running text at the left side from the monitor ist standing the first word again but is not running with the text only the running tetx shoud be watching
Espen #14
Posted 25 November 2012 - 04:43 AM
@darcline:
I really have no idea what you mean, but one thing to keep in mind:
The function I've written does not clear the screen, I do that manually before calling the function because there might be occasions where you don't want a function to overwrtie the whole screen.

So maybe you're calling the function multiple times without a term.clear() in between?
Sorry if I've misunderstood you, but maybe a screenshot of what you mean might help.
Cheers :D/>/>
darcline #15
Posted 25 November 2012 - 06:13 AM
no problemo :D/>/>/&amp;gt; i meant in the screen you see left the monitor and then the text and under the running text is the first word from the runnign text :P/>/>/&amp;gt;

Edited on 25 November 2012 - 05:16 AM
Espen #16
Posted 25 November 2012 - 08:42 AM
Ah, now I understand. Ok I didn't make this with monitors in mind. Gonna have to test this and get back to you.

Edit:
Ok I've done the exact same setup as you have on the screenshot.
That is, a gray computer with three gray monitors horizontally on top of it.
But it still works for me, the marquee scrolls from left to right indefinitely as expected, without any problems.

I even tried it on an advanced computer with advanced monitors, and I even mixed the computer-monitor combo up.
But it still worked as expected, so I have absolutely no idea why it's different for you.^^

Therefore I have some questions:
- Are you typing the program manually, or have you copy'n'pasted it into a Windows/Unix text-editor and saved that in the computer's folder?
- Which ComputerCraft version are you running?
- Are you using ComputerCraft from Tekkit?
Edited on 25 November 2012 - 09:49 AM
darcline #17
Posted 25 November 2012 - 11:40 AM
i have copy and paste, and i run tekkit ver 1.2.5 and the computercraft version from mc 1.2.5
and i have the sides forgotten :D/>/> left to right is false sry :P/>/> right to left is correct i hope this will not make some mistakes
Espen #18
Posted 25 November 2012 - 02:54 PM
Do you mean you've changed the code to go from right to left?
Because if that's the case then I can't say why it doesn't work for your as I'd have to see the changes you made.
If you take the code exactly as I've posted it, then it should work.
If not, then maybe it's something quirky with Tekkit going on? I can't really say as I'm using the official ComputerCraft only.
KaoS #19
Posted 25 November 2012 - 11:17 PM
well I used to use tekkit so lemmi take a quick look and build my scroll function again, I use tables to store the string in mine… be right back with a prototype
KaoS #20
Posted 25 November 2012 - 11:36 PM
here is a basic hash up, tested and functional but not very compact. you are of course welcome to modify it


local function marquee(str,delay)
  local tM={}
  for x=1,term.getSize() do
   tM[x]=' '
  end
  for x=1,#str do
   if #str&amp;gt;#tM then
    tM[x]=string.sub(str,x,x)
   else
    tM[math.floor((#tM-#str)/2)+x-1]=string.sub(str,x,x)
   end
  end
  os.queueEvent('start marquee')
  while os.pullEvent()~='key' do
   term.clear()
   term.setCursorPos(1,1)
   for x=1,term.getSize() do
    write(tM[x])
   end
   local first=tM[1]
   for x=1,#tM do
    tM[x]=tM[x+1] or ' '
   end
   tM[#tM]=first
   os.startTimer(delay)
  end
end
marquee('this is a test message',0.1)
darcline #21
Posted 26 November 2012 - 02:28 AM
no espen your script is running wounderfull but i have not editing anything but can you edit the text running from left to right to right to left i had you say the wrong side sry
Espen #22
Posted 26 November 2012 - 05:24 PM
no espen your script is running wounderfull but i have not editing anything but can you edit the text running from left to right to right to left i had you say the wrong side sry
Oh ok, now I get it. Sorry, honest misunderstanding.^^
I rewrote the function to accomodate both directions. I went a different route this time, i.e. no whitespace padding, but cursor repositioning.
It's still not completely to my liking, but that's as far as I go at 5 in the morning.^^
Regardless of my liking of the code, the scrolling in either direction works fine nontheless (at least in my tests).
The usage is on top of the function.

Spoiler
-- Scrolls text form left-to-right (default) or right-to-left
--   _sText    = Text to scroll                                  (mandatory)
--   _bReverse = true for right-to-left, false for left-to-right (optional; defaults to left-to-right)
--   _nDelay   = Scroll-Delay in seconds                         (optional; defaults to 0.1)
--   _yPos     = Y-Position to display marquee                   (optional; defaults to center)
local function marquee( _sText, _bReverse, _nDelay, _yPos )
  if not _sText then
    error("No text found")
  end

  -- Initialize Variables
  local nScrollTime  = _nDelay or 0.1 -- Scroll-Time in seconds
  local xMax, yMax   = term.getSize()
  local xPos         = 1
  local step         = _bReverse and -1 or 1
  local marquee_main = _sText
  local marquee_rest = ""
                yPos = _yPos or (yMax / 2)

  -- Start timer
  local scrollTimer = os.startTimer( nScrollTime )
  -- Start event loop
  while true do
    local sEvent, sPar = os.pullEvent()

    -- Quit on pressing Q
    if sEvent == "char" and string.lower(sPar) == "q" then
        print()
        break

    -- Wrap marquee text around if it reaches the end of the screen.
    elseif sEvent == "timer" and sPar == scrollTimer then

      -- Wrap for right-to-left
      if not _bReverse and (xPos + #marquee_main > xMax) then
        marquee_rest = string.sub(marquee_main, -1) .. marquee_rest
        marquee_main = string.sub(marquee_main, 1, #marquee_main - 1)
        term.setCursorPos(1, yPos)
        term.clearLine()
        write( marquee_rest )
        term.setCursorPos(xPos, yPos)
        write( marquee_main )

      -- Wrap for left-to-right
      elseif _bReverse and (xPos <= 0) then
        marquee_rest = marquee_rest .. string.sub(marquee_main, 1, 1)
        marquee_main = string.sub(marquee_main, 2)
        term.setCursorPos(1, yPos)
        term.clearLine()
        write( marquee_main )
        term.setCursorPos(xMax - #marquee_rest, yPos)
        write( marquee_rest )
        xPos = 1

      -- Continue scrolling, no wrap-around yet.
      else
        term.setCursorPos(xPos, yPos)
        term.clearLine()
        write( marquee_main )
      end

      -- Reset after a full wrap-around occurs.
      if #marquee_rest >= #_sText then
        marquee_rest = ""
        marquee_main = _sText

        if _bReverse then
          -- Reset cursor position for left-to-right
          xPos = xMax - #marquee_main
        else
          -- Reset cursor position for right-to-left
          xPos = 1
        end
      end

      -- Increment/Decrement x-Position
      xPos = xPos + step
      -- Restart timer
      scrollTimer = os.startTimer( nScrollTime )
    end
  end
end

term.clear()
marquee( "Press Q to quit.", true )

And why didn't anyone ask me why I included an unused 'writeCentered' function in my previous code!? :wacko:/>
I guess I planned on using that as a helper function at first and in the end forgot to remove it when it became redundant?
Hmm, who knows, sometimes the derpy-bird pounces when you least expect it.

Before I start blabbering any nonsense I should really go to bed, hope it works for you now. :)/>
Cheers
Edited on 26 November 2012 - 04:27 PM
KaoS #23
Posted 27 November 2012 - 01:36 AM
I feel redundant lol :)/> damn pros
Espen #24
Posted 27 November 2012 - 02:47 AM
I feel redundant lol :)/> damn pros
Come on, don't sell yourself short. I just started coding in Lua at the beginning of this year.
Granted, I've been programming for years before that, but once you understand the principles of programming itself then learning new languages isn't that hard.
Then it's mostly about best practices, being structured in one's design process and simply excercising until your ears bleed. :P/>

No but seriously, don't look at anyone's code and let yourself create a virtual barrier in your mind that differentiates between "pro" and "non-pro".
Personally, I don't consider myself a pro. I would feel full of myself if I'd call myself that. If other's give me a label then that's their thing.
But what matters most to me is basically: perpetual improvement.
And you can never be a pro at that, or reach any end-point for that matter, as you can always become better, always understand something better.
Always improve. Ad infinitum.

At least that's the perennial meaning of my life that get's me out of bed in the morning. ^_^/>
darcline #25
Posted 27 November 2012 - 06:45 AM
really thanks :D/> when you have luck i have any one for you what you can build in the script to but, only when you have fun ;)/> then write me
KaoS #26
Posted 27 November 2012 - 10:04 AM
Always improve. Ad infinitum. At least that's the perennial meaning of my life that get's me out of bed in the morning

that is a very good outlook, it will take you far.

being a diabolical coder my focus is the acquisition of power. once I am better than others: able to sneak around their systems without detection and gain power over them by hiding fail-safes and back doors while preventing them from doing the same to me then I am content :D/> I guess I'm a competitive kinda guy. I also take especial pleasure in the design of a perfect system.

that being said I am not utterly ruthless, I would not sabotage anyone who has helped me with pure intentions and I also like to help other coders as I feel… I d'no… a kinship with them I guess. they are the only group that I would never sabotage under the pretense of helping (there are others but that's just 'cos you don't mess with some people… yet :ph34r:/> )