-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_examples.zig
More file actions
367 lines (293 loc) · 10.5 KB
/
progress_examples.zig
File metadata and controls
367 lines (293 loc) · 10.5 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
const std = @import("std");
const zig_test = @import("zig_test");
/// Example 1: Basic Spinner
pub fn example_basic_spinner() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 1: Basic Spinner ===\n", .{});
var spinner = try zig_test.Spinner.init(allocator, "Loading data...", .dots);
defer spinner.deinit();
try spinner.start();
std.Thread.sleep(2000 * std.time.ns_per_ms);
spinner.succeed("Data loaded successfully");
}
/// Example 2: Different Spinner Styles
pub fn example_spinner_styles() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 2: Spinner Styles ===\n", .{});
const styles = [_]zig_test.SpinnerStyle{
.dots,
.line,
.arc,
.circle,
.square,
.arrow,
.bounce,
};
inline for (styles) |style| {
const style_name = @tagName(style);
const message = try std.fmt.allocPrint(allocator, "Testing {s} spinner...", .{style_name});
defer allocator.free(message);
var spinner = try zig_test.Spinner.init(allocator, message, style);
defer spinner.deinit();
try spinner.start();
std.Thread.sleep(1000 * std.time.ns_per_ms);
spinner.succeed(try std.fmt.allocPrint(allocator, "{s} style complete", .{style_name}));
allocator.free(try std.fmt.allocPrint(allocator, "{s} style complete", .{style_name}));
}
}
/// Example 3: Spinner with different outcomes
pub fn example_spinner_outcomes() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 3: Spinner Outcomes ===\n", .{});
// Success
var spinner1 = try zig_test.Spinner.init(allocator, "Operation 1...", .dots);
defer spinner1.deinit();
try spinner1.start();
std.Thread.sleep(500 * std.time.ns_per_ms);
spinner1.succeed("Operation 1 succeeded");
// Failure
var spinner2 = try zig_test.Spinner.init(allocator, "Operation 2...", .dots);
defer spinner2.deinit();
try spinner2.start();
std.Thread.sleep(500 * std.time.ns_per_ms);
spinner2.fail("Operation 2 failed");
// Warning
var spinner3 = try zig_test.Spinner.init(allocator, "Operation 3...", .dots);
defer spinner3.deinit();
try spinner3.start();
std.Thread.sleep(500 * std.time.ns_per_ms);
spinner3.warn("Operation 3 has warnings");
// Info
var spinner4 = try zig_test.Spinner.init(allocator, "Operation 4...", .dots);
defer spinner4.deinit();
try spinner4.start();
std.Thread.sleep(500 * std.time.ns_per_ms);
spinner4.info("Operation 4 information");
}
/// Example 4: Basic Progress Bar
pub fn example_basic_progress_bar() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 4: Basic Progress Bar ===\n", .{});
var bar = try zig_test.ProgressBar.init(allocator, 100, .{
.message = "Processing",
.width = 40,
});
defer bar.deinit();
var i: usize = 0;
while (i <= 100) : (i += 5) {
bar.update(i);
std.Thread.sleep(100 * std.time.ns_per_ms);
}
bar.finish();
}
/// Example 5: Progress Bar Styles
pub fn example_progress_bar_styles() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 5: Progress Bar Styles ===\n", .{});
const styles = [_]zig_test.ProgressBarStyle{
.classic,
.blocks,
.arrows,
.dots,
};
inline for (styles) |style| {
const style_name = @tagName(style);
const message = try std.fmt.allocPrint(allocator, "{s} style", .{style_name});
defer allocator.free(message);
var bar = try zig_test.ProgressBar.init(allocator, 50, .{
.message = message,
.style = style,
.width = 30,
});
defer bar.deinit();
var i: usize = 0;
while (i <= 50) : (i += 5) {
bar.update(i);
std.Thread.sleep(50 * std.time.ns_per_ms);
}
bar.finish();
}
}
/// Example 6: Progress Bar with Custom Options
pub fn example_progress_bar_options() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 6: Progress Bar Options ===\n", .{});
// Without percentage
var bar1 = try zig_test.ProgressBar.init(allocator, 50, .{
.message = "No percentage",
.show_percentage = false,
});
defer bar1.deinit();
var i: usize = 0;
while (i <= 50) : (i += 10) {
bar1.update(i);
std.Thread.sleep(100 * std.time.ns_per_ms);
}
bar1.finish();
// Without count
var bar2 = try zig_test.ProgressBar.init(allocator, 50, .{
.message = "No count",
.show_count = false,
});
defer bar2.deinit();
i = 0;
while (i <= 50) : (i += 10) {
bar2.update(i);
std.Thread.sleep(100 * std.time.ns_per_ms);
}
bar2.finish();
// Minimal
var bar3 = try zig_test.ProgressBar.init(allocator, 50, .{
.message = "Minimal",
.show_percentage = false,
.show_count = false,
});
defer bar3.deinit();
i = 0;
while (i <= 50) : (i += 10) {
bar3.update(i);
std.Thread.sleep(100 * std.time.ns_per_ms);
}
bar3.finish();
}
/// Example 7: Test Progress Tracker
pub fn example_test_progress() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 7: Test Progress Tracker ===\n", .{});
var progress = try zig_test.TestProgress.init(allocator, 10, .{
.use_spinner = true,
.use_progress_bar = true,
});
defer progress.deinit();
// Simulate running 10 tests
const test_names = [_][]const u8{
"test_addition",
"test_subtraction",
"test_multiplication",
"test_division",
"test_modulo",
"test_power",
"test_square_root",
"test_absolute",
"test_min",
"test_max",
};
for (test_names, 0..) |name, i| {
try progress.startTest(name);
std.Thread.sleep(200 * std.time.ns_per_ms);
// Simulate different outcomes
const passed = i != 3; // Fail the 4th test
const skipped = i == 5; // Skip the 6th test
progress.completeTest(passed, skipped);
}
progress.printSummary();
}
/// Example 8: Test Progress without Visual Indicators
pub fn example_test_progress_minimal() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 8: Minimal Test Progress ===\n", .{});
var progress = try zig_test.TestProgress.init(allocator, 5, .{
.use_spinner = false,
.use_progress_bar = false,
});
defer progress.deinit();
var i: usize = 0;
while (i < 5) : (i += 1) {
const name = try std.fmt.allocPrint(allocator, "test_{d}", .{i});
defer allocator.free(name);
try progress.startTest(name);
std.Thread.sleep(100 * std.time.ns_per_ms);
progress.completeTest(true, false);
}
progress.printSummary();
}
/// Example 9: Multi-Spinner for Parallel Operations
pub fn example_multi_spinner() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 9: Multi-Spinner ===\n", .{});
var multi = zig_test.MultiSpinner.init(allocator);
defer multi.deinit();
// Add multiple spinners
try multi.add("task1", "Processing task 1...");
try multi.add("task2", "Processing task 2...");
try multi.add("task3", "Processing task 3...");
std.Thread.sleep(1000 * std.time.ns_per_ms);
// Complete tasks at different times
multi.succeed("task1", "Task 1 complete");
std.Thread.sleep(500 * std.time.ns_per_ms);
multi.fail("task2", "Task 2 failed");
std.Thread.sleep(500 * std.time.ns_per_ms);
multi.succeed("task3", "Task 3 complete");
}
/// Example 10: Progress Bar with Increment
pub fn example_progress_increment() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 10: Progress Bar Increment ===\n", .{});
var bar = try zig_test.ProgressBar.init(allocator, 20, .{
.message = "Incremental progress",
});
defer bar.deinit();
var i: usize = 0;
while (i < 20) : (i += 1) {
bar.increment();
std.Thread.sleep(100 * std.time.ns_per_ms);
}
bar.finish();
}
/// Example 11: Long Running Operation with Spinner Update
pub fn example_spinner_update() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 11: Spinner with Updates ===\n", .{});
var spinner = try zig_test.Spinner.init(allocator, "Step 1: Initializing...", .dots);
defer spinner.deinit();
try spinner.start();
std.Thread.sleep(1000 * std.time.ns_per_ms);
try spinner.updateMessage("Step 2: Processing data...");
std.Thread.sleep(1000 * std.time.ns_per_ms);
try spinner.updateMessage("Step 3: Finalizing...");
std.Thread.sleep(1000 * std.time.ns_per_ms);
spinner.succeed("All steps completed");
}
/// Example 12: Combined Progress Indicators
pub fn example_combined_progress() !void {
const allocator = std.heap.page_allocator;
std.debug.print("\n=== Example 12: Combined Indicators ===\n", .{});
// Use spinner for overall operation
var spinner = try zig_test.Spinner.init(allocator, "Starting batch process...", .dots);
defer spinner.deinit();
try spinner.start();
std.Thread.sleep(500 * std.time.ns_per_ms);
spinner.stop();
// Use progress bar for sub-tasks
var bar = try zig_test.ProgressBar.init(allocator, 100, .{
.message = "Batch processing",
});
defer bar.deinit();
var i: usize = 0;
while (i <= 100) : (i += 10) {
bar.update(i);
std.Thread.sleep(200 * std.time.ns_per_ms);
}
bar.finish();
// Final success
var final_spinner = try zig_test.Spinner.init(allocator, "Completing...", .dots);
defer final_spinner.deinit();
try final_spinner.start();
std.Thread.sleep(500 * std.time.ns_per_ms);
final_spinner.succeed("Batch process completed successfully");
}
pub fn main() !void {
try example_basic_spinner();
try example_spinner_styles();
try example_spinner_outcomes();
try example_basic_progress_bar();
try example_progress_bar_styles();
try example_progress_bar_options();
try example_test_progress();
try example_test_progress_minimal();
try example_multi_spinner();
try example_progress_increment();
try example_spinner_update();
try example_combined_progress();
std.debug.print("\n=== All Progress Examples Complete! ===\n", .{});
}