utils 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1,3 @@
1
+ rvm use ruby-1.9.2-p290
2
+ rvm gemset create utils
3
+ rvm gemset use utils
data/Rakefile CHANGED
@@ -15,8 +15,8 @@ GemHadar do
15
15
  ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock'
16
16
  readme 'README.rdoc'
17
17
 
18
- dependency 'spruz', '~>0.2.10'
19
- dependency 'term-ansicolor', '1.0.5'
18
+ dependency 'spruz', '~>0.2.12'
19
+ dependency 'term-ansicolor', '~>1.0'
20
20
 
21
21
  install_library do
22
22
  libdir = CONFIG["sitelibdir"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.10
1
+ 0.0.12
data/bin/chroot-libs CHANGED
@@ -5,7 +5,7 @@ include FileUtils::Verbose
5
5
 
6
6
  msg = "Usage: #$0 EXECUTABLE DIR [LIB ..]"
7
7
  executable = ARGV.shift or fail msg
8
- dir = ARGV.shift or fail msg
8
+ dir = ARGV.shift or fail msg
9
9
  dir = File.expand_path dir
10
10
  mkdir_p dir
11
11
 
data/bin/discover CHANGED
@@ -1,102 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'utils'
4
- include Utils::Patterns
5
- require 'term/ansicolor'
6
- require 'spruz/xt'
7
-
8
- class ::String
9
- include Term::ANSIColor
10
- end
11
-
12
- class ::File
13
- include Utils::FileXt
14
- end
15
-
16
- class Finder
17
- include Utils::Find
18
-
19
- def initialize(opts = {})
20
- @args = opts[:args] || {}
21
- @roots = opts[:roots] || []
22
- pattern_opts = opts.subhash(:pattern) | {
23
- :cset => @args['a'],
24
- :icase => @args['i'],
25
- }
26
- @pattern = @args['r'] ?
27
- RegexpPattern.new(pattern_opts) :
28
- FuzzyPattern.new(pattern_opts)
29
- @directory = @args['d']
30
- @only_directory = @args['D']
31
- @pathes = []
32
- end
33
-
34
- attr_reader :pathes
35
-
36
- def attempt_match?(path)
37
- stat = File.stat(path)
38
- stat.symlink? and stat = File.lstat(path)
39
- if @only_directory
40
- stat.directory?
41
- elsif @directory
42
- stat.directory? || stat.file? && (stat.size == 0 || File.ascii?(path))
43
- else
44
- stat.file? && (stat.size == 0 || File.ascii?(path))
45
- end
46
- rescue SystemCallError => e
47
- warn "Caught #{e.class}: #{e}"
48
- nil
49
- end
50
-
51
- def search
52
- pathes = []
53
- find(*@roots) { |file| pathes << file }
54
- pathes.flatten!
55
- pathes.uniq!
56
- pathes.map! { |p| a = File.split(p) ; a.unshift(p) ; a }
57
- @suffix = @args['I']
58
- pathes = pathes.map! do |path, dir, file|
59
- @suffix && @suffix != File.extname(file)[1..-1] and next
60
- if do_match = attempt_match?(path) and $DEBUG
61
- warn "Attempt match of #{path.inspect}"
62
- end
63
- if do_match and match = @pattern.match(file)
64
- if FuzzyPattern === @pattern
65
- current = 0
66
- bold_file = ''
67
- score, e = 0, 0
68
- for i in 1...(match.size)
69
- match[i] or next
70
- b = match.begin(i)
71
- bold_file << file[current...b]
72
- bold_file << file[b, 1].bold
73
- score += (b - e)
74
- e = match.end(i)
75
- current = b + 1
76
- end
77
- bold_file << match.post_match
78
- [ score, file.size, path, File.join(dir, bold_file) ]
79
- else
80
- bold_file = file[0...match.begin(0)] <<
81
- file[match.begin(0)...match.end(0)].bold <<
82
- file[match.end(0)..-1]
83
- [ 0, file.size, path, File.join(dir, bold_file) ]
84
- end
85
- end
86
- end
87
- pathes.compact!
88
- @pathes, output = pathes.sort.transpose.values_at(-2, -1)
89
- if !@args['e'] && output && !output.empty?
90
- puts output
91
- end
92
- self
93
- end
94
- end
95
-
4
+ include Utils
5
+ require 'spruz/go'
96
6
  include Spruz::GO
97
7
 
98
8
  def edit_files(pathes)
99
- system "edit #{pathes.map { |x| "'#{x}'" } * ' '}"
9
+ editor = Utils::Edit::Editor.new do |config|
10
+ config.wait = true
11
+ end
12
+ editor.edit(*pathes)
100
13
  end
101
14
 
102
15
  def usage
@@ -123,7 +36,7 @@ Options are
123
36
  end
124
37
 
125
38
  args = go 'I:a:rdDcieh'
126
- args['h'] and usage
39
+ args['h'] and usage
127
40
  pattern = ARGV.shift or usage
128
41
  roots = (ARGV.empty? ? [ Dir.pwd ] : ARGV).map { |f| File.expand_path(f) }
129
42
 
@@ -134,7 +47,9 @@ finder = Finder.new(
134
47
  :pattern => pattern,
135
48
  :args => args,
136
49
  :roots => roots
137
- ).search
50
+ ).search do |output|
51
+ puts output
52
+ end
138
53
  if args['e']
