kang 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/kang CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  #
3
3
  # Created on 2009-1-7.
4
- # Copyright (c) 2009. All rights reserved.
4
+ # Copyright (c) 2011. All rights reserved.
5
5
 
6
6
  require 'rubygems'
7
7
  require File.expand_path(File.dirname(__FILE__) + "/../lib/kang")
data/lib/kang/cli.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Kang
2
2
  class CLI
3
3
  def self.execute(stdout, arguments=[])
4
- Kang.new.main_loop
4
+ Kang::Controller.new
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,3 @@
1
+ module Kang
2
+ VERSION = "0.0.2"
3
+ end
data/lib/kang.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # kang.rb - Main executable for GUI interface
5
5
  #
6
6
  # ====================================================================
7
- # Copyright (c) 2008 Tony Doan <tdoan@tdoan.com>. All rights reserved.
7
+ # Copyright (c) 2011 Tony Doan <tdoan@tdoan.com>. All rights reserved.
8
8
  #
9
9
  # This software is licensed as described in the file COPYING, which
10
10
  # you should have received as part of this distribution. The terms
@@ -14,102 +14,207 @@
14
14
  # ====================================================================
15
15
  #
16
16
  $:.unshift(File.dirname(__FILE__)) unless
17
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
17
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
18
18
 
19
- require 'wx'
20
- include Wx
19
+ begin
20
+ require 'gtk2'
21
+ rescue LoadError => ex
22
+ $stderr.puts("This is a GUI application that requires the gtk2 gem be installed, which in turn requires that GTK2 be installed.")
23
+ exit(-1)
24
+ end
21
25
 
22
26
  module Kang
23
- VERSION = '0.0.1'
24
- class EventFrame < Wx::Frame
25
- def message(text, title)
26
- m = Wx::MessageDialog.new(self, text, title, Wx::OK | Wx::ICON_INFORMATION)
27
- m.show_modal()
28
- end
27
+ class Data
28
+ def initialize
29
+ @re = nil
30
+ @match = nil
31
+ end
29
32
 
30
- def initialize()
31
- @highlight = Wx::TextAttr.new(Wx::GREEN, Wx::Colour.new(255, 255, 0) )
32
- @normal = Wx::TextAttr.new(Wx::BLACK, Wx::WHITE) #, Wx::Colour.new(255, 255, 0) )
33
-
34
- super(nil, -1, "Kang")
35
- set_client_size(Wx::Size.new(640,480))
36
- t1_title = Wx::StaticBox.new(self, -1, "Regex", Wx::DEFAULT_POSITION)
37
- t2_title = Wx::StaticBox.new(self, -1, "Text", Wx::DEFAULT_POSITION)
38
- ls_title = Wx::StaticBox.new(self, -1, "Groups", Wx::DEFAULT_POSITION)
39
- grid = Wx::GridSizer.new(2,10,10)
40
- sizer = Wx::BoxSizer.new(Wx::VERTICAL)
41
- supersizer = Wx::BoxSizer.new(Wx::VERTICAL)
42
- @text = Wx::TextCtrl.new(self,-1,'(Regex)? (in) (here)',Wx::DEFAULT_POSITION,Wx::DEFAULT_SIZE,Wx::TE_MULTILINE|TE_RICH)
43
- @text2 = Wx::TextCtrl.new(self,-1,'Text in here',Wx::DEFAULT_POSITION,Wx::DEFAULT_SIZE,Wx::TE_MULTILINE)
44
- @list = Wx::ListCtrl.new(self,-1,Wx::DEFAULT_POSITION,Wx::DEFAULT_SIZE,Wx::LC_REPORT)
45
- @list.insert_column(0,"Group Num",Wx::LIST_FORMAT_RIGHT, -1)
46
- @list.set_column_width(0,85)
47
- @list.insert_column(1,"Match Data",Wx::LIST_FORMAT_LEFT, -1)
48
- @list.set_column_width(1,180)
49
- t1sizer = Wx::StaticBoxSizer.new(t1_title,Wx::VERTICAL)
50
- t2sizer = Wx::StaticBoxSizer.new(t2_title,Wx::VERTICAL)
51
- lssizer = Wx::StaticBoxSizer.new(ls_title,Wx::VERTICAL)
52
- t1sizer.add(@text, 1,Wx::EXPAND|Wx::ALL,2)
53
- t2sizer.add(@text2,1,Wx::EXPAND|Wx::ALL,2)
54
- lssizer.add(@list,1,Wx::EXPAND|Wx::ALL,2)
55
- sizer.add(t1sizer,1,Wx::EXPAND,2)
56
- sizer.add(t2sizer,1,Wx::EXPAND,2)
57
- @status = StatusBar.new(self,-1)
58
- grid.add(sizer,1,Wx::EXPAND)
59
- grid.add(lssizer,1,Wx::EXPAND)
60
- supersizer.add(grid,1,Wx::EXPAND)
61
- supersizer.add(@status)
62
- self.set_sizer(supersizer)
63
- evt_text(@text.get_id){|event| text_change(event)}
64
- evt_text(@text2.get_id){|event| text_change(event)}
65
- text_change(nil)
66
- end
33
+ def update_match_string(match_string)
34
+ @match_string = match_string
35
+ update_match
36
+ end
37
+
38
+ def update_match
39
+ if @re
40
+ @match = @re.match(@match_string)
41
+ else
42
+ @match = nil
43
+ end
44
+ end
45
+
46
+ def match_group_count
47
+ if @match
48
+ @match.to_a.size-1
49
+ else
50
+ nil
51
+ end
52
+ end
53
+
54
+ def update_regexp(re_string)
55
+ begin
56
+ @re = Regexp.new(re_string)
57
+ rescue RegexpError
58
+ @re = nil
59
+ end
60
+ update_match
61
+ end
67
62
 
