boxen 0.3.3

A Rust library for creating styled terminal boxes around text with performance optimizations
Documentation
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/// Demonstration of common usage patterns and best practices for boxen
use ::boxen::{
    BorderStyle, TextAlignment, TitleAlignment, builder, double_box, round_box, simple_box,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Boxen Usage Patterns Demo ===\n");

    // 1. Quick and simple patterns
    demonstrate_quick_patterns()?;

    // 2. CLI application patterns
    demonstrate_cli_patterns()?;

    // 3. Configuration and settings display
    demonstrate_config_patterns()?;

    // 4. Status and notification patterns
    demonstrate_status_patterns()?;

    // 5. Data presentation patterns
    demonstrate_data_patterns()?;

    // 6. Interactive application patterns
    demonstrate_interactive_patterns()?;

    // 7. Logging and debugging patterns
    demonstrate_logging_patterns()?;

    // 8. Documentation and help patterns
    demonstrate_documentation_patterns()?;

    println!("\n=== Usage Patterns Demo Complete ===");
    Ok(())
}

fn demonstrate_quick_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("1. Quick and Simple Patterns:");

    // One-liner for simple messages
    println!("Quick message:");
    println!("{}", simple_box("Quick message"));

    // Emphasis with double border
    println!("Important message:");
    println!("{}", double_box("⚠️  Important: Please read carefully"));

    // Friendly rounded corners
    println!("Friendly notification:");
    println!("{}", round_box("✨ Welcome to our application!"));

    // Builder for slightly more complex needs
    println!("Builder one-liner:");
    println!(
        "{}",
        builder()
            .border_color("green")
            .padding(1)
            .render("✓ Success: Operation completed")?
    );

    println!();
    Ok(())
}

fn demonstrate_cli_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("2. CLI Application Patterns:");

    // Command help display
    println!("Command help:");
    let help_text = "USAGE:\n    myapp [OPTIONS] <FILE>\n\nOPTIONS:\n    -h, --help       Print help information\n    -v, --verbose    Enable verbose output\n    -o, --output     Output file path";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Single)
            .title("Help")
            .title_alignment(TitleAlignment::Center)
            .padding(1)
            .width(50)
            .render(help_text)?
    );

    // Progress indication
    println!("Progress display:");
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Round)
            .border_color("blue")
            .title("⏳ Processing")
            .padding(1)
            .render("████████░░ 80% complete\nProcessing file: data.csv\nETA: 30 seconds")?
    );

    // Error reporting
    println!("Error reporting:");
    println!("{}", builder()
        .border_style(BorderStyle::Bold)
        .border_color("red")
        .title("❌ Error")
        .padding(1)
        .render("Failed to open file: permission denied\n\nSuggestion: Check file permissions\nRun with: sudo myapp file.txt")?);

    // Success confirmation
    println!("Success confirmation:");
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Double)
            .border_color("green")
            .title("✅ Success")
            .padding(1)
            .text_alignment(TextAlignment::Center)
            .render("File processed successfully!\n\n3 records updated\n1 new record created")?
    );

    println!();
    Ok(())
}

fn demonstrate_config_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("3. Configuration and Settings Display:");

    // System configuration
    println!("System configuration:");
    let config_text = "Database URL: postgresql://localhost:5432/mydb\nRedis URL: redis://localhost:6379\nLog Level: INFO\nDebug Mode: false\nPort: 8080\nEnvironment: production";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Single)
            .title("🔧 Configuration")
            .title_alignment(TitleAlignment::Left)
            .padding(2)
            .width(55)
            .render(config_text)?
    );

    // Environment variables
    println!("Environment variables:");
    let env_text = "NODE_ENV=production\nPORT=3000\nDATABASE_URL=***hidden***\nAPI_KEY=***hidden***\nDEBUG=false";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Round)
            .border_color("cyan")
            .title("Environment")
            .padding(1)
            .margin(1)
            .render(env_text)?
    );

    // Feature flags
    println!("Feature flags:");
    let features_text = "✅ New Dashboard: enabled\n❌ Beta Features: disabled\n✅ Analytics: enabled\n⚠️  Experimental API: testing\n✅ Auto-backup: enabled";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Double)
            .title("🚩 Feature Flags")
            .title_alignment(TitleAlignment::Center)
            .padding(1)
            .width(40)
            .render(features_text)?
    );

    println!();
    Ok(())
}