139
54
  edit_files finder.pathes
140
55
  end
data/bin/edit CHANGED
@@ -2,54 +2,69 @@
2
2
 
3
3
  require 'spruz/go'
4
4
  include Spruz::GO
5
- require 'utils/edit'
6
- include Utils::Edit
5
+ require 'utils'
6
+ include Utils
7
7
  require 'tempfile'
8
8
 
9
- $opt = go 'w'
9
+ def usage
10
+ puts <<-EOT
11
+ Usage: #{File.basename($0)} [OPTS] [PATHES]
10
12
 
11
- vim = locate_vim_binary
12
- servername = [ '--servername', "G#{ENV['USER'].upcase}" ]
13
- serverlist = `#{vim} -g --serverlist`.split
14
- remote = $opt['w'] ? '--remote-wait' : '--remote'
13
+ PATHES are the directory and file pathes that are opened in the vim.
15
14
 
16
- argv = ARGV.dup
17
- if argv.empty?
18
- if !STDIN.tty?
19
- file = File.new(File.join(Dir.tmpdir, "edit_tmp.#$$"), 'w')
15
+ Options are
16
+
17
+ -w open a buffer in vim and wait until it is deleted
18
+ -s read files and files:linenumbers from stdin and open them
19
+ -p DURATION pause for this many seconds to wait for vim's reaction
20
+ -h display this help
21
+
22
+ EOT
23
+ exit 1
24
+ end
25
+
26
+ $opt = go 'wp:sh'
27
+ $opt['h'] and usage
28
+
29
+ editor = Editor.new do |config|
30
+ config.wait = $opt['w']
31
+ config.pause_duration = ($opt['p'] || 1).to_i
32
+ end
33
+
34
+ if $opt['s']
35
+ begin
20
36
  until STDIN.eof?
21
- buffer = STDIN.read(8192)
22
- file.write buffer
37
+ line = STDIN.gets
38
+ line = line.sub(/.*?([^:\s]+:)/, '\1')
39
+ editor.edit(line)
23
40
  end
24
- file.close
25
- argv << file.path
26
- elsif serverlist.member?(servername)
27
- system *cmd(vim, '-g', servername, remote, "stupid_trick#{rand}")
28
- sleep 1
29
- exec *cmd(vim, '-g', servername, '--remote-send', '<ESC>:bw<CR>')
30
- else
31
- exec *cmd(vim, '-g', servername, remote)
41
+ rescue Interrupt
32
42
  end
33
- end
34
- unless argv.empty?
35
- lineno_re = /^\s*([^:]+):(\d+)/
36
- if argv.first =~ lineno_re
37
- for a in argv
38
- if a =~ lineno_re
39
- system *cmd(vim, '-g', servername, remote, $1)
40
- sleep 1
41
- system *cmd(vim, '-g', servername, '--remote-send', "<ESC>:#$2<CR>")
42
- else
43
- system *cmd(vim, '-g', servername, remote, a)
43
+ else
44
+ argv = ARGV.dup
45
+ if argv.empty?
46
+ if !STDIN.tty?
47
+ file = File.new(File.join(Dir.tmpdir, "edit_tmp.#$$"), 'w')
48
+ until STDIN.eof?
49
+ buffer = STDIN.read(8192)
50
+ file.write buffer
44
51
  end
45
- if a != argv.last
46
- STDOUT.print "Press enter to edit the next file."
47
- STDOUT.flush
48
- STDIN.gets
52
+ file.close
53
+ argv << file.path
54
+ else
55
+ editor.ensure_running
56
+ end
57
+ end
58
+ unless argv.empty?
59
+ if editor.file_linenumber?(argv.first)
60
+ editor.wait = argv.size > 1
61
+ for current_file in argv
62
+ STDOUT.puts "Edit #{current_file}"
63
+ editor.edit current_file
49
64
  end
65
+ else
66
+ editor.edit(*argv)
50
67
  end
51
- else
52
- exec *cmd(vim, '-g', servername, remote, *argv)
53
68
  end
54
69
  end
55
70
  exit 0
data/bin/search CHANGED
@@ -2,164 +2,18 @@
2
2
  # encoding: UTF-8
3
3
 
4
4
  require 'utils'
