fileutil 0.0.2 → 0.0.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/fileutil.rb +73 -40
 - metadata +7 -6
 
    
        data/lib/fileutil.rb
    CHANGED
    
    | 
         @@ -1,36 +1,59 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            class File
         
     | 
| 
       2 
     | 
    
         
            -
              #  
     | 
| 
       3 
     | 
    
         
            -
               
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       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 
     | 
    
         
            -
                   
     | 
| 
      
 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 
     | 
    
         
            -
                   
     | 
| 
      
 13 
     | 
    
         
            +
                  rew(@find_registry[:re])
         
     | 
| 
       10 
14 
     | 
    
         
             
                end
         
     | 
| 
       11 
15 
     | 
    
         
             
              end
         
     | 
| 
       12 
     | 
    
         
            -
              # find all  
     | 
| 
       13 
     | 
    
         
            -
              #  
     | 
| 
      
 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 = [  
     | 
| 
       18 
     | 
    
         
            -
                while ! 
     | 
| 
      
 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 
     | 
    
         
            -
              #  
     | 
| 
       25 
     | 
    
         
            -
              #  
     | 
| 
       26 
     | 
    
         
            -
              #  
     | 
| 
       27 
     | 
    
         
            -
               
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
             
     | 
| 
      
 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 => : 
     | 
| 
      
 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  
     | 
| 
      
 76 
     | 
    
         
            +
                    return self
         
     | 
| 
       54 
77 
     | 
    
         
             
                  end
         
     | 
| 
       55 
78 
     | 
    
         
             
                rescue
         
     | 
| 
       56 
79 
     | 
    
         
             
                  seek p
         
     | 
| 
       57 
80 
     | 
    
         
             
                  return nil
         
     | 
| 
       58 
81 
     | 
    
         
             
                end
         
     | 
| 
       59 
82 
     | 
    
         
             
              end
         
     | 
| 
       60 
     | 
    
         
            -
              #  
     | 
| 
       61 
     | 
    
         
            -
              #  
     | 
| 
       62 
     | 
    
         
            -
              #  
     | 
| 
       63 
     | 
    
         
            -
               
     | 
| 
       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 => : 
     | 
| 
      
 89 
     | 
    
         
            +
                @find_registry = { :method => :ff, :re => re }
         
     | 
| 
       69 
90 
     | 
    
         
             
                buffer = 512
         
     | 
| 
       70 
91 
     | 
    
         
             
                begin
         
     | 
| 
       71 
92 
     | 
    
         
             
                  psearch = pos
         
     | 
| 
       72 
     | 
    
         
            -
                  chunk = read(buffer)
         
     | 
| 
       73 
     | 
    
         
            -
                   
     | 
| 
       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  
     | 
| 
      
 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  
     | 
| 
      
 146 
     | 
    
         
            +
                end while lines < ( n + 1 ) && p1 > buffer
         
     | 
| 
       122 
147 
     | 
    
         
             
                ary = chunks.split(/\n/)
         
     | 
| 
       123 
     | 
    
         
            -
                 
     | 
| 
       124 
     | 
    
         
            -
                 
     | 
| 
      
 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 
     | 
    
         
            -
                 
     | 
| 
       129 
     | 
    
         
            -
                   
     | 
| 
      
 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  
     | 
| 
      
 164 
     | 
    
         
            +
                return rew(/^/) 
         
     | 
| 
       132 
165 
     | 
    
         
             
              end
         
     | 
| 
       133 
166 
     | 
    
         
             
              # move to the last char of current line
         
     | 
| 
       134 
167 
     | 
    
         
             
              def line_end
         
     | 
| 
       135 
     | 
    
         
            -
                return  
     | 
| 
      
 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- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-12-12 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies: []
         
     | 
| 
       14 
     | 
    
         
            -
            description: use  
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
      
 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
         
     |