ragnetto-console 0.1.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 +127 -0
- data/lib/ragnetto/console/version.rb +7 -0
- data/lib/ragnetto/console.rb +228 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9493996b7bfe01888c558ac3934500a36ed9dacef3c724cf38d857f841b297b9
|
|
4
|
+
data.tar.gz: 99cb2949d29d83cb68cedc6ccf9e4e859058d398777d211db3cd84c28f86702d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: efc4d6e2a5e14e4d4fcab92e1f7928f7026b392189d3b43607d5b97bbaca6328680bb2db16b4b6ebdea86c20e4608127678248da6f1cfc9c479b2d709b15c4c2
|
|
7
|
+
data.tar.gz: ecac94ea8cb5b813f89274d0a31cf505d5145d7797ba3a1a7c6bd2b950fb6c8442b40fffc9f046d50017fddc76b992dc26ebd8e2ebfa8d7e89619f3176b37654
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ragnetto(R)
|
|
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,127 @@
|
|
|
1
|
+
# 🕷️ ragnetto-console
|
|
2
|
+
|
|
3
|
+
**Ragnetto-Console** is a lightweight Ruby utility for terminal manipulation using ANSI escape sequences
|
|
4
|
+
|
|
5
|
+
It provides an easy-to-use interface for colors, cursor positioning, and non-blocking input handling.
|
|
6
|
+
|
|
7
|
+
This module is part of the ragnetto software suite.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## 🚀 Features
|
|
14
|
+
|
|
15
|
+
* **ANSI Colors** : Full support for 16 foreground and background colors.
|
|
16
|
+
* **Cursor Control** : Move (X, Y), toggle visibility (ON/OFF), and change shapes (Block, Bar, Underline).
|
|
17
|
+
* **Advanced Input** : Read single keys immediately without pressing Enter (Non-blocking input) with or without Echo.
|
|
18
|
+
* **Cross-platform** : Support for Unix-like systems (Linux/macOS) and Windows (via PowerShell integration).
|
|
19
|
+
* **Terminal Info** : Auto-detect console width and height.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## 📦 Installation
|
|
26
|
+
|
|
27
|
+
Install the module via [RubyGems](https://rubygems.org):
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
gem install ragnetto-console
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or add it to your `Gemfile`:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
gem 'ragnetto-console'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## 🛠️ Quick Start
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
require 'ragnetto-console'
|
|
46
|
+
|
|
47
|
+
# Alias for convenience
|
|
48
|
+
C = Ragnetto::Console
|
|
49
|
+
|
|
50
|
+
# Clear screen and set window title
|
|
51
|
+
C.clear
|
|
52
|
+
C.title("Ragnetto-Console Demo")
|
|
53
|
+
|
|
54
|
+
# Write colored text at specific coordinates (X, Y)
|
|
55
|
+
C.write("Welcome to Ragnetto-Console!", "LIGHT_GREEN", "BLACK", 10, 5)
|
|
56
|
+
|
|
57
|
+
# Read a key without waiting for Enter
|
|
58
|
+
print "\nPress any key to exit... "
|
|
59
|
+
$stdout.flush
|
|
60
|
+
C.getkey
|
|
61
|
+
|
|
62
|
+
# Reset terminal styles to default
|
|
63
|
+
C.reset
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
## 📖 API Reference
|
|
70
|
+
|
|
71
|
+
| Function | Description |
|
|
72
|
+
| :--- | :--- |
|
|
73
|
+
| `console.clear()` | Clears the screen and resets the cursor to the home position. |
|
|
74
|
+
| `console.backcolor()` | Set the background text color. |
|
|
75
|
+
| `console.forecolor()` | Set the foreground text color. |
|
|
76
|
+
| `console.cursor(state)` | Toggles cursor visibility. |
|
|
77
|
+
| `console.caret(shape)` | Changes the cursor shape. |
|
|
78
|
+
| `console.position(x, y)` | Moves the cursor to the specified coordinates. |
|
|
79
|
+
| `console.write(text, [f], [b], [x], [y])` | Writes text with optional colors and coordinates. |
|
|
80
|
+
| `console.getkey()` | Reads a single keypress immediately (No-echo). |
|
|
81
|
+
| `console.putkey()` | Reads a single keypress and prints it (Echo). |
|
|
82
|
+
| `console.title(text)` | Sets the terminal window title. |
|
|
83
|
+
| `console.width()` | Returns the current terminal width. |
|
|
84
|
+
| `console.height()` | Returns the current terminal height. |
|
|
85
|
+
| `console.reset()` | Resets all styles, colors, and cursor states. |
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
## 💎 Constants
|
|
89
|
+
|
|
90
|
+
### 🎨 Colors
|
|
91
|
+
|
|
92
|
+
You can use these strings for foreground (`f`) and background (`b`) parameters:
|
|
93
|
+
|
|
94
|
+
* **Standard**: `BLACK`, `RED`, `GREEN`, `YELLOW`, `BLUE`, `MAGENTA`, `CYAN`, `WHITE`
|
|
95
|
+
* **Bright**: `GRAY`, `LIGHT_RED`, `LIGHT_GREEN`, `LIGHT_YELLOW`, `LIGHT_BLUE`, `LIGHT_MAGENTA`, `LIGHT_CYAN`, `LIGHT_WHITE`
|
|
96
|
+
|
|
97
|
+
### 🖱️ Caret Shapes
|
|
98
|
+
|
|
99
|
+
Values for the `console.caret(shape)` function:
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
| Constant | Description |
|
|
103
|
+
| :--- | :--- |
|
|
104
|
+
| `BLOCK_BLINK` | Blinking block cursor |
|
|
105
|
+
| `BLOCK_STEADY` | Static block cursor |
|
|
106
|
+
| `UNDERLINE_BLINK` | Blinking underline cursor |
|
|
107
|
+
| `UNDERLINE_STEADY` | Static underline cursor |
|
|
108
|
+
| `BAR_BLINK` | Blinking vertical bar |
|
|
109
|
+
| `BAR_STEADY` | Static vertical bar |
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
### ⚙️ States
|
|
113
|
+
|
|
114
|
+
Values for `console.cursor(state)`:
|
|
115
|
+
* `1` or `"ON"`: Show cursor.
|
|
116
|
+
* `0` or `"OFF"`: Hide cursor.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
## 📝 License
|
|
122
|
+
|
|
123
|
+
Distributed under the MIT License. See LICENSE for more information.
|
|
124
|
+
|
|
125
|
+
Author: [ragnetto-gab](https://github.com/ragnetto-gab) (Ragnetto®)
|
|
126
|
+
|
|
127
|
+
E-Mail: [ragnettosoftware@gmail.com](mailto:ragnettosoftware@gmail.com)
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# ------------------------------------------------------------------------------
|
|
2
|
+
# Ragnetto-Console
|
|
3
|
+
# ------------------------------------------------------------------------------
|
|
4
|
+
# File : console.rb
|
|
5
|
+
# Description : ANSI console control utility
|
|
6
|
+
# Language : Ruby
|
|
7
|
+
# ------------------------------------------------------------------------------
|
|
8
|
+
# Project : ragnetto-console
|
|
9
|
+
# Author : Gabriele Secci
|
|
10
|
+
# Editor : Ragnetto(R) Software
|
|
11
|
+
# E-Mail : ragnettosoftware@gmail.com
|
|
12
|
+
# ------------------------------------------------------------------------------
|
|
13
|
+
# Notes
|
|
14
|
+
# - This module provides terminal manipulation functions.
|
|
15
|
+
# - It is part of the Ragnetto module suite.
|
|
16
|
+
# ------------------------------------------------------------------------------
|
|
17
|
+
# Copyright (C) 2026 - All Rights Reserved
|
|
18
|
+
# ------------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
require_relative "console/version"
|
|
21
|
+
|
|
22
|
+
module Ragnetto
|
|
23
|
+
module Console
|
|
24
|
+
# -----------------------------------------------------------------------------
|
|
25
|
+
# CORE CONSTANTS
|
|
26
|
+
# -----------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
COLORS = {
|
|
29
|
+
"BLACK" => 0, "RED" => 1, "GREEN" => 2, "YELLOW" => 3,
|
|
30
|
+
"BLUE" => 4, "MAGENTA" => 5, "CYAN" => 6, "WHITE" => 7,
|
|
31
|
+
"GRAY" => 8, "LIGHT_RED" => 9, "LIGHT_GREEN" => 10, "LIGHT_YELLOW" => 11,
|
|
32
|
+
"LIGHT_BLUE" => 12, "LIGHT_MAGENTA" => 13, "LIGHT_CYAN" => 14, "LIGHT_WHITE" => 15
|
|
33
|
+
}.freeze
|
|
34
|
+
|
|
35
|
+
STATE = {
|
|
36
|
+
"OFF" => 0,
|
|
37
|
+
"ON" => 1
|
|
38
|
+
}.freeze
|
|
39
|
+
|
|
40
|
+
SHAPES = {
|
|
41
|
+
"BLOCK_BLINK" => 1,
|
|
42
|
+
"BLOCK_STEADY" => 2,
|
|
43
|
+
"UNDERLINE_BLINK" => 3,
|
|
44
|
+
"UNDERLINE_STEADY" => 4,
|
|
45
|
+
"BAR_BLINK" => 5,
|
|
46
|
+
"BAR_STEADY" => 6
|
|
47
|
+
}.freeze
|
|
48
|
+
|
|
49
|
+
# -----------------------------------------------------------------------------
|
|
50
|
+
# CORE FUNCTIONS
|
|
51
|
+
# -----------------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
# Clears the screen completely and resets the cursor
|
|
54
|
+
def self.clear
|
|
55
|
+
print "\e[2J\e[H"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Set the background color (0-15)
|
|
59
|
+
def self.backcolor(color)
|
|
60
|
+
c = color.is_a?(String) ? COLORS[color.upcase] : color
|
|
61
|
+
return unless c
|
|
62
|
+
|
|
63
|
+
if c < 8
|
|
64
|
+
print "\e[#{40 + c}m"
|
|
65
|
+
else
|
|
66
|
+
print "\e[#{100 + (c - 8)}m"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Set the text color (0-15)
|
|
71
|
+
def self.forecolor(color)
|
|
72
|
+
c = color.is_a?(String) ? COLORS[color.upcase] : color
|
|
73
|
+
return unless c
|
|
74
|
+
|
|
75
|
+
if c < 8
|
|
76
|
+
print "\e[#{30 + c}m"
|
|
77
|
+
else
|
|
78
|
+
print "\e[#{90 + (c - 8)}m"
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Manages the visibility of the cursor
|
|
83
|
+
def self.cursor(state)
|
|
84
|
+
s = state.is_a?(String) ? STATE[state.upcase] : state
|
|
85
|
+
|
|
86
|
+
if s == 1 || s == true || s.to_s.upcase == "ON"
|
|
87
|
+
print "\e[?25h"
|
|
88
|
+
else
|
|
89
|
+
print "\e[?25l"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Change the shape of the cursor (Caret)
|
|
94
|
+
def self.caret(shape)
|
|
95
|
+
s = shape.is_a?(String) ? SHAPES[shape.upcase] : shape
|
|
96
|
+
|
|
97
|
+
print "\e[#{s} q" if s && s >= 1 && s <= 6
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Move the cursor to a specific position
|
|
101
|
+
def self.position(x, y)
|
|
102
|
+
print "\e[#{y};#{x}H"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Writes text with attributes and positioning
|
|
106
|
+
def self.write(text, fore = nil, back = nil, x = nil, y = nil)
|
|
107
|
+
out = ""
|
|
108
|
+
|
|
109
|
+
out << "\e[#{y};#{x}H" if x && y
|
|
110
|
+
|
|
111
|
+
if back
|
|
112
|
+
c_back = back.is_a?(String) ? COLORS[back.upcase] : back
|
|
113
|
+
out << "\e[#{c_back < 8 ? 40 + c_back : 100 + (c_back - 8)}m" if c_back
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
if fore
|
|
117
|
+
c_fore = fore.is_a?(String) ? COLORS[fore.upcase] : fore
|
|
118
|
+
out << "\e[#{c_fore < 8 ? 30 + c_fore : 90 + (c_fore - 8)}m" if c_fore
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
out << text.to_s
|
|
122
|
+
out << "\e[0m"
|
|
123
|
+
|
|
124
|
+
print out
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Reads a single character without echoing
|
|
128
|
+
def self.getkey
|
|
129
|
+
if Gem.win_platform?
|
|
130
|
+
cmd = '[console]::ReadKey($true).KeyChar'
|
|
131
|
+
pipe = IO.popen(['powershell', '-Command', cmd], 'r')
|
|
132
|
+
char = pipe.read
|
|
133
|
+
|
|
134
|
+
pipe.close
|
|
135
|
+
|
|
136
|
+
char ? char.strip : ""
|
|
137
|
+
else
|
|
138
|
+
system("stty -icanon -echo")
|
|
139
|
+
char = $stdin.read(1)
|
|
140
|
+
system("stty icanon echo")
|
|
141
|
+
|
|
142
|
+
char
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Reads a single character and prints it to the screen
|
|
147
|
+
def self.putkey
|
|
148
|
+
char = ""
|
|
149
|
+
|
|
150
|
+
if Gem.win_platform?
|
|
151
|
+
cmd = '[console]::ReadKey($true).KeyChar'
|
|
152
|
+
begin
|
|
153
|
+
pipe = IO.popen(['powershell', '-Command', cmd], 'r')
|
|
154
|
+
raw_out = pipe.read
|
|
155
|
+
|
|
156
|
+
pipe.close
|
|
157
|
+
|
|
158
|
+
char = raw_out ? raw_out.gsub(/[\r\n]/, '') : ""
|
|
159
|
+
|
|
160
|
+
unless char.empty?
|
|
161
|
+
print char
|
|
162
|
+
$stdout.flush
|
|
163
|
+
end
|
|
164
|
+
rescue
|
|
165
|
+
end
|
|
166
|
+
else
|
|
167
|
+
system("stty -icanon -echo")
|
|
168
|
+
char = $stdin.read(1)
|
|
169
|
+
system("stty icanon echo")
|
|
170
|
+
|
|
171
|
+
if char && !char.empty?
|
|
172
|
+
print char
|
|
173
|
+
$stdout.flush
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
char
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Set the terminal window title
|
|
181
|
+
def self.title(title_text)
|
|
182
|
+
return unless title_text.is_a?(String)
|
|
183
|
+
|
|
184
|
+
print "\e]0;#{title_text}\a"
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Gets the current width of the console (columns)
|
|
188
|
+
def self.width
|
|
189
|
+
if Gem.win_platform?
|
|
190
|
+
pipe = IO.popen('powershell -command "$host.ui.rawui.WindowSize.Width"')
|
|
191
|
+
w = pipe.read.to_i
|
|
192
|
+
pipe.close
|
|
193
|
+
w.zero? ? 80 : w
|
|
194
|
+
else
|
|
195
|
+
pipe = IO.popen("stty size 2>/dev/null")
|
|
196
|
+
res = pipe.read
|
|
197
|
+
pipe.close
|
|
198
|
+
res.empty? ? 80 : res.split[1].to_i
|
|
199
|
+
end
|
|
200
|
+
rescue
|
|
201
|
+
80
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Gets the current height of the console (rows)
|
|
205
|
+
def self.height
|
|
206
|
+
if Gem.win_platform?
|
|
207
|
+
pipe = IO.popen('powershell -command "$host.ui.rawui.WindowSize.Height"')
|
|
208
|
+
h = pipe.read.to_i
|
|
209
|
+
pipe.close
|
|
210
|
+
h.zero? ? 24 : h
|
|
211
|
+
else
|
|
212
|
+
pipe = IO.popen("stty size 2>/dev/null")
|
|
213
|
+
res = pipe.read
|
|
214
|
+
pipe.close
|
|
215
|
+
res.empty? ? 24 : res.split[0].to_i
|
|
216
|
+
end
|
|
217
|
+
rescue
|
|
218
|
+
24
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Reset all styles to default values
|
|
222
|
+
def self.reset
|
|
223
|
+
print "\e[0m"
|
|
224
|
+
print "\e[?25h"
|
|
225
|
+
print "\e[0 q"
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ragnetto-console
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Gabriele Secci
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: A pure Ruby module providing terminal manipulation functions. Part of
|
|
13
|
+
the Ragnetto module suite.
|
|
14
|
+
email:
|
|
15
|
+
- ragnettosoftware@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- LICENSE
|
|
21
|
+
- README.md
|
|
22
|
+
- lib/ragnetto/console.rb
|
|
23
|
+
- lib/ragnetto/console/version.rb
|
|
24
|
+
licenses:
|
|
25
|
+
- MIT
|
|
26
|
+
metadata: {}
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: 3.0.0
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubygems_version: 4.0.12
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: ANSI console control utility in pure Ruby.
|
|
44
|
+
test_files: []
|