railsfinder 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/railsfinder/interface.rb +6 -113
- data/lib/railsfinder/schema_loader.rb +20 -0
- data/lib/railsfinder/schema_parser.rb +17 -0
- data/lib/railsfinder/table_selector.rb +104 -0
- data/lib/railsfinder/version.rb +1 -1
- data/lib/railsfinder.rb +4 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e69f78b357e24bf8a95dd1c10e1ed81124088a40f51d272d0cd1e9e7dca6e913
|
4
|
+
data.tar.gz: ea5b3ce83802715c82cfc06390babd23400be0e9acaa9f476b6d2798efa8366f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e6fee93a0bab5274f7d4586da2903a8fa2c12577f1c1b97404954f3390a553dcae3aabeac51679f5c790d54f0c2f1c2abe77cd8d65e3eab55de72cb773e2b75
|
7
|
+
data.tar.gz: 4726aa9b975f74f09c73334ca201f5a5e53b37857b1f71cdbedda1f3a3e2211715cd46e3daa1db20cfed0e9edac83d9aa315bbd5b2001b8f65b5745dbde5a96f
|
@@ -1,120 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require "tty-cursor"
|
4
|
-
require "tty-box"
|
1
|
+
require_relative 'table_selector'
|
2
|
+
require_relative 'schema_loader'
|
5
3
|
|
6
4
|
module RailsFinder
|
7
5
|
class Interface
|
8
|
-
SCHEMA_FILE_PATH = "db/schema.rb"
|
9
|
-
|
10
6
|
def self.start
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
data = load_schema_data
|
16
|
-
table_names = data.keys
|
17
|
-
filtered_table_names = table_names.dup
|
18
|
-
input = ""
|
19
|
-
|
20
|
-
render_layout(cursor, screen_size, filtered_table_names, selected_index, data, input)
|
21
|
-
|
22
|
-
begin
|
23
|
-
loop do
|
24
|
-
key = reader.read_keypress
|
25
|
-
case key
|
26
|
-
when "\e[A" # Up arrow
|
27
|
-
selected_index = (selected_index - 1) % filtered_table_names.length unless filtered_table_names.empty?
|
28
|
-
when "\e[B" # Down arrow
|
29
|
-
selected_index = (selected_index + 1) % filtered_table_names.length unless filtered_table_names.empty?
|
30
|
-
when "\r" # Enter
|
31
|
-
# Detail action
|
32
|
-
when "\e" # Escape key
|
33
|
-
break # Exit the loop, which will end the program
|
34
|
-
when "\u007F", "\b" # Backspace
|
35
|
-
input.chop!
|
36
|
-
else
|
37
|
-
input << key if key =~ /\A[\w\s]+\z/
|
38
|
-
end
|
39
|
-
|
40
|
-
filtered_table_names = table_names.select { |name| name.downcase.include?(input.downcase) }
|
41
|
-
selected_index = 0 if filtered_table_names.empty?
|
42
|
-
|
43
|
-
render_layout(cursor, screen_size, filtered_table_names, selected_index, data, input)
|
44
|
-
end
|
45
|
-
rescue TTY::Reader::InputInterrupt
|
46
|
-
system "clear"
|
47
|
-
puts "\nBye"
|
48
|
-
exit
|
49
|
-
ensure
|
50
|
-
system "clear"
|
51
|
-
puts "\nExiting RailsFinder..."
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.render_layout(cursor, screen_size, filtered_table_names, selected_index, data, input)
|
56
|
-
system "clear"
|
57
|
-
left_panel_width = screen_size[1] / 2
|
58
|
-
right_panel_width = screen_size[1] - left_panel_width
|
59
|
-
top_height = 3
|
60
|
-
|
61
|
-
puts cursor.move_to(0, 0)
|
62
|
-
print TTY::Box.frame(
|
63
|
-
top: 0,
|
64
|
-
left: 0,
|
65
|
-
width: left_panel_width,
|
66
|
-
height: top_height,
|
67
|
-
title: { top_left: " Search " },
|
68
|
-
border: :light,
|
69
|
-
) { input }
|
70
|
-
|
71
|
-
print TTY::Box.frame(
|
72
|
-
top: top_height,
|
73
|
-
left: 0,
|
74
|
-
width: left_panel_width,
|
75
|
-
height: screen_size[0] - top_height,
|
76
|
-
title: { top_left: " Select Table: #{selected_index + 1}/#{filtered_table_names.length} " },
|
77
|
-
border: :light,
|
78
|
-
) { filtered_table_names.map.with_index { |name, index| index == selected_index ? "=> #{name}" : " #{name}" }.join("\n") }
|
79
|
-
|
80
|
-
print TTY::Box.frame(
|
81
|
-
top: 0,
|
82
|
-
left: left_panel_width,
|
83
|
-
width: right_panel_width,
|
84
|
-
height: screen_size[0],
|
85
|
-
title: { top_left: " Details " },
|
86
|
-
border: :light,
|
87
|
-
) { data[filtered_table_names[selected_index]] || "" }
|
88
|
-
end
|
89
|
-
|
90
|
-
def self.load_schema_data
|
91
|
-
schema_path = find_schema_file
|
92
|
-
return {} unless schema_path && File.exist?(schema_path)
|
93
|
-
|
94
|
-
schema_content = File.read(schema_path)
|
95
|
-
parse_schema(schema_content)
|
96
|
-
end
|
97
|
-
|
98
|
-
def self.find_schema_file
|
99
|
-
# Attempts to locate the db/schema.rb file in the current directory
|
100
|
-
current_dir = Dir.pwd
|
101
|
-
schema_file = File.join(current_dir, SCHEMA_FILE_PATH)
|
102
|
-
|
103
|
-
File.exist?(schema_file) ? schema_file : nil
|
104
|
-
end
|
105
|
-
|
106
|
-
def self.parse_schema(schema_content)
|
107
|
-
tables = {}
|
108
|
-
|
109
|
-
# Adjust the scan to use capture parentheses correctly
|
110
|
-
schema_content.scan(/create_table "(.+?)"(.+?)end/m).each do |capture|
|
111
|
-
table_name = capture[0]
|
112
|
-
table_content = capture[1].strip
|
113
|
-
# Adds "\nend" to ensure 'end' is on a new line.
|
114
|
-
tables[table_name] = "create_table \"#{table_name}\"#{table_content}\nend"
|
115
|
-
end
|
116
|
-
|
117
|
-
tables
|
7
|
+
schema_loader = SchemaLoader.new
|
8
|
+
data = schema_loader.load_schema_data
|
9
|
+
table_selector = TableSelector.new(data)
|
10
|
+
table_selector.start
|
118
11
|
end
|
119
12
|
end
|
120
13
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RailsFinder
|
2
|
+
class SchemaLoader
|
3
|
+
SCHEMA_FILE_PATH = "db/schema.rb"
|
4
|
+
|
5
|
+
def load_schema_data
|
6
|
+
schema_path = find_schema_file
|
7
|
+
return {} unless schema_path && File.exist?(schema_path)
|
8
|
+
|
9
|
+
schema_content = File.read(schema_path)
|
10
|
+
SchemaParser.parse_schema(schema_content)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def find_schema_file
|
16
|
+
schema_file = File.join(Dir.pwd, SCHEMA_FILE_PATH)
|
17
|
+
File.exist?(schema_file) ? schema_file : nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RailsFinder
|
2
|
+
class SchemaParser
|
3
|
+
def self.parse_schema(schema_content)
|
4
|
+
tables = {}
|
5
|
+
schema_content.scan(/create_table ['"](.+?)['"](.+?)end/m).each do |table_name, table_content|
|
6
|
+
tables[table_name] = format_table(table_name, table_content.strip)
|
7
|
+
end
|
8
|
+
tables
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def self.format_table(name, content)
|
14
|
+
"create_table \"#{name}\" #{content}\nend"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'tty-reader'
|
2
|
+
require 'tty-screen'
|
3
|
+
require 'tty-cursor'
|
4
|
+
require 'tty-box'
|
5
|
+
|
6
|
+
module RailsFinder
|
7
|
+
class TableSelector
|
8
|
+
def initialize(data)
|
9
|
+
@data = data
|
10
|
+
@filtered_table_names = data.keys
|
11
|
+
@selected_index = 0
|
12
|
+
@input = ''
|
13
|
+
end
|
14
|
+
|
15
|
+
def start
|
16
|
+
loop do
|
17
|
+
read_user_input
|
18
|
+
break if @exit_requested
|
19
|
+
render_layout
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def read_user_input
|
26
|
+
reader = TTY::Reader.new
|
27
|
+
key = reader.read_keypress
|
28
|
+
handle_keypress(key)
|
29
|
+
end
|
30
|
+
|
31
|
+
def handle_keypress(key)
|
32
|
+
case key
|
33
|
+
when "\e[A" # Up arrow
|
34
|
+
@selected_index = (@selected_index - 1) % @filtered_table_names.length unless @filtered_table_names.empty?
|
35
|
+
when "\e[B" # Down arrow
|
36
|
+
@selected_index = (@selected_index + 1) % @filtered_table_names.length unless @filtered_table_names.empty?
|
37
|
+
when "\r" # Enter
|
38
|
+
# Detail action here
|
39
|
+
when "\e" # Escape key
|
40
|
+
@exit_requested = true
|
41
|
+
when "\u007F", "\b" # Backspace
|
42
|
+
@input.chop!
|
43
|
+
else
|
44
|
+
@input << key if key =~ /\A[\w\s]+\z/
|
45
|
+
end
|
46
|
+
|
47
|
+
update_filtered_tables
|
48
|
+
end
|
49
|
+
|
50
|
+
def update_filtered_tables
|
51
|
+
@filtered_table_names = @data.keys.select { |name| name.downcase.include?(@input.downcase) }
|
52
|
+
@selected_index = 0 if @filtered_table_names.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
def render_layout
|
56
|
+
system "clear"
|
57
|
+
puts TTY::Cursor.move_to(0, 0)
|
58
|
+
draw_input_box
|
59
|
+
draw_table_selection
|
60
|
+
draw_details_panel
|
61
|
+
end
|
62
|
+
|
63
|
+
def draw_input_box
|
64
|
+
left_panel_width = TTY::Screen.width / 2
|
65
|
+
top_height = 3
|
66
|
+
print TTY::Box.frame(
|
67
|
+
top: 0,
|
68
|
+
left: 0,
|
69
|
+
width: left_panel_width,
|
70
|
+
height: top_height,
|
71
|
+
title: { top_left: ' Search ' },
|
72
|
+
border: :light
|
73
|
+
) { @input }
|
74
|
+
end
|
75
|
+
|
76
|
+
def draw_table_selection
|
77
|
+
screen_size = TTY::Screen.size
|
78
|
+
left_panel_width = screen_size[1] / 2
|
79
|
+
top_height = 3
|
80
|
+
print TTY::Box.frame(
|
81
|
+
top: top_height,
|
82
|
+
left: 0,
|
83
|
+
width: left_panel_width,
|
84
|
+
height: screen_size[0] - top_height,
|
85
|
+
title: { top_left: " Select Table: #{@selected_index + 1}/#{@filtered_table_names.length} " },
|
86
|
+
border: :light
|
87
|
+
) { @filtered_table_names.map.with_index { |name, index| index == @selected_index ? "=> #{name}" : " #{name}" }.join("\n") }
|
88
|
+
end
|
89
|
+
|
90
|
+
def draw_details_panel
|
91
|
+
screen_size = TTY::Screen.size
|
92
|
+
left_panel_width = screen_size[1] / 2
|
93
|
+
right_panel_width = screen_size[1] - left_panel_width
|
94
|
+
print TTY::Box.frame(
|
95
|
+
top: 0,
|
96
|
+
left: left_panel_width,
|
97
|
+
width: right_panel_width,
|
98
|
+
height: screen_size[0],
|
99
|
+
title: { top_left: ' Details ' },
|
100
|
+
border: :light
|
101
|
+
) { @data[@filtered_table_names[@selected_index]] || "" }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/railsfinder/version.rb
CHANGED
data/lib/railsfinder.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "railsfinder/version"
|
4
|
+
require_relative "railsfinder/interface"
|
5
|
+
require_relative "railsfinder/schema_loader"
|
6
|
+
require_relative "railsfinder/schema_parser"
|
7
|
+
require_relative "railsfinder/table_selector"
|
4
8
|
|
5
9
|
module Railsfinder
|
6
10
|
class Error < StandardError; end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: railsfinder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jpstudioweb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-box
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.22'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.22'
|
69
83
|
description: This gem enables users to search and view details of tables defined in
|
70
84
|
the schema.rb file of a Rails application.
|
71
85
|
email:
|
@@ -82,6 +96,9 @@ files:
|
|
82
96
|
- bin/setup
|
83
97
|
- lib/railsfinder.rb
|
84
98
|
- lib/railsfinder/interface.rb
|
99
|
+
- lib/railsfinder/schema_loader.rb
|
100
|
+
- lib/railsfinder/schema_parser.rb
|
101
|
+
- lib/railsfinder/table_selector.rb
|
85
102
|
- lib/railsfinder/version.rb
|
86
103
|
homepage: http://example.com/mygem
|
87
104
|
licenses:
|