rich-ruby 1.0.0
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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +546 -0
- data/examples/demo.rb +106 -0
- data/examples/showcase.rb +420 -0
- data/examples/smoke_test.rb +41 -0
- data/examples/stress_test.rb +604 -0
- data/examples/syntax_markdown_demo.rb +166 -0
- data/examples/verify.rb +215 -0
- data/examples/visual_demo.rb +145 -0
- data/lib/rich/_palettes.rb +148 -0
- data/lib/rich/box.rb +342 -0
- data/lib/rich/cells.rb +512 -0
- data/lib/rich/color.rb +628 -0
- data/lib/rich/color_triplet.rb +220 -0
- data/lib/rich/console.rb +549 -0
- data/lib/rich/control.rb +332 -0
- data/lib/rich/json.rb +254 -0
- data/lib/rich/layout.rb +314 -0
- data/lib/rich/markdown.rb +509 -0
- data/lib/rich/markup.rb +175 -0
- data/lib/rich/panel.rb +311 -0
- data/lib/rich/progress.rb +430 -0
- data/lib/rich/segment.rb +387 -0
- data/lib/rich/style.rb +433 -0
- data/lib/rich/syntax.rb +1145 -0
- data/lib/rich/table.rb +525 -0
- data/lib/rich/terminal_theme.rb +126 -0
- data/lib/rich/text.rb +433 -0
- data/lib/rich/tree.rb +220 -0
- data/lib/rich/version.rb +5 -0
- data/lib/rich/win32_console.rb +582 -0
- data/lib/rich.rb +108 -0
- metadata +106 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fa94457aad9a3aa12f96f641c46233988b4bdc69b6216ac13eec7f129d92ead8
|
|
4
|
+
data.tar.gz: '08c5c0db424bd981431c99c7acbb27607755f8873ee3f5385bdd5fd78b8d6c2c'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1490b22a9ba9f7e3b5e5f373b2919253c75c8a290679fb469c604033778b20d4e8bef0581318d11c316be2d7ac1a90a242172a3be648b9226585df51cd2e41af
|
|
7
|
+
data.tar.gz: 6465d77278e194c62c0eaabb1804d2fe6dda8d5009427cf41765bcc7380b6a3ad7b38debb3c2fd0aafbf4ea7e82687481e5fd99bd2bffba622a2c81a44c733ac
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Rich Ruby Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,546 @@
|
|
|
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.
|
data/examples/demo.rb
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Comprehensive demo of Rich library features
|
|
4
|
+
|
|
5
|
+
require_relative "../lib/rich"
|
|
6
|
+
|
|
7
|
+
console = Rich::Console.new
|
|
8
|
+
|
|
9
|
+
# Title
|
|
10
|
+
console.rule("Rich Library Demo", style: "bold magenta")
|
|
11
|
+
puts ""
|
|
12
|
+
|
|
13
|
+
# 1. Colors and Styles
|
|
14
|
+
puts "1. Colors and Styles"
|
|
15
|
+
puts "=" * 40
|
|
16
|
+
console.print("Normal text")
|
|
17
|
+
console.print("Bold text", style: "bold")
|
|
18
|
+
console.print("Italic text", style: "italic")
|
|
19
|
+
console.print("Underlined text", style: "underline")
|
|
20
|
+
console.print("Red text", style: "red")
|
|
21
|
+
console.print("Green on yellow", style: "green on yellow")
|
|
22
|
+
console.print("Bold blue italic", style: "bold blue italic")
|
|
23
|
+
puts ""
|
|
24
|
+
|
|
25
|
+
# 2. Text with Spans
|
|
26
|
+
puts "2. Text with Style Spans"
|
|
27
|
+
puts "=" * 40
|
|
28
|
+
text = Rich::Text.new("Hello ")
|
|
29
|
+
text.append("World", style: "bold red")
|
|
30
|
+
text.append("! This is ")
|
|
31
|
+
text.append("Rich", style: "italic magenta")
|
|
32
|
+
text.append(" for Ruby.")
|
|
33
|
+
puts text.render + "\n"
|
|
34
|
+
|
|
35
|
+
# 3. Markup
|
|
36
|
+
puts "3. Markup Parsing"
|
|
37
|
+
puts "=" * 40
|
|
38
|
+
markup = Rich::Markup.render("[bold]Bold[/bold] and [italic red]italic red[/italic red] text")
|
|
39
|
+
puts markup + "\n"
|
|
40
|
+
|
|
41
|
+
# 4. Panel
|
|
42
|
+
puts "4. Panel Component"
|
|
43
|
+
puts "=" * 40
|
|
44
|
+
panel = Rich::Panel.new(
|
|
45
|
+
"This is content inside a panel.\nIt can have multiple lines.",
|
|
46
|
+
title: "My Panel",
|
|
47
|
+
subtitle: "Subtitle here",
|
|
48
|
+
border_style: "cyan",
|
|
49
|
+
title_style: "bold white"
|
|
50
|
+
)
|
|
51
|
+
puts panel.render(max_width: 50)
|
|
52
|
+
puts ""
|
|
53
|
+
|
|
54
|
+
# 5. Table
|
|
55
|
+
puts "5. Table Component"
|
|
56
|
+
puts "=" * 40
|
|
57
|
+
table = Rich::Table.new(
|
|
58
|
+
title: "User Data",
|
|
59
|
+
show_header: true,
|
|
60
|
+
border_style: "blue"
|
|
61
|
+
)
|
|
62
|
+
table.add_column("Name", header_style: "bold")
|
|
63
|
+
table.add_column("Age", justify: :right)
|
|
64
|
+
table.add_column("City", header_style: "bold green")
|
|
65
|
+
|
|
66
|
+
table.add_row("Alice", "30", "New York")
|
|
67
|
+
table.add_row("Bob", "25", "San Francisco")
|
|
68
|
+
table.add_row("Charlie", "35", "London")
|
|
69
|
+
table.add_row("Diana", "28", "Tokyo")
|
|
70
|
+
|
|
71
|
+
puts table.render(max_width: 60)
|
|
72
|
+
puts ""
|
|
73
|
+
|
|
74
|
+
# 6. Box Styles
|
|
75
|
+
puts "6. Different Box Styles"
|
|
76
|
+
puts "=" * 40
|
|
77
|
+
|
|
78
|
+
[Rich::Box::ASCII, Rich::Box::SQUARE, Rich::Box::ROUNDED, Rich::Box::HEAVY, Rich::Box::DOUBLE].each do |box_style|
|
|
79
|
+
small_panel = Rich::Panel.new(
|
|
80
|
+
"Content",
|
|
81
|
+
title: box_style.class == Class ? box_style.name : "Box",
|
|
82
|
+
box: box_style,
|
|
83
|
+
expand: false,
|
|
84
|
+
padding: 0
|
|
85
|
+
)
|
|
86
|
+
puts small_panel.render(max_width: 30)
|
|
87
|
+
end
|
|
88
|
+
puts ""
|
|
89
|
+
|
|
90
|
+
# 7. Color System Info
|
|
91
|
+
puts "7. System Information"
|
|
92
|
+
puts "=" * 40
|
|
93
|
+
info_table = Rich::Table.new(show_header: false, box: Rich::Box::SIMPLE)
|
|
94
|
+
info_table.add_column("Property", style: "cyan")
|
|
95
|
+
info_table.add_column("Value", style: "yellow")
|
|
96
|
+
info_table.add_row("Color System", console.color_system.to_s)
|
|
97
|
+
info_table.add_row("Terminal", console.terminal? ? "Yes" : "No")
|
|
98
|
+
info_table.add_row("Width", console.width.to_s)
|
|
99
|
+
info_table.add_row("Height", console.height.to_s)
|
|
100
|
+
info_table.add_row("Ruby Version", RUBY_VERSION)
|
|
101
|
+
info_table.add_row("Platform", RUBY_PLATFORM)
|
|
102
|
+
|
|
103
|
+
puts info_table.render(max_width: 50)
|
|
104
|
+
puts ""
|
|
105
|
+
|
|
106
|
+
console.rule("Demo Complete!", style: "bold green")
|