Stringscan

Check-in [a7d8752c26]
Login

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

Overview
Comment:Fix spacing in code
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a7d8752c2663a51154962681fe5d85d802d71ce7d1f8510ac6168b2bb0e39474
User & Date: kevin 2017-06-24 14:18:14
Context
2017-06-24
14:34
Update spacing check-in: 6883ab21ea user: kevin tags: trunk
14:18
Fix spacing in code check-in: a7d8752c26 user: kevin tags: trunk
2017-06-23
03:30
Update to user help check-in: d77e683476 user: kevin tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

#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()

#find additional Tcl libraries
if $platform == 'aqua'
  Tk::AUTO_PATH.list <<= File.dirname(__FILE__)
end


if $platform == 'aqua'
  TkPackage.require('windowlist')
end
TkPackage.require('regproc')
TkPackage.require('machelp')
TkPackage.require('softwareupdate')
TkPackage.require('xplat')


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. 
        text =  MIME::Types.type_for(i).first.to_s
        type =  text.split('/')[0]
        if 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)

    $icondata = 'R0lGODlhQABAAOf/AB8jJSktLyUvLyouMCsvMSwwMi0xMy4yNC8zNS80NjI2ODU5Ozc7PT5APjxBQz5CRUJEQUJGSERJS0hKR0ZLTUBPX0tPUk9QTk5SVFBST0dVZlJUUVBVV1RVU0NZaFJXWVVXVVZYVUdab1dZVlVaXFhaV1lbWFpcWVhcX0tedFtdWi1kplxeW09hbF5fXTZlm1xgYzhloUlkeV9hXkhlhWBiX15jZWFjYE9mgT1opWJkYTRsp2NlYmRlY2VmZGZnZWNoaj9to1FsgU9sjGdpZlRsh2VqbGhqZ2lraElvoGpsaVNwkWttamltcF5we1lwjEZzqm1vbFFzmW5wbWxwc1l0iVh0lXByb25ydVR2nHJzcUt5qk14sHB0d2V3gnN1clx4mUh7uFt6lXV3dHN3ell7oWZ5kFh9nVZ9qXd5dnp5cVKAsXl7eFt/slaAuWN/oGx/il+Ap3h9f3t9enx+e2GCqX1/fH+BfmWGrXeGjIKEgWKJtmmJpIOFgoGFiHKImW6Jn4SGg4eGfnaJlWWLuIWHhHyIlWOOs2mNrluPx2yNtXSMqIiKh3eOnomLiHGQq4uMiXyPm3+PlYyOi4+OhnGSuY6QjXmUqpKRiX6VppCSj3eWspKUkXuXun2YrpSWk3Gd0H+bvpGZoXedyoWcrZeZloKdtH6euZqcmXyhwpyem4yjtImlu4ykwaCin4Gn1YyovqSlooqqxpGoxqaopYiu3Jauv6mrqKOstJqtxZWwx6utqp2wyaGww7Cvpoy13a+xrqeywLK0sa63v6i4y7W3tKy4xaG94aK+1bm7uLC8yrS8xJ3C5L2/vLbBz7rCysPFwr7Hz7zI1sjKx8PM1MHN27zQ6cfP2M7Qzc3S1MfT4cvT28nV483W3tLX2cra7tfZ1s/b6dPb5N7Z2Nvd2tLe7c/f89jg6dXh79/h3uLk4dnl8+nj4uTm49/o8N3p9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eYohfGMMYwcCGKPBgiEo2oggyKYIUYZCMeu5iDdKdLXenS4brX1cMkbPuS4ZCAEYXQAzjiQQg8LAIQgPjDHwYRCUlIwgkeSAEOhiAFLsBjHoyYFCUoMYn++ve//63tWwKAAAW4wA52vEYOPMHgTGRiEF5oQQU0IAIaDKG5zsDHND6Apw57+MMfLpvZEDCAMcSDHq1IghiqIIQUiMADIqjwEJaQhSQoAh766MMAAMDjHgMAURKJQCz6AY9KvOAJTyjCEqQghSVn4QxBaAM7+nGLEQC5MnNIhgNnsYItKAIPdYhDHOqwhRWEAh79UIfyOnahgiu/RQ/N6Mc+vNGJHaxgB3bOgSKugY9+pAMcluhDLELg5qcU4hbx6Ic+6JENt16DHvTDhyug0ZNS6MGAhX7KJJKhDn3wox+g1kc8isGEDZTiFqrYBSoEzQQIZNopdrBELHaxi1gwogYdEAgEILGLU7uiEKqgg6tfnSYIpEEYp44FJDgxiWETW0xEKEYpaEGL/pZiAs9O0wmkHYtbaIIRrshAticSD3vYYx7qgEgGgFGKbt8im9get0HmF+p4kAMiE6CFKmKRjmzOQd5ZggAqmkEOcsSDCQDPUgNKgQ98AKMBCRfTBTIA8Yhb/OKICggAOw=='
    $icon = TkPhotoImage.new(:data => $icondata)
 
    #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 :command, :label => 'License', :command=>proc{getReg}
      $appmenu.add :command, :label=>'Check for Updates', :command=>proc{checkUpdate}
      $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)
      $direntry.value = $dirname
    }
    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'
        $helpmenu.add :command, :label=>'Stringscan Help', :command=>proc{
          Tk.tk_call('machelp::userhelp')
        }
      end
         $helpmenu.add :command, :label=>'Contact Code by Kevin', :command=>proc{
        Tk.tk_call('xplat::launch', 'mailto:kw@codebykevin.com?subject=Stringscan')
     }
      $helpmenu.add :command, :label=>'Web Site', :command=>proc{
         Tk.tk_call('xplat::launch', 'https://www.codebykevin.com/stringscan.html')
      }

      if $platform != 'aqua'
      $helpmenu.add :command, :label => 'About Stringscan', :command=> proc{aboutWindow}
      $helpmenu.add :command, :label => 'License', :command=>proc{getReg}
      $helpmenu.add :command, :label=>'Check for Updates', :command=>proc{checkUpdate}
      
    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 = Tk::chooseDirectory('initialdir'=>Dir.home, 'parent'=>$root)
        $direntry.value = $dirname
      }
    }.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
      takefocus 0
      activestyle "none"
      exportselection 0
      selectbackground "RoyalBlue1"
      selectforeground "white"
      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')
    $lbox.bind '<ListboxSelect>', proc {
       begin
      if $lbox.curselection.empty?
       puts "No selection in listbox."
      else
         highlighttext  $lbox.get($lbox.curselection[0])
      end
      rescue
        puts "Cannot get selection in listbox."
      end
     }
    $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)
  end

  #read file for display in text widget
  def highlighttext(file)
   $tbox.configure('state'=>'normal')
   $tbox.delete('1.0', 'end') 
   f = open(file, 'rb')
    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',
    :detail  => "Version 1.0\n(c) 2017 Wordtech Communications LLC",
    :parent => $root
  end

