View difference between Paste ID: uzK5V1Kv and fdbvGdAA
SHOW: | | - or go back to the newest paste.
1
local monitor
2
local sides = {"left", "right", "top", "bottom", "back", "front"}
3
local str = "Hello, world! "
4
local pos = 1
5
local speedStep = 0.5
6
local speed = 2
7
local maxSpeed = 10
8
local running = false
9
local keys = {
10
    backspace=14,
11
    enter=28,
12
    f1=59,
13
    f3=61,
14
    leftArrow=203,
15
    rightArrow=205,
16
}
17
local locked = false
18
local password = ""
19
local correct_password = ""
20
21
local banner = {
22
" ___ ___                                          ",
23
"|   Y   |.---.-..----..-----..--.--..-----..-----.",
24
"|.      ||  _  ||   _||  _  ||  |  ||  -__||  -__|",
25
"|. \\_/  ||___._||__|  |__   ||_____||_____||_____|",
26-
"|:  |   |                |__|                     ",
26+
"|:  |   |                |__|                      ",
27-
"|::.|:. |   v1.1                 by zapwow        ",
27+
"|::.|:. |   v1.1                 by Severino       ",
28
"'--- ---'   "
29
}
30
31
function setup()
32
    -- prevent user from terminating
33
    os.pullEvent = os.pullEventRaw
34
35
    -- attempt to load the string from disk
36
    local file = io.open("marquee-text", "r")
37
    if file then
38
        str = file:read("*l") or str
39
        speed = tonumber(file:read("*l")) or speed
40
        correct_password = file:read("*l") or correct_password
41
        if string.len(correct_password) > 0 then
42
            -- the password was set at some point, lock automatically
43
            locked = true
44
        end
45
        file:close()
46
    end
47
48
    for i,side in ipairs(sides) do
49
        if peripheral.getType(side) == "monitor" then
50
            monitor = peripheral.wrap(side)
51
            monitor.setTextScale(5)
52
            running = true
53
            return
54
        end
55
    end
56
    print("No monitor attached")
57
    error()
58
end
59
60
function draw()
61
    -- draw the string offset by pos
62
    -- increment pos
63
    -- when pos > str len, pos = 1
64
    monitor.clear()
65
    monitor.setCursorPos(1,1)
66
67
    if string.len(str) == 0 then
68
        return
69
    end
70
71
    -- pad the str to fit the screen
72
    local _str = str
73
    local len, height = monitor.getSize()
74
    while string.len(_str) < len do
75
        _str = _str .. _str
76
    end
77
78
    local output = string.sub(_str, pos) .. string.sub(_str, 0, pos-1)
79
    monitor.write(output)
80
end
81
82
function update()
83
    pos = pos + 1
84
    if pos > string.len(str) then
85
        pos = 1
86
    end
87
end
88
89
function marquee()
90
    while running do
91
        draw()
92
        if speed > 0 then
93
            update()
94
            sleep(1/speed)
95
        else
96
            sleep(0.5)
97
        end
98
    end
99
end
100
101
function drawGUI()
102
    --always draw the banner
103
    term.clear()
104
    for i,v in ipairs(banner) do
105
        term.setCursorPos(1,i)
106
        term.write(v)
107
    end
108
109
    if locked then
110
        drawLockedGUI()
111
    else
112
        drawUnlockedGUI()
113
    end
114
end
115
116
function drawLockedGUI()
117
    local line = #banner + 2
118
    term.setCursorPos(1,line)
119
    if string.len(correct_password) == 0 then
120
        term.write("  Set password: ")
121
    else
122
        term.write("  Password: ")
123
    end
124
    term.write(string.rep('*', string.len(password)))
125
end
126
127
function drawUnlockedGUI()
128
    local line = #banner + 2
129
130
    -- lock
131
    term.setCursorPos(1,line)
132
    term.write("  To lock terminal, press F1")
133
134
    -- exit
135
    line = line + 1
136
    term.setCursorPos(1,line)
137
    term.write("  To quit, press F3")
138
139
    -- speed gui
140
    line = line + 2
141
    term.setCursorPos(1,line)
142
    term.write(
143
        "  Speed: <" .. 
144
        string.rep('-', speed/speedStep) ..
145
        'X' .. 
146
        string.rep('-', (maxSpeed - speed)/speedStep) .. 
147
        "> " .. 
148
        string.format("%.1f", speed) .. 
149
        " steps/sec"
150
    )
151
152
    -- text
153
    line = line + 2
154
    term.setCursorPos(1,line)
155
    term.write(
156
        "  Text: " .. 
157
        str
158
    )
159
    term.setCursorBlink(true)
160
end
161
162
function save()
163
    local file = io.open("marquee-text", "w")
164
    if file then
165
        file:write(str)
166
        file:write("\n")
167
        file:write(speed)
168
        file:write("\n")
169
        file:write(password)
170
        file:close()
171
    end
172
end
173
174
function gui()
175
    drawGUI()
176
    while running do
177
        local evt, p1, p2, p3 = os.pullEvent()
178
        if evt == "char" then
179
            if locked then
180
                -- read the password
181
                password = password .. p1
182
            else
183
                str = str .. p1
184
                save()
185
            end
186
        elseif evt == "key" then
187
            if p1 == keys["backspace"] and not locked then
188
                str = string.sub(str, 1, string.len(str)-1)
189
                save()
190
            elseif p1 == keys["leftArrow"] and not locked then
191
                speed = math.max(speed-speedStep, 0)
192
                save()
193
            elseif p1 == keys["rightArrow"] and not locked then
194
                speed = math.min(speed+speedStep, maxSpeed)
195
                save()
196
            elseif p1 == keys["f1"] and not locked then
197
                locked = true
198
            elseif p1 == keys["f3"] and not locked then
199
                term.clear()
200
                term.setCursorPos(1,1)
201
                return true
202
            elseif p1 == keys["enter"] and locked then
203
                if string.len(correct_password) == 0 then
204
                    correct_password = password
205
                    save()
206
                else
207
                    if password == correct_password then
208
                        locked = false
209
                    end
210
                end
211
                password = ""
212
            end
213
        end
214
        drawGUI()
215
    end
216
end
217
218
219
setup()
220
parallel.waitForAny(marquee, gui)