68
- def set_status(text)
69
- @status.set_status_text(text)
63
+ def regexp_valid?
64
+ @re.nil? ? false : true
65
+ end
66
+
67
+ def match?
68
+ @match ? true : false
69
+ end
70
+
71
+ def match_begin
72
+ if @match
73
+ @match.begin(0)
74
+ else
75
+ nil
76
+ end
77
+ end
78
+
79
+ def match_end
80
+ if @match
81
+ @match.end(0)
82
+ else
83
+ nil
84
+ end
85
+ end
86
+
87
+ def matches
88
+ if @match and (@match.length > 1)
89
+ @match[1..-1]
90
+ else
91
+ []
92
+ end
93
+ end
70
94
  end
71
95
 
72
- def text_change(event)
73
- ip = @text2.get_insertion_point
74
- size = @text2.get_value.size
75
- begin
76
- r = Regexp.new(@text.get_value)
77
- set_status("")
78
- rescue
79
- set_status("Invalid Regex")
80
- @list.delete_all_items
81
- end
82
- unless r.nil?
83
- md = r.match(@text2.get_value)
84
- unless md.nil?
85
- b = md.begin(0)
86
- e = md.end(0)
87
- ret= @text2.set_style(0,size,@normal)
88
- @text2.append_text("")
89
- @text2.set_style(b,e,@highlight)
90
- @text2.append_text("")
91
- @list.delete_all_items
92
- md.to_a[1..-1].each_with_index do |s,i|
93
- @list.insert_item(i,"#{i+1}")
94
- @list.set_item(i,1,s) unless s.nil?
95
- end
96
+ class Controller
97
+ def initialize
98
+ @view = View.new(self)
99
+ @data = Data.new
100
+ @view.start
101
+ end
102
+
103
+ def key_up_match(view,event,match_string)
104
+ @view.update_status("")
105
+ @data.update_match_string(match_string)
106
+ key_up
107
+ end
108
+
109
+ def key_up_reg(view,event,text)
110
+ @view.update_status("")
111
+ @data.update_regexp(text)
112
+ key_up
113
+ end
114
+
115
+ private
116
+ def key_up
117
+ unless @data.regexp_valid?
118
+ @view.remove_tag
119
+ @view.update_status("Invalid Regexp")
96
120
  else
97
- ret = @text2.set_style(0,size,@normal)
98
- @text2.append_text("")
99
- @list.delete_all_items
121
+ count = @data.match_group_count ? @data.match_group_count : "no"
122
+ @view.update_status("Matched with #{@data.match_group_count} grouping(s)")
123
+ if @data.match?
124
+ @view.update_tag(@data.match_begin,@data.match_end)
125
+ else
126
+ @view.remove_tag
127
+ @view.update_status("no match")
128
+ end
100
129
  end
101
- else
102
- ret = @text2.set_style(0,size,@normal)
103
- @text2.append_text("")
130
+ @view.update_match_groups(@data.matches)
104
131
  end
105
- @text2.set_insertion_point(ip)
106
132
  end
107
- end
108
133
 
109
- class Kang < App
134
+ class View
135
+ def initialize(controller)
136
+ @controller = controller
137
+ @window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
138
+ @window.set_title "Kang"
139
+ @window.border_width = 10
140
+ @window.set_size_request(600, 400)
141
+ @window.signal_connect('delete_event') { Gtk.main_quit }
142
+
143
+ wintop = Gtk::ScrolledWindow.new
144
+ wintop.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS)
145
+
146
+ winbottom = Gtk::ScrolledWindow.new
147
+ winbottom.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS)
148
+
149
+ @statusbar = Gtk::Statusbar.new
150
+ @statusbar.push(0,"")
151
+
152
+ @regview = Gtk::TextView.new
153
+ @regview.buffer.text = "RegExp here"
154
+
155
+ @matchview = Gtk::TextView.new
156
+ @matchview.buffer.text = "Match Text Here"
157
+
158
+ @matchview.buffer.create_tag("colors",{ "foreground" => "green", "background" => "#000000" })
159
+
160
+ wintop.add(@regview)
161
+ winbottom.add(@matchview)
110
162
 