#check version of installed software
def checkUpdate 
    Tk.tk_call('softwareupdate::setIcon', $icon)
    Tk.tk_call('softwareupdate::checkVersion', $appname, $appversion)
end

#check version of installed software
def getReg
  Tk.tk_call('regproc::getReg')
end

#user help
def showHelp
  Tk.tk_call('machelp::userhelp')
end
  
 
#here we initialize our app class
def initialize

    $dirname = ""
    $searchterm = ""
    $appname = 'Stringscan'
    $appversion = '1.0'
    Tk.tk_call('machelp::setAppName', $appname, $appversion)
    Tk.tk_call('softwareupdate::setAppName', $appname)
    Tk.tk_call('softwareupdate::setVersion', $appname, $appversion)

    bgerror = Tk.install_cmd(proc{
                               |*args|
                               puts args
                             })
    Tk.ip_eval("proc bgerror {args} {#{bgerror} $args}")

        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}")
          Tk.ip_eval("proc ::tk::mac::ShowHelp {} {#{Tk.install_cmd(proc{showHelp})}}")
        end        
        drawgui()
        Tk.tk_call('regproc::readLic', $appname, $appversion)
  end             
end
 
app = StringscanApp.new()
Tk.mainloop()



<

<









|



|




|









|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

<
|

|

|












<



<




|

<




|
|
|
|
|
|

|



|


|


|



|

|

|
|

|
|
|
|

|
|
|
|
<


|
<



|
|
|
|
|
|
|




|
|
|
|

|

<








<




|
|
|


|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|



|
|
|
|
|
|
|
|
|
|





|
|
|
|
|
|




|

|
|
|
|
|

|
|



|
|

|
|





|
|
|
|


|
|
<
|
|





|









|




|

