rich-ruby 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,546 +1,547 @@
1
- # Rich Ruby (rich-ruby)
2
-
3
- A Pure Ruby library for rich text and beautiful formatting in the terminal.
4
-
5
- Rich Ruby provides an elegant API for creating stylish terminal output with colors,
6
- tables, panels, trees, progress bars, syntax highlighting, and more. It is inspired
7
- by the Python Rich library but is a complete Ruby-native implementation.
8
-
9
- ---
10
-
11
- ## Table of Contents
12
-
13
- 1. [Features](#features)
14
- 2. [Requirements](#requirements)
15
- 3. [Installation](#installation)
16
- 4. [Quick Start](#quick-start)
17
- 5. [Usage Guide](#usage-guide)
18
- - [Colors and Styles](#colors-and-styles)
19
- - [Text and Markup](#text-and-markup)
20
- - [Panels](#panels)
21
- - [Tables](#tables)
22
- - [Trees](#trees)
23
- - [Progress Bars and Spinners](#progress-bars-and-spinners)
24
- - [Syntax Highlighting](#syntax-highlighting)
25
- - [Markdown Rendering](#markdown-rendering)
26
- - [JSON Output](#json-output)
27
- 6. [API Reference](#api-reference)
28
- 7. [Testing](#testing)
29
- 8. [License](#license)
30
-
31
- ---
32
-
33
- ## Features
34
-
35
- - **Colors**: Full support for 16-color, 256-color, and TrueColor (24-bit) terminals
36
- - **Styles**: Bold, italic, underline, strikethrough, blink, reverse, and more
37
- - **Markup**: Simple markup syntax like `[bold red]text[/]` for inline styling
38
- - **Panels**: Bordered boxes with titles and subtitles
39
- - **Tables**: Data tables with column alignment and styling
40
- - **Trees**: Hierarchical tree views with different guide styles
41
- - **Progress**: Animated progress bars and spinners
42
- - **Syntax**: Code syntax highlighting for Ruby, Python, JavaScript, SQL, and more
43
- - **Markdown**: Render Markdown documents in the terminal
44
- - **Windows**: Full Windows Console API support with automatic ANSI enabling
45
- - **Zero Dependencies**: Pure Ruby with no external gem dependencies
46
-
47
- ---
48
-
49
- ## Requirements
50
-
51
- This library was developed and tested on:
52
-
53
- - **Ruby**: 3.4.8 (MSVC build)
54
- - **Platform**: Windows 10 64-bit (21H2)
55
- - **Compiler**: Visual Studio 2026 (MSVC)
56
-
57
- The library should work on:
58
-
59
- - Ruby 3.0 or later
60
- - Windows, macOS, Linux
61
- - Any terminal supporting ANSI escape codes
62
-
63
- ### Windows Native Support
64
-
65
- For Windows users, the library provides **native integration** with the Windows Console API using [Fiddle](https://ruby-doc.org/stdlib/libdoc/fiddle/rdoc/Fiddle.html). This allows it to:
66
-
67
- 1. **Enable ANSI/VT Processing**: Automatically configures the console to handle ANSI escape codes, even on older versions of Windows 10.
68
- 2. **Fallback to Console API**: On legacy systems where ANSI is not supported, it uses the Windows Console API (e.g., `SetConsoleTextAttribute`) to provide color and styling.
69
- 3. **Accurate Console Dimensions**: Uses native API calls to determine the exact width and height of the console window.
70
- 4. **Hardware Features**: Full control over cursor visibility, window titles, and screen clearing via native calls.
71
-
72
- This works out-of-the-box with Windows Terminal, PowerShell, and the classic Command Prompt.
73
-
74
- ---
75
-
76
- ## Installation
77
-
78
- ### From RubyGems
79
-
80
- ```bash
81
- gem install rich-ruby
82
- ```
83
-
84
- ### From source
85
-
86
- ```bash
87
- git clone https://github.com/tigel-agm/rich.git
88
- cd rich
89
- gem build rich-ruby.gemspec
90
- gem install rich-ruby-0.1.0.gem
91
- ```
92
-
93
- ### In your Gemfile
94
-
95
- ```ruby
96
- gem 'rich-ruby'
97
- ```
98
-
99
- ---
100
-
101
- ## Quick Start
102
-
103
- ```ruby
104
- require 'rich'
105
-
106
- # Simple styled output
107
- Rich.print("[bold cyan]Hello[/] [yellow]World![/]")
108
-
109
- # Create a console instance for more control
110
- console = Rich::Console.new
111
- console.print("Welcome!", style: "bold green")
112
-
113
- # Display a panel
114
- panel = Rich::Panel.new(
115
- "This is important information.",
116
- title: "Notice",
117
- border_style: "cyan"
118
- )
119
- puts panel.render(max_width: 50)
120
-
121
- # Display a table
122
- table = Rich::Table.new(title: "Users")
123
- table.add_column("Name", header_style: "bold")
124
- table.add_column("Role")
125
- table.add_row("Alice", "Admin")
126
- table.add_row("Bob", "User")
127
- puts table.render(max_width: 40)
128
- ```
129
-
130
- ---
131
-
132
- ## Documentation
133
-
134
- For more detailed information, check out our guides:
135
-
136
- - [**How-To Use**](docs/how-to-use.md): Tiered guide for all levels.
137
- - [**Troubleshooting & FAQ**](docs/troubleshooting.md): Solutions for common issues.
138
- - [**Customization**](docs/customization.md): Learn how to extend the library.
139
- - [**Cheat Sheet**](docs/cheat-sheet.md): Quick reference for styles and colors.
140
- - [**Windows Notes**](docs/windows-notes.md): Technical details on Windows support.
141
- - [**Test Report**](docs/test-report.md): Verification and performance results.
142
-
143
- ---
144
-
145
- ## Usage Guide
146
-
147
- ### Colors and Styles
148
-
149
- Rich Ruby supports multiple color systems:
150
-
151
- ```ruby
152
- # Standard 16 colors
153
- style = Rich::Style.parse("red")
154
- style = Rich::Style.parse("bright_blue")
155
-
156
- # 256-color palette
157
- style = Rich::Style.parse("color(42)")
158
-
159
- # TrueColor (24-bit)
160
- style = Rich::Style.parse("#ff5500")
161
- style = Rich::Style.parse("rgb(255, 85, 0)")
162
-
163
- # Background colors
164
- style = Rich::Style.parse("white on blue")
165
- style = Rich::Style.parse("black on #ffcc00")
166
- ```
167
-
168
- Available text attributes:
169
-
170
- | Attribute | Description |
171
- |-----------|-------------|
172
- | `bold` | Bold text |
173
- | `dim` | Dimmed text |
174
- | `italic` | Italic text |
175
- | `underline` | Underlined text |
176
- | `underline2` | Double underline |
177
- | `overline` | Overlined text |
178
- | `blink` | Blinking text |
179
- | `reverse` | Reversed colors |
180
- | `conceal` | Hidden text |
181
- | `strike` | Strikethrough |
182
-
183
- Combine multiple attributes:
184
-
185
- ```ruby
186
- style = Rich::Style.parse("bold italic red on white")
187
- ```
188
-
189
- ### Text and Markup
190
-
191
- The markup syntax provides an easy way to style text inline:
192
-
193
- ```ruby
194
- # Basic markup
195
- Rich.print("[bold]Bold text[/bold]")
196
- Rich.print("[red]Red text[/red]")
197
-
198
- # Shorthand close tag
199
- Rich.print("[bold]Bold text[/]")
200
-
201
- # Nested styles
202
- Rich.print("[bold][red]Bold and red[/red][/bold]")
203
-
204
- # Combined styles
205
- Rich.print("[bold italic cyan on black]Styled[/]")
206
-
207
- # In strings
208
- text = Rich::Markup.parse("Name: [cyan]Alice[/] Age: [yellow]30[/]")
209
- puts Rich::Segment.render(text.to_segments)
210
- ```
211
-
212
- ### Panels
213
-
214
- Create bordered panels to highlight content:
215
-
216
- ```ruby
217
- # Simple panel
218
- panel = Rich::Panel.new("Hello World")
219
- puts panel.render(max_width: 40)
220
-
221
- # Panel with title and subtitle
222
- panel = Rich::Panel.new(
223
- "Important information goes here.",
224
- title: "Alert",
225
- subtitle: "Read carefully",
226
- border_style: "red",
227
- title_style: "bold white"
228
- )
229
- puts panel.render(max_width: 50)
230
-
231
- # Different box styles
232
- panel = Rich::Panel.new("Content", box: Rich::Box::DOUBLE)
233
- panel = Rich::Panel.new("Content", box: Rich::Box::ROUNDED)
234
- panel = Rich::Panel.new("Content", box: Rich::Box::HEAVY)
235
- panel = Rich::Panel.new("Content", box: Rich::Box::ASCII)
236
- ```
237
-
238
- ### Tables
239
-
240
- Create formatted data tables:
241
-
242
- ```ruby
243
- # Create a table
244
- table = Rich::Table.new(title: "Sales Report", border_style: "blue")
245
-
246
- # Add columns with styling
247
- table.add_column("Product", header_style: "bold cyan")
248
- table.add_column("Price", justify: :right, header_style: "bold cyan")
249
- table.add_column("Quantity", justify: :center)
250
-
251
- # Add data rows
252
- table.add_row("Widget", "$10.00", "100")
253
- table.add_row("Gadget", "$25.50", "50")
254
- table.add_row("Gizmo", "$5.99", "200")
255
-
256
- # Render
257
- puts table.render(max_width: 60)
258
- ```
259
-
260
- Column justification options: `:left`, `:center`, `:right`
261
-
262
- ### Trees
263
-
264
- Display hierarchical data:
265
-
266
- ```ruby
267
- # Create a tree
268
- tree = Rich::Tree.new("Project", style: "bold yellow")
269
-
270
- # Add nodes
271
- src = tree.add("src/", style: "bold")
272
- src.add("main.rb", style: "green")
273
- src.add("config.rb", style: "green")
274
-
275
- lib = tree.add("lib/")
276
- lib.add("utils.rb")
277
-
278
- tree.add("README.md", style: "cyan")
279
-
280
- # Render
281
- puts tree.render
282
- ```
283
-
284
- Different guide styles:
285
-
286
- ```ruby
287
- tree = Rich::Tree.new("Root", guide: Rich::TreeGuide::ASCII)
288
- tree = Rich::Tree.new("Root", guide: Rich::TreeGuide::ROUNDED)
289
- tree = Rich::Tree.new("Root", guide: Rich::TreeGuide::BOLD)
290
- ```
291
-
292
- ### Progress Bars and Spinners
293
-
294
- Show progress for long-running tasks:
295
-
296
- ```ruby
297
- # Progress bar
298
- bar = Rich::ProgressBar.new(total: 100, width: 40)
299
-
300
- 100.times do |i|
301
- bar.update(i + 1)
302
- print "\rProgress: #{bar.render}"
303
- sleep(0.05)
304
- end
305
- puts ""
306
-
307
- # Spinner
308
- spinner = Rich::Spinner.new(frames: Rich::ProgressStyle::DOTS)
309
-
310
- 20.times do
311
- print "\r#{spinner.frame} Loading..."
312
- spinner.advance
313
- sleep(0.1)
314
- end
315
- puts "\rDone! "
316
- ```
317
-
318
- Available spinner styles:
319
- - `Rich::ProgressStyle::DOTS`
320
- - `Rich::ProgressStyle::LINE`
321
- - `Rich::ProgressStyle::CIRCLE`
322
- - `Rich::ProgressStyle::BOUNCE`
323
-
324
- ### Syntax Highlighting
325
-
326
- Highlight source code:
327
-
328
- ```ruby
329
- code = <<~RUBY
330
- def greet(name)
331
- puts "Hello, #{name}!"
332
- end
333
- RUBY
334
-
335
- # Basic highlighting
336
- syntax = Rich::Syntax.new(code, language: "ruby")
337
- puts syntax.render
338
-
339
- # With line numbers
340
- syntax = Rich::Syntax.new(code, language: "ruby", line_numbers: true)
341
- puts syntax.render
342
-
343
- # With a theme
344
- syntax = Rich::Syntax.new(code, language: "python", theme: :monokai)
345
- syntax = Rich::Syntax.new(code, language: "javascript", theme: :dracula)
346
- ```
347
-
348
- Supported languages: Ruby, Python, JavaScript, SQL, JSON, YAML, Bash
349
-
350
- Available themes: `:default`, `:monokai`, `:dracula`
351
-
352
- ### Markdown Rendering
353
-
354
- Render Markdown in the terminal:
355
-
356
- ```ruby
357
- markdown = <<~MD
358
- # Welcome
359
-
360
- This is **bold** and *italic* text.
361
-
362
- ## Features
363
-
364
- - Item one
365
- - Item two
366
-
367
- ```ruby
368
- puts "Code block"
369
- ```
370
-
371
- | Column A | Column B |
372
- |----------|----------|
373
- | Value 1 | Value 2 |
374
- MD
375
-
376
- md = Rich::Markdown.new(markdown)
377
- puts md.render(max_width: 70)
378
- ```
379
-
380
- Supported Markdown elements:
381
- - Headings (H1-H6)
382
- - Bold, italic, strikethrough
383
- - Inline code
384
- - Code blocks with language
385
- - Ordered and unordered lists
386
- - Blockquotes
387
- - Tables
388
- - Links
389
- - Horizontal rules
390
-
391
- ### JSON Output
392
-
393
- Pretty-print JSON with syntax highlighting:
394
-
395
- ```ruby
396
- data = {
397
- "name" => "Alice",
398
- "age" => 30,
399
- "active" => true,
400
- "roles" => ["admin", "user"]
401
- }
402
-
403
- puts Rich::JSON.to_s(data)
404
-
405
- # Highlight existing JSON string
406
- json_str = '{"key": "value"}'
407
- puts Rich::JSON.highlight(json_str)
408
- ```
409
-
410
- ---
411
-
412
- ## API Reference
413
-
414
- ### Core Classes
415
-
416
- | Class | Description |
417
- |-------|-------------|
418
- | `Rich::Console` | Main console interface |
419
- | `Rich::Color` | Color representation and parsing |
420
- | `Rich::Style` | Text style with color and attributes |
421
- | `Rich::Segment` | Styled text segment |
422
- | `Rich::Text` | Rich text with spans |
423
- | `Rich::Markup` | Markup parser |
424
-
425
- ### Components
426
-
427
- | Class | Description |
428
- |-------|-------------|
429
- | `Rich::Panel` | Bordered panel |
430
- | `Rich::Table` | Data table |
431
- | `Rich::Tree` | Tree view |
432
- | `Rich::ProgressBar` | Progress bar |
433
- | `Rich::Spinner` | Animated spinner |
434
- | `Rich::Syntax` | Syntax highlighter |
435
- | `Rich::Markdown` | Markdown renderer |
436
- | `Rich::JSON` | JSON formatter |
437
-
438
- ### Module Methods
439
-
440
- ```ruby
441
- Rich.print(*objects, style: nil) # Print with markup support
442
- Rich.print_json(data) # Print formatted JSON
443
- Rich.rule(title) # Print horizontal rule
444
- Rich.get_console # Get global console instance
445
- Rich.reconfigure(**options) # Reconfigure global console
446
- ```
447
-
448
- ---
449
-
450
- ## Testing
451
-
452
- Run the full test suite:
453
-
454
- ```bash
455
- cd rich
456
- ruby -W0 -Ilib -Itest -e "Dir['test/test_*.rb'].each { |f| require_relative f }"
457
- ```
458
-
459
- > [!TIP]
460
- > Use the `-W0` flag to suppress environmental warnings (like the `io-nonblock` extension warning) for a cleaner test output.
461
-
462
- Or with Rake (if rake is available):
463
-
464
- ```bash
465
- rake test
466
- ```
467
-
468
- Run individual test files:
469
-
470
- ```bash
471
- ruby -W0 -Ilib -Itest test/test_color.rb
472
- ruby -W0 -Ilib -Itest test/test_style.rb
473
- ```
474
-
475
- Run stress tests:
476
-
477
- ```bash
478
- ruby examples/stress_test.rb
479
- ```
480
-
481
- ---
482
-
483
- ## Project Structure
484
-
485
- ```
486
- rich/
487
- lib/
488
- rich.rb # Main entry point
489
- rich/
490
- color.rb # Color handling
491
- style.rb # Text styles
492
- text.rb # Rich text
493
- markup.rb # Markup parser
494
- panel.rb # Panel component
495
- table.rb # Table component
496
- tree.rb # Tree component
497
- progress.rb # Progress bars and spinners
498
- syntax.rb # Syntax highlighting
499
- markdown.rb # Markdown rendering
500
- json.rb # JSON formatting
501
- console.rb # Console interface
502
- ...
503
- test/
504
- test_*.rb # Test files
505
- examples/
506
- demo.rb # Basic demo
507
- showcase.rb # Interactive showcase
508
- stress_test.rb # Stress tests
509
- ```
510
-
511
- ---
512
-
513
- ## License
514
-
515
- This project is licensed under the MIT License.
516
-
517
- This means:
518
- - You can use, modify, and distribute this software for any purpose
519
- - You can use it in commercial projects
520
- - You must include the original copyright and license notice
521
-
522
- See the [LICENSE](LICENSE) file for the full license text.
523
-
524
- ---
525
-
526
- ## Credits
527
-
528
- This library is an original Ruby implementation inspired by the concepts of the
529
- Python Rich library by Will McGugan. All code is original and written specifically
530
- for Ruby.
531
-
532
- Developed on Ruby 3.4.8 (MSVC) on Windows 10 64-bit (21H2) with Visual Studio 2026.
533
-
534
- ---
535
-
536
- ## Contributing
537
-
538
- Contributions are welcome! Please:
539
-
540
- 1. Fork the repository
541
- 2. Create a feature branch
542
- 3. Make your changes
543
- 4. Add tests for new functionality
544
- 5. Submit a pull request
545
-
546
- All contributions must be compatible with the MIT license.
1
+ # Rich Ruby (rich-ruby)
2
+
3
+ A Pure Ruby library for rich text and beautiful formatting in the terminal.
4
+
5
+ Rich Ruby provides an elegant API for creating stylish terminal output with colors,
6
+ tables, panels, trees, progress bars, syntax highlighting, and more. It is inspired
7
+ by the Python Rich library but is a complete Ruby-native implementation.
8
+
9
+ ---
10
+
11
+ ## Table of Contents
12
+
13
+ 1. [Features](#features)
14
+ 2. [Requirements](#requirements)
15
+ 3. [Installation](#installation)
16
+ 4. [Quick Start](#quick-start)
17
+ 5. [Usage Guide](#usage-guide)
18
+ - [Colors and Styles](#colors-and-styles)
19
+ - [Text and Markup](#text-and-markup)
20
+ - [Panels](#panels)
21
+ - [Tables](#tables)
22
+ - [Trees](#trees)
23
+ - [Progress Bars and Spinners](#progress-bars-and-spinners)
24
+ - [Syntax Highlighting](#syntax-highlighting)
25
+ - [Markdown Rendering](#markdown-rendering)
26
+ - [JSON Output](#json-output)
27
+ 6. [API Reference](#api-reference)
28
+ 7. [Testing](#testing)
29
+ 8. [License](#license)
30
+
31
+ ---
32
+
33
+ ## Features
34
+
35
+ - **Colors**: Full support for 16-color, 256-color, and TrueColor (24-bit) terminals
36
+ - **Styles**: Bold, italic, underline, strikethrough, blink, reverse, and more
37
+ - **Markup**: Simple markup syntax like `[bold red]text[/]` for inline styling
38
+ - **Panels**: Bordered boxes with titles and subtitles
39
+ - **Tables**: Data tables with column alignment and styling
40
+ - **Trees**: Hierarchical tree views with different guide styles
41
+ - **Progress**: Animated progress bars and spinners
42
+ - **Syntax**: Code syntax highlighting for Ruby, Python, JavaScript, SQL, and more
43
+ - **Markdown**: Render Markdown documents in the terminal
44
+ - **Windows**: Full Windows Console API support with automatic ANSI enabling
45
+ - **Zero Dependencies**: Pure Ruby with no external gem dependencies
46
+
47
+ ---
48
+
49
+ ## Requirements
50
+
51
+ This library was developed and tested on:
52
+
53
+ - **Ruby**: 3.4.8 (MSVC build)
54
+ - **Platform**: Windows 10 64-bit (21H2)
55
+ - **Compiler**: Visual Studio 2026 (MSVC)
56
+
57
+ The library should work on:
58
+
59
+ - Ruby 3.4 or later (see `required_ruby_version` in the gemspec)
60
+ - Windows, macOS, Linux
61
+ - Any terminal supporting ANSI escape codes
62
+
63
+ ### Windows Native Support
64
+
65
+ For Windows users, the library provides **native integration** with the Windows Console API using [Fiddle](https://ruby-doc.org/stdlib/libdoc/fiddle/rdoc/Fiddle.html). This allows it to:
66
+
67
+ 1. **Enable ANSI/VT Processing**: Automatically configures the console to handle ANSI escape codes, even on older versions of Windows 10.
68
+ 2. **Fallback to Console API**: On legacy systems where ANSI is not supported, it uses the Windows Console API (e.g., `SetConsoleTextAttribute`) to provide color and styling.
69
+ 3. **Accurate Console Dimensions**: Uses native API calls to determine the exact width and height of the console window.
70
+ 4. **Hardware Features**: Full control over cursor visibility, window titles, and screen clearing via native calls.
71
+
72
+ This works out-of-the-box with Windows Terminal, PowerShell, and the classic Command Prompt.
73
+
74
+ ---
75
+
76
+ ## Installation
77
+
78
+ ### From RubyGems
79
+
80
+ ```bash
81
+ gem install rich-ruby
82
+ ```
83
+
84
+ ### From source
85
+
86
+ ```bash
87
+ git clone https://github.com/tigel-agm/rich-ruby.git
88
+ cd rich-ruby
89
+ gem build rich-ruby.gemspec
90
+ gem install ./rich-ruby-*.gem
91
+ ```
92
+
93
+ ### In your Gemfile
94
+
95
+ ```ruby
96
+ gem 'rich-ruby'
97
+ ```
98
+
99
+ ---
100
+
101
+ ## Quick Start
102
+
103
+ ```ruby
104
+ require 'rich'
105
+
106
+ # Simple styled output
107
+ Rich.print("[bold cyan]Hello[/] [yellow]World![/]")
108
+
109
+ # Create a console instance for more control
110
+ console = Rich::Console.new
111
+ console.print("Welcome!", style: "bold green")
112
+
113
+ # Display a panel
114
+ panel = Rich::Panel.new(
115
+ "This is important information.",
116
+ title: "Notice",
117
+ border_style: "cyan"
118
+ )
119
+ puts panel.render(max_width: 50)
120
+
121
+ # Display a table
122
+ table = Rich::Table.new(title: "Users")
123
+ table.add_column("Name", header_style: "bold")
124
+ table.add_column("Role")
125
+ table.add_row("Alice", "Admin")
126
+ table.add_row("Bob", "User")
127
+ puts table.render(max_width: 40)
128
+ ```
129
+
130
+ ---
131
+
132
+ ## Documentation
133
+
134
+ For more detailed information, check out our guides:
135
+
136
+ - [**How-To Use**](docs/how-to-use.md): Tiered guide for all levels.
137
+ - [**Troubleshooting & FAQ**](docs/troubleshooting.md): Solutions for common issues.
138
+ - [**Customization**](docs/customization.md): Learn how to extend the library.
139
+ - [**Cheat Sheet**](docs/cheat-sheet.md): Quick reference for styles and colors.
140
+ - [**Windows Notes**](docs/windows-notes.md): Technical details on Windows support.
141
+ - [**Architecture**](docs/architecture.md): Internal design and data flow.
142
+ - [**Test Report**](docs/test-report.md): Verification and performance results.
143
+
144
+ ---
145
+
146
+ ## Usage Guide
147
+
148
+ ### Colors and Styles
149
+
150
+ Rich Ruby supports multiple color systems:
151
+
152
+ ```ruby
153
+ # Standard 16 colors
154
+ style = Rich::Style.parse("red")
155
+ style = Rich::Style.parse("bright_blue")
156
+
157
+ # 256-color palette
158
+ style = Rich::Style.parse("color(42)")
159
+
160
+ # TrueColor (24-bit)
161
+ style = Rich::Style.parse("#ff5500")
162
+ style = Rich::Style.parse("rgb(255, 85, 0)")
163
+
164
+ # Background colors
165
+ style = Rich::Style.parse("white on blue")
166
+ style = Rich::Style.parse("black on #ffcc00")
167
+ ```
168
+
169
+ Available text attributes:
170
+
171
+ | Attribute | Description |
172
+ |-----------|-------------|
173
+ | `bold` | Bold text |
174
+ | `dim` | Dimmed text |
175
+ | `italic` | Italic text |
176
+ | `underline` | Underlined text |
177
+ | `underline2` | Double underline |
178
+ | `overline` | Overlined text |
179
+ | `blink` | Blinking text |
180
+ | `reverse` | Reversed colors |
181
+ | `conceal` | Hidden text |
182
+ | `strike` | Strikethrough |
183
+
184
+ Combine multiple attributes:
185
+
186
+ ```ruby
187
+ style = Rich::Style.parse("bold italic red on white")
188
+ ```
189
+
190
+ ### Text and Markup
191
+
192
+ The markup syntax provides an easy way to style text inline:
193
+
194
+ ```ruby
195
+ # Basic markup
196
+ Rich.print("[bold]Bold text[/bold]")
197
+ Rich.print("[red]Red text[/red]")
198
+
199
+ # Shorthand close tag
200
+ Rich.print("[bold]Bold text[/]")
201
+
202
+ # Nested styles
203
+ Rich.print("[bold][red]Bold and red[/red][/bold]")
204
+
205
+ # Combined styles
206
+ Rich.print("[bold italic cyan on black]Styled[/]")
207
+
208
+ # In strings
209
+ text = Rich::Markup.parse("Name: [cyan]Alice[/] Age: [yellow]30[/]")
210
+ puts Rich::Segment.render(text.to_segments)
211
+ ```
212
+
213
+ ### Panels
214
+
215
+ Create bordered panels to highlight content:
216
+
217
+ ```ruby
218
+ # Simple panel
219
+ panel = Rich::Panel.new("Hello World")
220
+ puts panel.render(max_width: 40)
221
+
222
+ # Panel with title and subtitle
223
+ panel = Rich::Panel.new(
224
+ "Important information goes here.",
225
+ title: "Alert",
226
+ subtitle: "Read carefully",
227
+ border_style: "red",
228
+ title_style: "bold white"
229
+ )
230
+ puts panel.render(max_width: 50)
231
+
232
+ # Different box styles
233
+ panel = Rich::Panel.new("Content", box: Rich::Box::DOUBLE)
234
+ panel = Rich::Panel.new("Content", box: Rich::Box::ROUNDED)
235
+ panel = Rich::Panel.new("Content", box: Rich::Box::HEAVY)
236
+ panel = Rich::Panel.new("Content", box: Rich::Box::ASCII)
237
+ ```
238
+
239
+ ### Tables
240
+
241
+ Create formatted data tables:
242
+
243
+ ```ruby
244
+ # Create a table
245
+ table = Rich::Table.new(title: "Sales Report", border_style: "blue")
246
+
247
+ # Add columns with styling
248
+ table.add_column("Product", header_style: "bold cyan")
249
+ table.add_column("Price", justify: :right, header_style: "bold cyan")
250
+ table.add_column("Quantity", justify: :center)
251
+
252
+ # Add data rows
253
+ table.add_row("Widget", "$10.00", "100")
254
+ table.add_row("Gadget", "$25.50", "50")
255
+ table.add_row("Gizmo", "$5.99", "200")
256
+
257
+ # Render
258
+ puts table.render(max_width: 60)
259
+ ```
260
+
261
+ Column justification options: `:left`, `:center`, `:right`
262
+
263
+ ### Trees
264
+
265
+ Display hierarchical data:
266
+
267
+ ```ruby
268
+ # Create a tree
269
+ tree = Rich::Tree.new("Project", style: "bold yellow")
270
+
271
+ # Add nodes
272
+ src = tree.add("src/", style: "bold")
273
+ src.add("main.rb", style: "green")
274
+ src.add("config.rb", style: "green")
275
+
276
+ lib = tree.add("lib/")
277
+ lib.add("utils.rb")
278
+
279
+ tree.add("README.md", style: "cyan")
280
+
281
+ # Render
282
+ puts tree.render
283
+ ```
284
+
285
+ Different guide styles:
286
+
287
+ ```ruby
288
+ tree = Rich::Tree.new("Root", guide: Rich::TreeGuide::ASCII)
289
+ tree = Rich::Tree.new("Root", guide: Rich::TreeGuide::ROUNDED)
290
+ tree = Rich::Tree.new("Root", guide: Rich::TreeGuide::BOLD)
291
+ ```
292
+
293
+ ### Progress Bars and Spinners
294
+
295
+ Show progress for long-running tasks:
296
+
297
+ ```ruby
298
+ # Progress bar
299
+ bar = Rich::ProgressBar.new(total: 100, width: 40)
300
+
301
+ 100.times do |i|
302
+ bar.update(i + 1)
303
+ print "\rProgress: #{bar.render}"
304
+ sleep(0.05)
305
+ end
306
+ puts ""
307
+
308
+ # Spinner
309
+ spinner = Rich::Spinner.new(frames: Rich::ProgressStyle::DOTS)
310
+
311
+ 20.times do
312
+ print "\r#{spinner.frame} Loading..."
313
+ spinner.advance
314
+ sleep(0.1)
315
+ end
316
+ puts "\rDone! "
317
+ ```
318
+
319
+ Available spinner styles:
320
+ - `Rich::ProgressStyle::DOTS`
321
+ - `Rich::ProgressStyle::LINE`
322
+ - `Rich::ProgressStyle::CIRCLE`
323
+ - `Rich::ProgressStyle::BOUNCE`
324
+
325
+ ### Syntax Highlighting
326
+
327
+ Highlight source code:
328
+
329
+ ```ruby
330
+ code = <<~RUBY
331
+ def greet(name)
332
+ puts "Hello, #{name}!"
333
+ end
334
+ RUBY
335
+
336
+ # Basic highlighting
337
+ syntax = Rich::Syntax.new(code, language: "ruby")
338
+ puts syntax.render
339
+
340
+ # With line numbers
341
+ syntax = Rich::Syntax.new(code, language: "ruby", line_numbers: true)
342
+ puts syntax.render
343
+
344
+ # With a theme
345
+ syntax = Rich::Syntax.new(code, language: "python", theme: :monokai)
346
+ syntax = Rich::Syntax.new(code, language: "javascript", theme: :dracula)
347
+ ```
348
+
349
+ Supported languages: Ruby, Python, JavaScript, SQL, JSON, YAML, Bash
350
+
351
+ Available themes: `:default`, `:monokai`, `:dracula`
352
+
353
+ ### Markdown Rendering
354
+
355
+ Render Markdown in the terminal:
356
+
357
+ ```ruby
358
+ markdown = <<~MD
359
+ # Welcome
360
+
361
+ This is **bold** and *italic* text.
362
+
363
+ ## Features
364
+
365
+ - Item one
366
+ - Item two
367
+
368
+ ```ruby
369
+ puts "Code block"
370
+ ```
371
+
372
+ | Column A | Column B |
373
+ |----------|----------|
374
+ | Value 1 | Value 2 |
375
+ MD
376
+
377
+ md = Rich::Markdown.new(markdown)
378
+ puts md.render(max_width: 70)
379
+ ```
380
+
381
+ Supported Markdown elements:
382
+ - Headings (H1-H6)
383
+ - Bold, italic, strikethrough
384
+ - Inline code
385
+ - Code blocks with language
386
+ - Ordered and unordered lists
387
+ - Blockquotes
388
+ - Tables
389
+ - Links
390
+ - Horizontal rules
391
+
392
+ ### JSON Output
393
+
394
+ Pretty-print JSON with syntax highlighting:
395
+
396
+ ```ruby
397
+ data = {
398
+ "name" => "Alice",
399
+ "age" => 30,
400
+ "active" => true,
401
+ "roles" => ["admin", "user"]
402
+ }
403
+
404
+ puts Rich::JSON.to_s(data)
405
+
406
+ # Highlight existing JSON string (highlight returns segments)
407
+ json_str = '{"key": "value"}'
408
+ puts Rich::Segment.render(Rich::JSON.highlight(json_str))
409
+ ```
410
+
411
+ ---
412
+
413
+ ## API Reference
414
+
415
+ ### Core Classes
416
+
417
+ | Class | Description |
418
+ |-------|-------------|
419
+ | `Rich::Console` | Main console interface |
420
+ | `Rich::Color` | Color representation and parsing |
421
+ | `Rich::Style` | Text style with color and attributes |
422
+ | `Rich::Segment` | Styled text segment |
423
+ | `Rich::Text` | Rich text with spans |
424
+ | `Rich::Markup` | Markup parser |
425
+
426
+ ### Components
427
+
428
+ | Class | Description |
429
+ |-------|-------------|
430
+ | `Rich::Panel` | Bordered panel |
431
+ | `Rich::Table` | Data table |
432
+ | `Rich::Tree` | Tree view |
433
+ | `Rich::ProgressBar` | Progress bar |
434
+ | `Rich::Spinner` | Animated spinner |
435
+ | `Rich::Syntax` | Syntax highlighter |
436
+ | `Rich::Markdown` | Markdown renderer |
437
+ | `Rich::JSON` | JSON formatter |
438
+
439
+ ### Module Methods
440
+
441
+ ```ruby
442
+ Rich.print(*objects, style: nil) # Print with markup support
443
+ Rich.print_json(data) # Print formatted JSON
444
+ Rich.rule(title) # Print horizontal rule
445
+ Rich.get_console # Get global console instance
446
+ Rich.reconfigure(**options) # Reconfigure global console
447
+ ```
448
+
449
+ ---
450
+
451
+ ## Testing
452
+
453
+ Run the full test suite:
454
+
455
+ ```bash
456
+ cd rich-ruby
457
+ ruby -W0 -Ilib -Itest -e "Dir['test/test_*.rb'].each { |f| require_relative f }"
458
+ ```
459
+
460
+ > [!TIP]
461
+ > Use the `-W0` flag to suppress environmental warnings (like the `io-nonblock` extension warning) for a cleaner test output.
462
+
463
+ Or with Rake (if rake is available):
464
+
465
+ ```bash
466
+ rake test
467
+ ```
468
+
469
+ Run individual test files:
470
+
471
+ ```bash
472
+ ruby -W0 -Ilib -Itest test/test_color.rb
473
+ ruby -W0 -Ilib -Itest test/test_style.rb
474
+ ```
475
+
476
+ Run stress tests:
477
+
478
+ ```bash
479
+ ruby examples/stress_test.rb
480
+ ```
481
+
482
+ ---
483
+
484
+ ## Project Structure
485
+
486
+ ```
487
+ rich/
488
+ lib/
489
+ rich.rb # Main entry point
490
+ rich/
491
+ color.rb # Color handling
492
+ style.rb # Text styles
493
+ text.rb # Rich text
494
+ markup.rb # Markup parser
495
+ panel.rb # Panel component
496
+ table.rb # Table component
497
+ tree.rb # Tree component
498
+ progress.rb # Progress bars and spinners
499
+ syntax.rb # Syntax highlighting
500
+ markdown.rb # Markdown rendering
501
+ json.rb # JSON formatting
502
+ console.rb # Console interface
503
+ ...
504
+ test/
505
+ test_*.rb # Test files
506
+ examples/
507
+ demo.rb # Basic demo
508
+ showcase.rb # Interactive showcase
509
+ stress_test.rb # Stress tests
510
+ ```
511
+
512
+ ---
513
+
514
+ ## License
515
+
516
+ This project is licensed under the MIT License.
517
+
518
+ This means:
519
+ - You can use, modify, and distribute this software for any purpose
520
+ - You can use it in commercial projects
521
+ - You must include the original copyright and license notice
522
+
523
+ See the [LICENSE](LICENSE) file for the full license text.
524
+
525
+ ---
526
+
527
+ ## Credits
528
+
529
+ This library is an original Ruby implementation inspired by the concepts of the
530
+ Python Rich library by Will McGugan [https://github.com/willmcgugan]. All code is original and written specifically
531
+ for Ruby.
532
+
533
+ Developed on Ruby 3.4.8 (MSVC) on Windows 10 64-bit (21H2) with Visual Studio 2026.
534
+
535
+ ---
536
+
537
+ ## Contributing
538
+
539
+ Contributions are welcome! Please:
540
+
541
+ 1. Fork the repository
542
+ 2. Create a feature branch
543
+ 3. Make your changes
544
+ 4. Add tests for new functionality
545
+ 5. Submit a pull request
546
+
547
+ All contributions must be compatible with the MIT license.