fileutil 0.0.2 → 0.0.85

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/fileutil.rb +73 -40
  2. metadata +7 -6
data/lib/fileutil.rb CHANGED
@@ -1,36 +1,59 @@
1
1
  class File
2
- # find next (in the same direction with
3
- # last search)
4
- def find!
5
- if @find_registry[:method] == :ffind
2
+ # repeat last ff/rew command
3
+ def repeat
4
+ if @find_registry[:method] == :ff
6
5
  seek(1,1)
7
- ffind(@find_registry[:re])
6
+ if ff(@find_registry[:re]).nil?
7
+ seek(-1,1)
8
+ return nil
9
+ else
10
+ return self
11
+ end
8
12
  else
9
- rfind(@find_registry[:re])
13
+ rew(@find_registry[:re])
10
14
  end
11
15
  end
12
- # find all matches in the file
13
- # regardless of the current position
16
+ # find all occurences of args (with or)
17
+ # in the file egardless of the current
18
+ # position
14
19
  def findall *args
15
20
  p = pos
16
21
  seek(0)
17
- results = [ ffind(*args) ]
18
- while !find!.nil?
22
+ results = [ ff(*args).pos ]
23
+ while !repeat.nil?
19
24
  results.push pos
20
25
  end
21
26
  seek(p)
22
27
  return results
23
28
  end
24
- # find the last matching text backwards
25
- # starting from the current position.
26
- # accepts multiple search strings and/or
27
- # regexes, returns the position of the first
28
- # one it encounters, also move to its position
29
- def rfind *args
29
+ # do not rewind, just give
30
+ # the position that rew would
31
+ # rewind to.
32
+ def rew? *args
33
+ p = pos
34
+ p1 = rew(*args)
35
+ p1.nil? ? p1 = nil : p1 = pos
36
+ seek p
37
+ return p1
38
+ end
39
+ # do not fast forward, just give
40
+ # the position that ff would fast
41
+ # forward to.
42
+ def ff? *args
43
+ p = pos
44
+ p1 = ff(*args)
45
+ p1.nil? ? p1 = nil : p1 = pos
46
+ seek p
47
+ return p1
48
+ end
49
+ # rewind until it finds one of args
50
+ # supplied as string or regex
51
+ # returns nil if nothing found
52
+ def rew *args
30
53
  p = pos
31
54
  psearch = p
32
55
  re = Regexp.union(args)
33
- @find_registry = { :method => :rfind, :re => re }
56
+ @find_registry = { :method => :rew, :re => re }
34
57
  begin
35
58
  break if psearch == 0
36
59
  512 > pos ? buffer = pos : buffer = 512 # buffer = [512, p].min
@@ -50,33 +73,30 @@ class File
50
73
  return nil
51
74
  else
52
75
  seek(psearch + check)
53
- return pos
76
+ return self
54
77
  end
55
78
  rescue
56
79
  seek p
57
80
  return nil
58
81
  end
59
82
  end
60
- # find the last matching text forwards
61
- # starting from the current position.
62
- # accepts multiple search strings and/or
63
- # regexes, returns the position of the first
64
- # one it encounters, also move to its position
65
- def ffind *args
83
+ # fast forward until it finds one of args
84
+ # supplied as string or regex
85
+ # returns nil if nothing found
86
+ def ff *args
66
87
  p = pos
67
88
  re = Regexp.union(args)
68
- @find_registry = { :method => :ffind, :re => re }
89
+ @find_registry = { :method => :ff, :re => re }
69
90
  buffer = 512
70
91
  begin
71
92
  psearch = pos
72
- chunk = read(buffer)
73
- break if eof?
74
- seek(-80,1)
93
+ chunk = read(buffer) || ''
94
+ seek(-80,1) unless eof?
75
95
  check = chunk.index(re)
76
- end while check.nil?
96
+ end while check.nil? and !eof?
77
97
  begin
78
98
  seek(psearch + check)
79
- return pos
99
+ return self
80
100
  rescue
81
101
  seek p
82
102
  return nil
@@ -87,6 +107,8 @@ class File
87
107
  # start from current line (default is start)
88
108
  # from beggining; reset for do not change
89
109
  # current position (default).
110
+ # head leaves cursor at the beginning of the
111
+ # first line it doesn't return if reset false
90
112
  def head(n, cur=false, reset=true)
91
113
  #eof guard
92
114
  p = pos
@@ -98,18 +120,21 @@ class File
98
120
  lines.push(readline.chomp)
99
121
  end
100
122
  seek(p) if reset
101
- return lines
123
+ return lines unless lines.length == 0
124
+ return nil
102
125
  end
103
126
  # unix tail like utility, returns lines
104
127
  # as an array. optional arguments cur for
105
128
  # start from current line (default is start)
106
129
  # from end; reset for do not change
107
130
  # current position (default).
131
+ # tail leaves 'cursor' at the end of
132
+ # last line it doesn't return if reset false
108
133
  def tail(n, cur=false, reset=true)
109
134
  p = pos
110
- chunks = ''
135
+ chunks = '$'
111
136
  lines = 0
112
- cur ? line_end : seek(size)
137
+ cur ? line_end.seek(1,1) : seek(size)
113
138
  begin
114
139
  p1 = pos
115
140
  p1 < 512 ? buffer = p1 : buffer = 512
@@ -118,20 +143,28 @@ class File
118
143
  lines += chunk.count("\n")
119
144
  chunks = chunk + chunks
120
145
  seek(-chunk.size,1)
121
- end while lines < ( n + 1 ) && p1 != 0
146
+ end while lines < ( n + 1 ) && p1 > buffer
122
147
  ary = chunks.split(/\n/)
123
- reset ? seek(p) : seek(p1-1-(ary.last(n).join("\n").length))
124
- return ary.last(n)
148
+ ary.pop
149
+ if reset
150
+ seek(p)
151
+ else
152
+ seek(p1-2-(ary.last(n).join("\n").length)) rescue seek 0
153
+ end
154
+ return ary.last(n).reverse
125
155
  end
126
156
  # move to the first char of current line
127
157
  def line_start
128
- unless readchar == "\n"
129
- return rfind(/^/)
158
+ if pos > 0
159
+ seek(-1,1)
160
+ return self if readchar == "\n"
161
+ elsif pos == 0
162
+ return self
130
163
  end
131
- return pos
164
+ return rew(/^/)
132
165
  end
133
166
  # move to the last char of current line
134
167
  def line_end
135
- return ffind(/$/)
168
+ return ff(/$/)
136
169
  end
137
170
  end
metadata CHANGED
@@ -1,18 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fileutil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
4
  prerelease:
5
+ version: 0.0.85
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sencer Selcuk
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-11 00:00:00.000000000 Z
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: use ffind, rfind, findall, find! to find text in a file etc.
15
- email: sencer@selc.uk
14
+ description: use ff, rew, line_start, line_end to move in a file head and tail -to
15
+ get lines, and to move line-by-line
16
+ email: sencerselcuk@twitter
16
17
  executables: []
17
18
  extensions: []
18
19
  extra_rdoc_files: []
@@ -25,17 +26,17 @@ rdoc_options: []
25
26
  require_paths:
26
27
  - lib
27
28
  required_ruby_version: !ruby/object:Gem::Requirement
28
- none: false
29
29
  requirements:
30
30
  - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
32
  version: '0'
33
- required_rubygems_version: !ruby/object:Gem::Requirement
34
33
  none: false
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
36
  - - ! '>='
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
+ none: false
39
40
  requirements: []
40
41
  rubyforge_project:
41
42
  rubygems_version: 1.8.23