local chatbox = peripheral.find("chatBox")
local monitor = peripheral.find("monitor")

monitor.setTextScale(0.5)
monitor.setBackgroundColor(colors.black)
monitor.setTextColor(colors.white)
monitor.clear()

local width, height = monitor.getSize()
local cursorLine = 1

local function addLine(text)
    if #text > width then
        text = text:sub(1, width)
    end

    if cursorLine > height then
        monitor.scroll(1)          -- alles eine zeile nach oben schieben
        monitor.setCursorPos(1, height)
    else
        monitor.setCursorPos(1, cursorLine)
        cursorLine = cursorLine + 1
    end

    monitor.write(text)
end

addLine("=== chat log gestartet ===")

while true do
    local event, username, message = os.pullEvent("chat")
    addLine(username .. ": " .. message)
end