5
- include Utils::Patterns
6
- require 'term/ansicolor'
7
- require 'spruz/xt'
8
-
9
- class ::String
10
- include Term::ANSIColor
11
- end
12
-
13
- class ::File
14
- include Utils::FileXt
15
- end
16
-
17
- class Grepper
18
- PRUNE = /\A(\.svn|\.git|CVS|tmp)\Z/
19
- SKIP = /(\A\.|\.sw[pon]\Z|~\Z)/
20
-
21
- class Queue
22
- def initialize(max_size)
23
- @max_size, @data = max_size, []
24
- end
25
-
26
- attr_reader :max_size
27
-
28
- def data
29
- @data.dup
30
- end
31
-
32
- def push(x)
33
- @data.shift if @data.size > @max_size
34
- @data << x
35
- self
36
- end
37
- alias << push
38
- end
39
-
40
- def initialize(opts = {})
41
- @args = opts[:args] || {}
42
- @roots = opts[:roots] || []
43
- if n = @args.values_at(*%w[A B C]).compact.first
44
- if n.to_s =~ /\A\d+\Z/ and (n = n.to_i) >= 1
45
- @queue = Queue.new n
46
- else
47
- raise ArgumentError, "needs to be an integer number >= 1"
48
- end
49
- end
50
- @pathes = []
51
- pattern_opts = opts.subhash(:pattern) | {
52
- :cset => @args['a'],
53
- :icase => @args['i'],
54
- }
55
- @pattern = @args['R'] ?
56
- FuzzyPattern.new(pattern_opts) :
57
- RegexpPattern.new(pattern_opts)
58
- @name_pattern =
59
- if name_pattern = @args['N']
60
- RegexpPattern.new(:pattern => name_pattern)
61
- elsif name_pattern = @args['n']
62
- FuzzyPattern.new(:pattern => name_pattern)
63
- end
64
- @skip_pattern =
65
- if skip_pattern = @args['S']
66
- RegexpPattern.new(:pattern => skip_pattern)
67
- elsif skip_pattern = @args['s']
68
- FuzzyPattern.new(:pattern => skip_pattern)
69
- end
70
- end
71
-
72
- attr_reader :pathes
73
-
74
- def match(filename)
75
- @filename = filename
76
- bn = File.basename(filename)
77
- @output = []
78
- s = File.stat(filename)
79
- if s.directory? && bn =~ PRUNE
80
- $DEBUG and warn "Pruning '#{filename}'."
81
- Utils::Find.prune
82
- end
83
- if s.file? && bn !~ SKIP && (!@name_pattern || @name_pattern.match(bn))
84
- File.open(filename, 'rb') do |file|
85
- if file.binary? != true
86
- $DEBUG and warn "Matching '#{filename}'."
87
- match_lines file
88
- else
89
- $DEBUG and warn "Skipping binary file '#{filename}'."
90
- end
91
- end
92
- else
93
- $DEBUG and warn "Skipping '#{filename}'."
94
- end
95
- unless @output.empty?
96
- case
97
- when @args['l'], @args['e']
98
- @output.uniq!
99
- @pathes.concat @output
100
- else
101
- STDOUT.puts @output
102
- end
103
- @output.clear
104
- end
105
- self
106
- rescue SystemCallError => e
107
- warn "Caught #{e.class}: #{e}"
108
- nil
109
- end
110
-
111
- def match_lines(file)
112
- for line in file
113
- if m = @pattern.match(line)
114
- @skip_pattern and @skip_pattern =~ line and next
115
- line[m.begin(0)...m.end(0)] = m[0].on_yellow
116
- @queue and @queue << line
117
- if @args['l'] or @args['e']
118
- @output << @filename
119
- elsif @args['L']
120
- @output << "#{@filename}:#{file.lineno}"
121
- else
122
- @output << "#{@filename}:#{file.lineno}".red
123
- if @args['B'] or @args['C']
124
- @output.concat @queue.data
125
- else
126
- @output << line
127
- end
128
- if @args['A'] or @args['C']
129
- where = file.tell
130
- @queue.max_size.times do
131
- file.eof? and break
132
- line = file.readline
133
- @queue << line
134
- @output << line
135
- end
136
- file.seek where
137
- end
138
- end
139
- else
140
- @queue and @queue << line
141
- end
142
- end
143
- end
144
-
145
- def search
146
- @suffix = @args['I']
147
- for dir in @roots
148
- Utils::Find.find(dir) do |filename|
149
- if !@suffix || @suffix == File.extname(filename)[1..-1]
150
- match(filename)
151
- end
152
- end
153
- end
154
- @pathes.sort!
155
- self
156
- end
157
- end
158
-
5
+ include Utils
6
+ require 'spruz/go'
159
7
  include Spruz::GO
160
8
 
161
9
  def edit_files(pathes)
162
- system "edit #{pathes.map { |x| "'#{x}'" } * ' '}"
10
+ editor = Utils::Edit::Editor.new do |config|
11
+ config.wait = true
12
+ end
13
+ for (file, linenumber) in pathes.map { |p| p.split(':') }
14
+ STDERR.puts "Edit #{file}:#{linenumber}"
15
+ editor.edit_linenumber(file, linenumber)
16
+ end
163
17
  end
164
18
 
165
19
  def usage
@@ -193,7 +47,7 @@ Options are
193
47
  end
194
48
 
195
49
  args = go 'I:A:B:C:s:S:n:N:a:RcilLeh'
196
- args['h'] and usage
50
+ args['h'] and usage
197
51
  pattern = ARGV.shift or usage
198
52
  roots = (ARGV.empty? ? [ Dir.pwd ] : ARGV).map { |f| File.expand_path(f) }
199
53