Stringscan

Check-in [d3127b90fb]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Refine search process
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d3127b90fb21640ac20f5ba3b25d22c5145ffebf382836462bffed26c1792953
User & Date: kevin 2017-06-11 04:14:14
Context
2017-06-11
04:20
More refinements check-in: 8ff20c9b76 user: kevin tags: trunk
04:14
Refine search process check-in: d3127b90fb user: kevin tags: trunk
03:32
Add files for complete build check-in: 60cab51a79 user: kevin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added stringscan.rb.





















































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#Stringscan: grep-like tool, written in Ruby-Tk. (c) 2017 Kevin Walzer/WordTech Communications LLC. License: MIT license.
 
#encoding: UTF-8
 
require 'tk'
require 'tkballoonhelp'
require 'tkextlib/tile'
require 'find'
require 'tkextlib/tcllib/tablelist_tile'
require 'mime/types'
require 'tk/tk_mac'
require 'tkextlib/tkDND'


$platform = Tk.windowingsystem()
if $platform == 'aqua'
  TkPackage.require('windowlist')
end


class StringscanApp

  #core method; here we search for a string in text files within a directory and display a list of matching files in the listbox 
  def stringgrep
    $file_list = []
    $grep_list = []
    $lbox.delete(0, 'end')
    Find.find("#{$dirname}") do |path|
      $file_list << path unless FileTest.directory?(path)
    end
    for i in $file_list
      begin
        #We are only reading text files here, not binary. 
        if MIME::Types.type_for(i).first.media_type =~ /text/
          f = File.open(i, "r:iso-8859-1:utf-8")
          result = f.read
          if result =~ /#{$searchterm}/ then
          $lbox.insert('end', "#{i}")
          f.close
        end
        end
      rescue
        #puts "Search term not found.\n"
      end
    end
    rescue
      puts "Directory not found.\n"
  puts "Search complete.\n"
  end

 
  def drawgui
    
    begin
      Tk.ip_eval("console hide")
    rescue
      puts "Could not hide console.\n"
    end

    #image data
    $folderdata = 'R0lGODlhGAAYAIABAAQHB////yH+EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAAEALAAAAAAYABgAAAI1hI8Wy70JgZshJurOtFzmHm0ghJXVZVJkKo7uC8cr28z0g944ojP2/aMFWcNU0SRLxk5KSQEAOw=='

    $glassdata = 'R0lGODlhGAAYAIABAAAAAP///yH+EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAAEALAAAAAAYABgAAAJHjI8Gy+nJYntQWkBVRDLv9zkTNXKleAYhuaBYpr2m7LWVXdOxfvMd67OATpeJcFa89JK/A1O5PMJUySlyZaVis1oel/o9FAAAOw=='

    $folderimage = TkPhotoImage.new(:data => $folderdata)
    $glassimage = TkPhotoImage.new(:data => $glassdata)
 
    #initialize variables for entry
    $dirname = TkVariable.new
    $searchterm = TkVariable.new
    #top window and frame

    Tk::TkDND::DND
    
    $root = TkRoot.new {
      title "Stringscan"
    }
    
    #menu
    $menubar = TkMenu.new($root)
    TkOption.add '*tearOff', 0
    if $platform == 'aqua'
      $appmenu = TkSysMenu_Apple.new($menubar)
      $menubar.add :cascade, :menu => $appmenu
      $appmenu.add :command, :label => 'About Stringscan', :command=> proc{aboutWindow}
      $appmenu.add :separator
    end
    $searchmenu = TkMenu.new($menubar)
    $menubar.add :cascade, :menu => $searchmenu, :label => 'Search'
    $searchmenu.add :command, :label => 'Run Search', :command => proc{stringgrep}
    $searchmenu.add :command, :label => 'Choose Directory...', :command => proc{ $dirname = Tk::chooseDirectory('initialdir'=>Dir.home, 'parent'=>$root)}
    if $platform == 'win32'
      $searchmenu.add :command, :label => "Exit", :command=>exit
    end
    if $platform == 'aqua'
      Tk.tk_call("windowlist::windowMenu",  $menubar)
    end
    $helpmenu = TkSysMenu_Help.new($menubar)
    $menubar.add :cascade, :menu => $helpmenu, :label => 'Help'
    if $platform == 'aqua'
      #       Tk.ip_eval("proc ::tk::mac::ShowHelp {} {#{Tk.install_cmd(proc{...})}}")
    end
    $root['menu'] = $menubar
    $mainframe = Tk::Tile::Frame.new($root).pack('side' => 'top','fill' => 'both','expand' => 'yes')
 
    #button frame and buttons
    $buttonframe = Tk::Tile::Frame.new($mainframe){padding 2}.pack('side' => 'top','fill' => 'both','expand' => 'no')
    $choosebutton = Tk::Tile::Button.new($buttonframe) {
      image $folderimage
      takefocus 0
      padding 5
      command  proc{$dirname.value = Tk::chooseDirectory('initialdir'=>Dir.home, 'parent'=>$root)}
    }.pack('side' => 'left','fill' => 'both','expand' => 'no')
    Tk::RbWidget::BalloonHelp.new($choosebutton, 'text'=>'Select Directory', 'background'=>'lightyellow', 'relief'=>'flat')
    stringgrepProc = proc {stringgrep}
    $runbutton = Tk::Tile::Button.new($buttonframe) {
      image $glassimage
      takefocus 0
      padding 5
      command stringgrepProc
    }.pack('side' => 'left','fill' => 'both','expand' => 'no')
       Tk::RbWidget::BalloonHelp.new($runbutton, 'text'=>'Run Search', 'background'=>'lightyellow', 'relief'=>'flat')
    $sep = Tk::Tile::Separator.new($mainframe) { orient 'horizontal' }.pack('side'=> 'top', 'fill'=>'both')
 
    #labels and entries
    $topframe = Tk::Tile::Frame.new($mainframe){padding 5}.pack('side' => 'top','fill' => 'both','expand' => 'no')
    $chooselabel = Tk::Tile::Label.new($topframe) {text "Directory:"}.pack('side' => 'left','fill' => 'both','expand' => 'no')
    $direntry = Tk::Tile::Entry.new($topframe) {textvariable $dirname }.pack('side' => 'left','fill' => 'both','expand' => 'no')
    $termlabel = Tk::Tile::Label.new($topframe) {text "Search Term:"}.pack('side' => 'left','fill' => 'both','expand' => 'no')
    $termentry = Tk::Tile::Entry.new($topframe) {textvariable $searchterm}.pack('side' => 'left','fill' => 'both','expand' => 'no')
    $termentry.bind('Return', proc {stringgrep})
    $nextsep = Tk::Tile::Separator.new($mainframe) {orient 'horizontal'}.pack('side'=>'top', 'fill'=>'both')
 
    #listbox, textbox, scrollbars
    $bottomframe = Tk::Frame.new($mainframe).pack('side'=>'bottom','fill' => 'both', 'expand' => 'yes')
    $bottomleftframe  =Tk::Tile::Frame.new($bottomframe).pack('side'=>'left','fill' => 'both', 'expand' => 'yes')
    $listlabel = Tk::Tile::Label.new($bottomleftframe) {
      text 'File Name'
      style 'TablelistHeader.TLabel'
      padding 1
    }.pack('side'=>'top', 'fill'=>'both', 'expand' => 'no')
    $lbox = Tk::Listbox.new($bottomleftframe){
      width 50
      height 30
      borderwidth 0
      dnd_bindtarget('text/uri-list', '<Drop>', '%D') {|d|
        filename=d.join(' ')
                               begin
                                 if File.directory?(filename)
                                   $dirname = filename
                                   $direntry.value = filename
                                 end
                               rescue
                                 raise
                               end
        }
    }.pack('side'=>'left','fill' => 'both', 'expand' => 'yes')
    $scrollframe = Tk::Frame.new($bottomleftframe).pack('side'=>'right','fill' => 'y', 'expand' => 'no')
    $listscroll = Tk::Scrollbar.new($scrollframe).pack('side'=>'right','fill' => 'y', 'expand' => 'no')
    $lbox.yscrollbar($listscroll)
    $bottomrightframe  =Tk::Tile::Frame.new($bottomframe).pack('side'=>'right','fill' => 'both', 'expand' => 'yes')
    $tbox = Tk::Text::new($bottomrightframe){
      borderwidth 0.1
      relief 'sunken'
      wrap 'word'
      highlightcolor 'SlateGray3'
      bg 'white'
      state 'disabled'
    }.pack('side'=>'left','fill' => 'both', 'expand' => 'yes')
    $tscrollframe = Tk::Frame.new($bottomrightframe).pack('side'=>'right','fill' => 'y', 'expand' => 'no')
    $tscroll = Tk::Scrollbar.new($tscrollframe).pack('side'=>'right','fill' => 'y', 'expand' => 'no')
    $tbox.yscrollbar($tscroll)
    $lbox.bind '<ListboxSelect>', proc {
      begin
      if $lbox.curselection.empty?
        return
      else
        highlighttext TkSelection.get
      end
      rescue
      end
    }
  end

  #read file for display in text widget
  def highlighttext(file)
   $tbox.delete('1.0', 'end') 
   f = open(file, 'rb')
   $tbox.configure('state'=>'normal')
    while(!f.eof?)
      $tbox.insert('end', f.read(1000))
	end
    f.close
    $tbox.configure('state'=>'disabled')
    $tbox.tag_configure('search', :background=>'yellow')
    sethighlight
  end

  #set highlight color for search term
  def sethighlight
    $tbox.configure('state'=>'normal')
    $tbox.tag_remove('search', '0.0', 'end')
    return if $searchterm == ""
    cur = '1.0'
    loop {
      cur, len = $tbox.search_with_length("#{$searchterm}", cur, 'end')
           break if cur == ""
	   $tbox.tag_add('search', cur, "#{cur} + #{len} char")
           cur = $tbox.index("#{cur} + #{len} char")
    }
    $tbox.configure('state'=>'disabled')
  end
 
 
 #about window for app
  def aboutWindow
    Tk::messageBox :type => 'ok',
    :message => 'Stringscan: Text Search Tool',
    :icon => 'info', :title => 'About Stringscan',
    :parent => $root
  end

 
  #here we initialize our app class
   def initialize
    $dirname = nil
    $searchterm = nil

        if $platform == 'aqua'
          ##map Ruby proc to "odoc" Apple Event
          setDir = Tk.install_cmd(proc{
                               |*args|
                               filename=(args[0]).delete('{}')
                               begin
                                 if File.directory?(filename)
                                   $dirname = filename
                                   $direntry.value = filename
                                 end
                               rescue
                                 raise
                               end
                          })    
     Tk.ip_eval("proc ::tk::mac::OpenDocument {args} {#{setDir} $args}")
        end        
    drawgui()
  end             
end
 
app = StringscanApp.new()
Tk.mainloop()