fn demonstrate_status_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("4. Status and Notification Patterns:");

    // System status dashboard
    println!("System status:");
    let status_text = "🟢 API Server: Online\n🟢 Database: Connected\n🟡 Cache: Degraded (high latency)\n🔴 Email Service: Offline\n🟢 File Storage: Available";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Bold)
            .title("📊 System Status")
            .title_alignment(TitleAlignment::Center)
            .padding(2)
            .width(45)
            .text_alignment(TextAlignment::Left)
            .render(status_text)?
    );

    // Health check results
    println!("Health check:");
    println!("{}", builder()
        .border_style(BorderStyle::Round)
        .border_color("green")
        .background_color("black")
        .title("💚 Health Check")
        .padding(1)
        .render("All systems operational\nResponse time: 45ms\nUptime: 99.9%\nLast check: 2 seconds ago")?);

    // Warning notification
    println!("Warning notification:");
    println!("{}", builder()
        .border_style(BorderStyle::SingleDouble)
        .border_color("yellow")
        .title("⚠️  Warning")
        .padding(1)
        .render("Disk space is running low\n\nCurrent usage: 85%\nRecommended action: Clean up old logs")?);

    // Critical alert
    println!("Critical alert:");
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::DoubleSingle)
            .border_color("red")
            .background_color("black")
            .title("🚨 CRITICAL ALERT")
            .title_alignment(TitleAlignment::Center)
            .padding(2)
            .text_alignment(TextAlignment::Center)
            .render(
                "SYSTEM OVERLOAD DETECTED\n\nCPU: 95%\nMemory: 98%\nImmediate action required!"
            )?
    );

    println!();
    Ok(())
}

fn demonstrate_data_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("5. Data Presentation Patterns:");

    // Table-like data
    println!("Tabular data:");
    let table_text = "Name          | Status    | Last Seen\n──────────────┼───────────┼──────────────\nAlice Johnson | Online    | 2 min ago\nBob Smith     | Away      | 15 min ago\nCarol Davis   | Offline   | 2 hours ago\nDave Wilson   | Online    | Just now";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Single)
            .title("👥 User Status")
            .padding(1)
            .render(table_text)?
    );

    // Statistics display
    println!("Statistics:");
    let stats_text = "📈 Performance Metrics\n\nRequests/sec:     1,247\nAvg Response:     125ms\nError Rate:       0.02%\nActive Users:     3,456\nMemory Usage:     67%";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Double)
            .border_color("blue")
            .padding(2)
            .width(35)
            .render(stats_text)?
    );

    // Key-value pairs
    println!("Configuration values:");
    let kv_text = "server.host ................. localhost\nserver.port ................. 8080\ndb.connections.max .......... 100\ndb.connections.timeout ...... 30s\ncache.ttl ................... 3600s\nlog.level ................... INFO";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Round)
            .title("⚙️  Settings")
            .padding(1)
            .width(50)
            .render(kv_text)?
    );

    println!();
    Ok(())
}

