pretty_irb 0.1.2 → 0.1.4

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.
@@ -11,6 +11,7 @@ module PrettyIRB
11
11
 
12
12
  # Save a code snippet
13
13
  def save(name, code, description = "")
14
+ return "❌ Invalid snippet name. Use only letters, numbers, underscores, and hyphens." unless valid_name?(name)
14
15
  filename = snippet_path(name)
15
16
 
16
17
  snippet = {
@@ -27,6 +28,7 @@ module PrettyIRB
27
28
 
28
29
  # Load a snippet
29
30
  def load(name)
31
+ return "❌ Invalid snippet name." unless valid_name?(name)
30
32
  filename = snippet_path(name)
31
33
  return "❌ Snippet '#{name}' not found" unless File.exist?(filename)
32
34
 
@@ -56,6 +58,7 @@ module PrettyIRB
56
58
  data = JSON.parse(File.read(file))
57
59
  data["name"].include?(keyword) ||
58
60
  data["code"].include?(keyword) ||
61
+ data["description"].to_s.include?(keyword) ||
59
62
  data["tags"].any? { |tag| tag.include?(keyword) }
60
63
  end
61
64
 
@@ -73,6 +76,7 @@ module PrettyIRB
73
76
 
74
77
  # Delete a snippet
75
78
  def delete(name)
79
+ return "❌ Invalid snippet name." unless valid_name?(name)
76
80
  filename = snippet_path(name)
77
81
  return "❌ Snippet '#{name}' not found" unless File.exist?(filename)
78
82
 
@@ -82,6 +86,7 @@ module PrettyIRB
82
86
 
83
87
  # Show snippet details
84
88
  def show(name)
89
+ return "❌ Invalid snippet name." unless valid_name?(name)
85
90
  filename = snippet_path(name)
86
91
  return "❌ Snippet '#{name}' not found" unless File.exist?(filename)
87
92
 
@@ -100,6 +105,10 @@ module PrettyIRB
100
105
  FileUtils.mkdir_p(SNIPPET_DIR) unless Dir.exist?(SNIPPET_DIR)
101
106
  end
102
107
 
108
+ def valid_name?(name)
109
+ name.to_s.match?(/\A[a-zA-Z0-9_-]+\z/)
110
+ end
111
+
103
112
  def snippet_path(name)
104
113
  File.join(SNIPPET_DIR, "#{name}.json")
105
114
  end
@@ -0,0 +1,88 @@
1
+ require "colorize"
2
+
3
+ module PrettyIRB
4
+ # Shared ANSI-aware terminal layout and styling utilities.
5
+ module UI
6
+ ANSI_REGEX = /\e\[[0-9;]*m/.freeze
7
+
8
+ module_function
9
+
10
+ def colorize?
11
+ return true unless defined?(PrettyIRB) && PrettyIRB.respond_to?(:configuration)
12
+
13
+ PrettyIRB.configuration.colorize
14
+ end
15
+
16
+ def strip_ansi(text)
17
+ text.to_s.gsub(ANSI_REGEX, "")
18
+ end
19
+
20
+ def visible_length(text)
21
+ strip_ansi(text).length
22
+ end
23
+
24
+ def pad_right(text, width)
25
+ padding = [width - visible_length(text), 0].max
26
+ "#{text}#{' ' * padding}"
27
+ end
28
+
29
+ def paint(text, *styles)
30
+ str = text.to_s
31
+ return str unless colorize?
32
+
33
+ styles.reduce(str) { |acc, style| acc.public_send(style) }
34
+ rescue
35
+ text.to_s
36
+ end
37
+
38
+ def rule(width = 62, char: "─")
39
+ paint(char * width, :light_black)
40
+ end
41
+
42
+ def badge(label, color: :cyan, bold: false)
43
+ styles = bold ? [color, :bold] : [color]
44
+ paint("[#{label}]", *styles)
45
+ end
46
+
47
+ def table_row(left, right, left_width: 34, indent: 4)
48
+ prefix = " " * indent
49
+ "#{prefix}#{pad_right(left, left_width)}#{right}"
50
+ end
51
+
52
+ # Renders a rounded box with ANSI-safe width calculation and optional title.
53
+ def box(lines, title: nil, min_width: 60, padding_x: 2)
54
+ content_lines = Array(lines).map { |line| line == :divider ? :divider : line.to_s }
55
+ text_lines = content_lines.reject { |line| line == :divider }
56
+ max_line_w = text_lines.map { |line| visible_length(line) }.max || 0
57
+ title_w = title ? visible_length(title) + 4 : 0
58
+ inner_width = [min_width, max_line_w + (padding_x * 2), title_w].max
59
+
60
+ top_border = build_top_border(inner_width, title)
61
+ bottom_border = paint("╰#{'─' * inner_width}╯", :light_black)
62
+ pad_str = " " * padding_x
63
+ v_bar = paint("│", :light_black)
64
+
65
+ body = content_lines.map do |line|
66
+ if line == :divider
67
+ paint("├#{'─' * inner_width}┤", :light_black)
68
+ else
69
+ padded = pad_right(line, inner_width - (padding_x * 2))
70
+ "#{v_bar}#{pad_str}#{padded}#{pad_str}#{v_bar}"
71
+ end
72
+ end
73
+
74
+ ([top_border] + body + [bottom_border]).join("\n")
75
+ end
76
+
77
+ def build_top_border(inner_width, title)
78
+ if title
79
+ label = " #{title} "
80
+ remaining = [inner_width - 1 - visible_length(label), 1].max
81
+ "#{paint('╭─', :light_black)}#{label}#{paint("#{'─' * remaining}╮", :light_black)}"
82
+ else
83
+ paint("╭#{'─' * inner_width}╮", :light_black)
84
+ end
85
+ end
86
+ private_class_method :build_top_border
87
+ end
88
+ end
@@ -1,3 +1,3 @@
1
1
  module PrettyIRB
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/pretty_irb.rb CHANGED
@@ -5,6 +5,8 @@ require "did_you_mean"
5
5
  require "reline"
6
6
 
7
7
  require_relative "pretty_irb/version"
8
+ require_relative "pretty_irb/configuration"
9
+ require_relative "pretty_irb/ui"
8
10
  require_relative "pretty_irb/formatter"
9
11
  require_relative "pretty_irb/auto_corrector"
10
12
  require_relative "pretty_irb/ai_helper"
@@ -13,12 +15,27 @@ require_relative "pretty_irb/snippet_manager"
13
15
  require_relative "pretty_irb/variable_inspector"
14
16
  require_relative "pretty_irb/benchmarker"
15
17
  require_relative "pretty_irb/cheat_sheet"
18
+ require_relative "pretty_irb/irb_commands"
16
19
  require_relative "pretty_irb/shell"
20
+ require_relative "pretty_irb/railtie" if defined?(Rails::Railtie)
17
21
 
18
22
  module PrettyIRB
19
23
  class Error < StandardError; end
20
24
 
21
25
  class << self
26
+ def configuration
27
+ @configuration ||= Configuration.new
28
+ end
29
+
30
+ def configure
31
+ yield(configuration)
32
+ configuration
33
+ end
34
+
35
+ def reset_configuration!
36
+ @configuration = Configuration.new
37
+ end
38
+
22
39
  def start(config = {})
23
40
  Shell.new(config).run
24
41
  end
data/pretty_irb.gemspec CHANGED
@@ -1,6 +1,8 @@
1
+ require_relative "lib/pretty_irb/version"
2
+
1
3
  Gem::Specification.new do |spec|
2
4
  spec.name = "pretty_irb"
3
- spec.version = "0.1.2"
5
+ spec.version = PrettyIRB::VERSION
4
6
  spec.authors = ["Jayesh"]
5
7
  spec.email = ["jayesh@example.com"]
6
8
  spec.summary = "A pretty IRB replacement with auto-correct and beautiful fonts"
@@ -37,6 +39,7 @@ Gem::Specification.new do |spec|
37
39
  # Allow any version >= 1.5 but < 3.0 to avoid conflicts with older constraints.
38
40
  spec.add_dependency "did_you_mean", ">= 1.5", "< 3.0" # Auto-correct suggestions
39
41
  spec.add_dependency "reline", "~> 0.3" # Enhanced readline
42
+ spec.add_dependency "benchmark" # Performance measurement
40
43
 
41
44
  # Development dependencies
42
45
  spec.add_development_dependency "bundler", "~> 2.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_irb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayesh
@@ -85,6 +85,20 @@ dependencies:
85
85
  - - "~>"
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0.3'
88
+ - !ruby/object:Gem::Dependency
89
+ name: benchmark
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ type: :runtime
96
+ prerelease: false
97
+ version_requirements: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
88
102
  - !ruby/object:Gem::Dependency
89
103
  name: bundler
90
104
  requirement: !ruby/object:Gem::Requirement
@@ -171,10 +185,14 @@ files:
171
185
  - lib/pretty_irb/auto_corrector.rb
172
186
  - lib/pretty_irb/benchmarker.rb
173
187
  - lib/pretty_irb/cheat_sheet.rb
188
+ - lib/pretty_irb/configuration.rb
174
189
  - lib/pretty_irb/formatter.rb
175
190
  - lib/pretty_irb/history_manager.rb
191
+ - lib/pretty_irb/irb_commands.rb
192
+ - lib/pretty_irb/railtie.rb
176
193
  - lib/pretty_irb/shell.rb
177
194
  - lib/pretty_irb/snippet_manager.rb
195
+ - lib/pretty_irb/ui.rb
178
196
  - lib/pretty_irb/variable_inspector.rb
179
197
  - lib/pretty_irb/version.rb
180
198
  - pretty_irb.gemspec