1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
-- Quick example: Demonstrates the most common use cases
import("core.base.tty")
function example1_simple_progress()
print("\n=== Example 1: Simple Progress Bar ===\n")
if not tty.has_vtansi() then
print("ANSI not supported")
return
end
print("Downloading file...")
for i = 0, 100, 5 do
local width = 40
local filled = math.floor(i / 100 * width)
local bar = string.rep("█", filled) .. string.rep("░", width - filled)
-- Move to start of line, clear it, and redraw
tty.cr()
tty.erase_line()
io.write(string.format("[%s] %3d%%", bar, i))
io.flush()
os.sleep(50)
end
print("") -- New line after progress
end
function example2_update_previous_line()
print("\n=== Example 2: Update Previous Line ===\n")
if not tty.has_vtansi() then
print("ANSI not supported")
return
end
io.write("Building project...\n")
io.write("Status: Starting...\n")
io.flush()
os.sleep(1000)
-- Go back and update the status line
tty.cursor_move_up(1)
tty.cr()
tty.erase_line()
io.write("Status: Compiling files...\n")
io.flush()
os.sleep(1000)
tty.cursor_move_up(1)
tty.cr()
tty.erase_line()
io.write("Status: Done! ✓\n")
io.flush()
end
function example3_multi_line_update()
print("\n=== Example 3: Multi-line Updates ===\n")
if not tty.has_vtansi() then
print("ANSI not supported")
return
end
-- Create a simple status board
io.write("Task 1: Waiting...\n")
io.write("Task 2: Waiting...\n")
io.write("Task 3: Waiting...\n")
io.flush()
tty.cursor_hide()
-- Update Task 1 to Running
tty.cursor_move_up(3)
tty.cr()
tty.erase_line()
io.write("Task 1: Running... \n")
io.flush()
os.sleep(500)
-- Update Task 1 to Done
tty.cursor_move_up(1)
tty.cr()
tty.erase_line()
io.write("Task 1: Done ✓\n")
io.flush()
-- Update Task 2 to Running
tty.cr()
tty.erase_line()
io.write("Task 2: Running... \n")
io.flush()
os.sleep(500)
-- Update Task 2 to Done
tty.cursor_move_up(1)
tty.cr()
tty.erase_line()
io.write("Task 2: Done ✓\n")
io.flush()
-- Update Task 3 to Running
tty.cr()
tty.erase_line()
io.write("Task 3: Running... \n")
io.flush()
os.sleep(500)
-- Update Task 3 to Done
tty.cursor_move_up(1)
tty.cr()
tty.erase_line()
io.write("Task 3: Done ✓\n")
io.flush()
tty.cursor_show()
end
function main()
print(string.rep("=", 60))
print("TTY Cursor Control - Quick Examples")
print(string.rep("=", 60))
example1_simple_progress()
example2_update_previous_line()
example3_multi_line_update()
print("\n" .. string.rep("=", 60))
print("All examples completed!")
print("Check test.lua, cursor_control.lua, and live_dashboard.lua")
print("for more advanced examples.")
print(string.rep("=", 60))
end
|