fn demonstrate_interactive_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("6. Interactive Application Patterns:");

    // Menu display
    println!("Menu interface:");
    let menu_text = "1. Create new project\n2. Open existing project\n3. Import from Git\n4. Settings\n5. Exit\n\nPress number to select option...";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Round)
            .title("📋 Main Menu")
            .title_alignment(TitleAlignment::Center)
            .padding(2)
            .width(35)
            .render(menu_text)?
    );

    // Form display
    println!("Form interface:");
    let form_text = "Username: [________________]\nPassword: [****************]\nEmail:    [________________]\n\n[ ] Remember me\n[x] Accept terms\n\n[Submit] [Cancel]";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Double)
            .title("🔐 Login Form")
            .padding(2)
            .width(35)
            .render(form_text)?
    );

    // Dialog box
    println!("Confirmation dialog:");
    let dialog_text = "Are you sure you want to delete\nthis file?\n\nThis action cannot be undone.\n\n[Yes] [No] [Cancel]";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Bold)
            .border_color("red")
            .title("⚠️  Confirm Delete")
            .title_alignment(TitleAlignment::Center)
            .padding(2)
            .text_alignment(TextAlignment::Center)
            .width(40)
            .render(dialog_text)?
    );

    println!();
    Ok(())
}

fn demonstrate_logging_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("7. Logging and Debugging Patterns:");

    // Debug information
    println!("Debug output:");
    let debug_text = "Function: process_data()\nInput: 1,247 records\nFiltered: 1,203 valid records\nProcessed: 1,203 records\nOutput: success.json\nDuration: 2.34s";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Single)
            .border_color("cyan")
            .title("🐛 Debug Info")
            .padding(1)
            .render(debug_text)?
    );

    // Error trace
    println!("Error trace:");
    let trace_text = "ERROR: Database connection failed\n\nStack trace:\n  at connect() line 45\n  at init() line 23\n  at main() line 12\n\nCause: Connection timeout after 30s";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Bold)
            .border_color("red")
            .title("💥 Error Trace")
            .padding(1)
            .width(45)
            .render(trace_text)?
    );

    // Performance metrics
    println!("Performance log:");
    let perf_text = "⏱️  Execution Time: 1.23s\n📊 Memory Peak: 45.2 MB\n🔄 Cache Hits: 89%\n📈 Throughput: 2,456 ops/sec\n🎯 Success Rate: 99.8%";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Round)
            .border_color("green")
            .title("Performance")
            .padding(1)
            .render(perf_text)?
    );

    println!();
    Ok(())
}

fn demonstrate_documentation_patterns() -> Result<(), Box<dyn std::error::Error>> {
    println!("8. Documentation and Help Patterns:");

    // API documentation
    println!("API documentation:");
    let api_text = "GET /api/users/{id}\n\nReturns user information by ID.\n\nParameters:\n  id (required): User ID\n\nResponse: User object\nStatus: 200 OK | 404 Not Found";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Single)
            .title("📚 API Reference")
            .padding(2)
            .width(50)
            .render(api_text)?
    );

    // Quick reference
    println!("Quick reference:");
    let ref_text = "Keyboard Shortcuts:\n\nCtrl+N    New file\nCtrl+O    Open file\nCtrl+S    Save file\nCtrl+Z    Undo\nCtrl+Y    Redo\nF1        Help";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Round)
            .title("⌨️  Shortcuts")
            .padding(1)
            .render(ref_text)?
    );

    // Tips and tricks
    println!("Tips display:");
    let tips_text = "💡 Pro Tip:\n\nUse Ctrl+Shift+P to open the\ncommand palette for quick access\nto all available commands.\n\nTry typing 'help' to see more tips!";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Double)
            .border_color("yellow")
            .title("Tips & Tricks")
            .title_alignment(TitleAlignment::Center)
            .padding(2)
            .width(45)
            .text_alignment(TextAlignment::Left)
            .render(tips_text)?
    );

    // Version information
    println!("Version info:");
    let version_text = "MyApp v2.1.0\nBuild: 20241201-abc123\nRust: 1.70.0\nOS: Windows 11\n\n© 2024 MyCompany\nLicense: MIT";
    println!(
        "{}",
        builder()
            .border_style(BorderStyle::Classic)
            .title("ℹ️  About")
            .title_alignment(TitleAlignment::Center)
            .padding(2)
            .text_alignment(TextAlignment::Center)
            .width(30)
            .render(version_text)?
    );

    println!();
    Ok(())
}