summaryrefslogtreecommitdiff
path: root/tests/modules/tty/quick_example.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2025-10-27 14:17:54 +0800
committerGitHub <[email protected]>2025-10-27 14:17:54 +0800
commit06dd715e91d3fed3b8101801eceda228c3ed4877 (patch)
tree8c370783dd532b6cd8bec516af2644167ce22a4a /tests/modules/tty/quick_example.lua
parent954944d14d86d2ca4747887150a35b440864c03d (diff)
parent988bc41fb22cdd7e4e005fc985576d46b2fe20d9 (diff)
Merge pull request #6970 from xmake-io/tty
Improve tty
Diffstat (limited to 'tests/modules/tty/quick_example.lua')
-rw-r--r--tests/modules/tty/quick_example.lua137
1 files changed, 137 insertions, 0 deletions
diff --git a/tests/modules/tty/quick_example.lua b/tests/modules/tty/quick_example.lua
new file mode 100644
index 000000000..794a79d58
--- /dev/null
+++ b/tests/modules/tty/quick_example.lua
@@ -0,0 +1,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
+