111
- def on_init
112
- EventFrame.new.show
163
+ @list_store = Gtk::ListStore.new(Integer, String)
164
+ treeview = Gtk::TreeView.new(@list_store)
165
+ column0 = Gtk::TreeViewColumn.new("#",Gtk::CellRendererText.new, {:text => 0})
166
+ column1 = Gtk::TreeViewColumn.new("Match",Gtk::CellRendererText.new, {:text => 1})
167
+ treeview.append_column(column0)
168
+ treeview.append_column(column1)
169
+ treeview.selection.mode = Gtk::SELECTION_NONE
170
+
171
+ vpaned = Gtk::VPaned.new
172
+ vpaned.add1(wintop)
173
+ vpaned.add2(winbottom)
174
+ vpaned.set_size_request(400,400)
175
+ hpaned = Gtk::HPaned.new
176
+ hpaned.add1(vpaned)
177
+ hpaned.add2(treeview)
178
+ vbox = Gtk::VBox.new(false,0)
179
+ vbox.pack_start(hpaned,true,true,0)
180
+ vbox.pack_start(@statusbar,false,false,0)
181
+ @window.add(vbox)
182
+
183
+ @regview.signal_connect("key-release-event") {|view,event| @controller.key_up_reg(view,event,view.buffer.text)}
184
+ @matchview.signal_connect("key-release-event") {|view,event| @controller.key_up_match(view,event,view.buffer.text)}
185
+ end
186
+
187
+ def start
188
+ @window.show_all
189
+ begin
190
+ Gtk.main
191
+ rescue SystemExit, Interrupt
192
+ exit(0)
193
+ end
194
+ end
195
+
196
+ def update_tag(tag_begin,tag_end)
197
+ remove_tag
198
+ b = @matchview.buffer.get_iter_at_offset(tag_begin)
199
+ e = @matchview.buffer.get_iter_at_offset(tag_end)
200
+ @matchview.buffer.apply_tag("colors",b,e)
201
+ end
202
+
203
+ def update_status(message)
204
+ @statusbar.pop(0)
205
+ @statusbar.push(0,message)
206
+ end
207
+
208
+ def update_match_groups(groups)
209
+ @list_store.clear
210
+ groups.each_with_index{|item,i| iter = @list_store.append; iter[0]=i;iter[1]=item}
211
+ end
212
+
213
+ def remove_tag
214
+ buffer = @matchview.buffer
215
+ bstart = buffer.get_iter_at_offset(0)
216
+ bend = buffer.get_iter_at_offset(buffer.text.size)
217
+ @matchview.buffer.remove_tag("colors",bstart,bend)
218
+ end
113
219
  end
114
220
  end
115
- end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ prerelease:
5
+ version: 0.0.2
5
6
  platform: ruby
6
7
  authors:
7
8
  - Tony Doan
@@ -9,88 +10,58 @@ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2009-01-07 00:00:00 -08:00
13
- default_executable:
13
+ date: 2011-06-21 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: wxruby
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.9.8
24
- version:
25
- - !ruby/object:Gem::Dependency
26
- name: newgem
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 1.2.3
34
- version:
35
- - !ruby/object:Gem::Dependency
36
- name: hoe
37
- type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
16
+ name: gtk2
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
40
20
  requirements:
41
21
  - - ">="
42
22
  - !ruby/object:Gem::Version
43
- version: 1.8.0
44
- version:
45
- description: "* KANG - The Ruby Regex Debugger"
46
- email:
47
- - tdoan@tdoan.com
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description: The Ruby Regex Debugger. Put your regex in the top pane, and your match text in the bottom pane. Kang will highlight the match text that matches and show you any match groups you have created down the right hand side.
27
+ email: tdoan@tdoan.com
48
28
  executables:
49
29
  - kang
50
30
  extensions: []
51
31
 
52
- extra_rdoc_files:
53
- - History.txt
54
- - Manifest.txt
55
- - README.txt
32
+ extra_rdoc_files: []
33
+
56
34
  files:
57
- - History.txt
58
- - Manifest.txt
59
- - README.txt
60
- - Rakefile
61
- - bin/kang
62
35
  - lib/kang.rb
63
36
  - lib/kang/cli.rb