|
|
<
<
|
<
<
<
<
<
<
<
|
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|

>
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271


272







273





274





















275
276
277
278
279
#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

#find additional Tcl libraries
if $platform == 'aqua'
    Tk::AUTO_PATH.list <<= File.dirname(__FILE__)
end


if $platform == 'aqua'
    TkPackage.require('windowlist')
end
TkPackage.require('regproc')
TkPackage.require('machelp')
TkPackage.require('softwareupdate')
TkPackage.require('xplat')


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. 
		text =  MIME::Types.type_for(i).first.to_s
		type =  text.split('/')[0]
		if 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)

    $icondata = 'R0lGODlhQABAAOf/AB8jJSktLyUvLyouMCsvMSwwMi0xMy4yNC8zNS80NjI2ODU5Ozc7PT5APjxBQz5CRUJEQUJGSERJS0hKR0ZLTUBPX0tPUk9QTk5SVFBST0dVZlJUUVBVV1RVU0NZaFJXWVVXVVZYVUdab1dZVlVaXFhaV1lbWFpcWVhcX0tedFtdWi1kplxeW09hbF5fXTZlm1xgYzhloUlkeV9hXkhlhWBiX15jZWFjYE9mgT1opWJkYTRsp2NlYmRlY2VmZGZnZWNoaj9to1FsgU9sjGdpZlRsh2VqbGhqZ2lraElvoGpsaVNwkW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kc8isGEDZTiFqrYBSoEzQQIZNopdrBELHaxi1gwogYdEAgEILGLU7uiEKqgg6tfnSYIpEEYp44FJDgxiWETW0xEKEYpaEGL/pZiAs9O0wmkHYtbaIIRrshAticSD3vYYx7qgEgGgFGKbt8im9get0HmF+p4kAMiE6CFKmKRjmzOQd5ZggAqmkEOcsSDCQDPUgNKgQ98AKMBCRfTBTIA8Yhb/OKICggAOw=='
    $icon = TkPhotoImage.new(:data => $icondata)

    #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 :command, :label => 'License', :command=>proc{getReg}
        $appmenu.add :command, :label=>'Check for Updates', :command=>proc{checkUpdate}
        $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)
        $direntry.value = $dirname
    }
    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'
        $helpmenu.add :command, :label=>'Stringscan Help', :command=>proc{
            Tk.tk_call('machelp::userhelp')
        }
    end
    $helpmenu.add :command, :label=>'Contact Code by Kevin', :command=>proc{
        Tk.tk_call('xplat::launch', 'mailto:kw@codebykevin.com?subject=Stringscan')
    }
    $helpmenu.add :command, :label=>'Web Site', :command=>proc{
        Tk.tk_call('xplat::launch', 'https://www.codebykevin.com/stringscan.html')
    }

    if $platform != 'aqua'
        $helpmenu.add :command, :label => 'About Stringscan', :command=> proc{aboutWindow}
        $helpmenu.add :command, :label => 'License', :command=>proc{getReg}
        $helpmenu.add :command, :label=>'Check for Updates', :command=>proc{checkUpdate}

    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 = Tk::chooseDirectory('initialdir'=>Dir.home, 'parent'=>$root)
            $direntry.value = $dirname
        }
    }.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
        takefocus 0
        activestyle "none"
        exportselection 0
        selectbackground "RoyalBlue1"
        selectforeground "white"
        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')
    $lbox.bind '<ListboxSelect>', proc {
        begin
            if $lbox.curselection.empty?
                puts "No selection in listbox."
            else
                highlighttext  $lbox.get($lbox.curselection[0])
            end
        rescue
            puts "Cannot get selection in listbox."
        end
    }
    $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)
end

#read file for display in text widget
def highlighttext(file)
    $tbox.configure('state'=>'normal')
    $tbox.delete('1.0', 'end') 
    f = open(file, 'rb')
    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',
    :detail  => "Version 1.0\n(c) 2017 Wordtech Communications LLC",
    :parent => $root
end

#check version of installed software
def checkUpdate 
    Tk.tk_call('softwareupdate::setIcon', $icon)
    Tk.tk_call('softwareupdate::checkVersion', $appname, $appversion)
end

#check version of installed software
def getReg
    Tk.tk_call('regproc::getReg')
end

#user help
def showHelp
    Tk.tk_call('machelp::userhelp')
end




end







#end of app class




























app = StringscanApp.new
Tk.mainloop