64
- - test/test_helper.rb
65
- - test/test_kang.rb
66
- has_rdoc: true
67
- homepage: http://kang.rubyforge.org/
37
+ - lib/kang/version.rb
38
+ - bin/kang
39
+ homepage: http://github.com/tdoan/kang
40
+ licenses: []
41
+
68
42
  post_install_message:
69
- rdoc_options:
70
- - --main
71
- - README.txt
43
+ rdoc_options: []
44
+
72
45
  require_paths:
73
46
  - lib
74
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
75
49
  requirements:
76
50
  - - ">="
77
51
  - !ruby/object:Gem::Version
78
52
  version: "0"
79
- version:
80
53
  required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
81
55
  requirements:
82
56
  - - ">="
83
57
  - !ruby/object:Gem::Version
84
58
  version: "0"
85
- version:
86
59
  requirements: []
87
60
 
88
- rubyforge_project: kang
89
- rubygems_version: 1.3.1
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.5
90
63
  signing_key:
91
- specification_version: 2
92
- summary: "* KANG - The Ruby Regex Debugger"
93
- test_files:
94
- - test/test_helper.rb
95
- - test/test_kang.rb
96
- - test/test_kang_cli.rb
64
+ specification_version: 3
65
+ summary: A visual regex debugger
66
+ test_files: []
67
+
data/History.txt DELETED
@@ -1,4 +0,0 @@
1
- == 0.0.1 2009-01-07
2
-
3
- * 1 major enhancement:
4
- * Initial release
data/Manifest.txt DELETED
@@ -1,9 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- bin/kang
6
- lib/kang.rb
7
- lib/kang/cli.rb
8
- test/test_helper.rb
9
- test/test_kang.rb
data/README.txt DELETED
@@ -1,36 +0,0 @@
1
- = kang
2
-
3
- * http://kang.rubyforge.org/
4
-
5
- == DESCRIPTION:
6
-
7
- * KANG - The Ruby Regex Debugger
8
-
9
- == FEATURES/PROBLEMS:
10
-
11
- * This is a cross-platform GUI application. To run it you'll need Ruby (I've only tested with 1.8.7 so far) and the wxruby gem (I've only tested with 1.9.8).
12
-
13
- == SYNOPSIS:
14
-
15
- FIX (code sample of usage)
16
-
17
- == REQUIREMENTS:
18
-
19
- * ruby
20
- * rubygems
21
- * wxruby
22
-
23
- == INSTALL:
24
-
25
- * sudo gem install kang
26
-
27
- == LICENSE:
28
-
29
- ====================================================================
30
- Copyright (c) 2008 Tony Doan <tdoan@tdoan.com>. All rights reserved.
31
- This software is licensed as described in the file COPYING, which
32
- you should have received as part of this distribution. The terms
33
- are also available at http://github.com/tdoan/kang/tree/master/COPYING.
34
- If newer versions of this license are posted there, you may use a
35
- newer version instead, at your option.
36
- ====================================================================
data/Rakefile DELETED
@@ -1,28 +0,0 @@
1
- %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
- require File.dirname(__FILE__) + '/lib/kang'
3
-
4
- # Generate all the Rake tasks
5
- # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
- $hoe = Hoe.new('kang', Kang::VERSION) do |p|
7
- p.developer('Tony Doan', 'tdoan@tdoan.com')
8
- p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
- #p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
- p.rubyforge_name = p.name # TODO this is default value
11
- p.extra_deps = [
12
- ['wxruby','>= 1.9.8'],
13
- ]
14
- p.extra_dev_deps = [
15
- ['newgem', ">= #{::Newgem::VERSION}"]
16
- ]
17
-
18
- p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
- path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
- p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
- p.rsync_args = '-av --delete --ignore-errors'
22
- end
23
-
24
- require 'newgem/tasks' # load /tasks/*.rake
25
- Dir['tasks/**/*.rake'].each { |t| load t }
26
-
27
- # TODO - want other tests/tasks run by default? Add them to the list
28
- # task :default => [:spec, :features]
data/test/test_helper.rb DELETED
@@ -1,3 +0,0 @@
1
- require 'stringio'
2
- require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/kang'
data/test/test_kang.rb DELETED
@@ -1,11 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- class TestKang < Test::Unit::TestCase
4
-
5
- def setup
6
- end
7
-
8
- def test_truth
9
- assert true
10
- end
11
- end
@@ -1,14 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper.rb")
2
- require 'kang/cli'
3
-
4
- class TestKangCli < Test::Unit::TestCase
5
- def setup
6
- Kang::CLI.execute(@stdout_io = StringIO.new, [])
7
- @stdout_io.rewind
8
- @stdout = @stdout_io.read
9
- end
10
-
11
- def test_not_print_default_output
12
- assert_no_match(/To update this executable/, @stdout)
13
- end
14
- end