Permalink
Browse files

lib/lingo/attendee/text_reader.rb: Progress reporting.

  • Loading branch information...
1 parent cb22acc commit 3babd8eca538a0014b932f547948200cf0201e9b @blackwinter committed Mar 6, 2012
Showing with 115 additions and 63 deletions.
  1. +1 −0 lib/lingo.rb
  2. +27 −17 lib/lingo/attendee/text_reader.rb
  3. +3 −43 lib/lingo/database/show_progress.rb
  4. +81 −0 lib/lingo/show_progress.rb
  5. +1 −1 lingo-all.cfg
  6. +1 −1 lingo.cfg
  7. +1 −1 lir.cfg
View
@@ -278,6 +278,7 @@ def warn(*msg)
require_relative 'lingo/cachable'
require_relative 'lingo/reportable'
require_relative 'lingo/agenda_item'
+require_relative 'lingo/show_progress'
require_relative 'lingo/database'
require_relative 'lingo/language'
require_relative 'lingo/attendee'
@@ -109,9 +109,10 @@ class TextReader < self
# TODO: lir-record-pattern abkürzen
# Interpretation der Parameter
def init
- @files = Array(get_key('files', '-'))
- @chomp = get_key('chomp', true)
- @filter = get_key('filter', false)
+ @files = Array(get_key('files', '-'))
+ @chomp = get_key('chomp', true)
+ @filter = get_key('filter', false)
+ @progress = get_key('progress', false)
@lir = get_key('lir-record-pattern', nil)
@lir = Regexp.new(@lir) if @lir
@@ -128,34 +129,43 @@ def control(cmd, param)
# Gibt eine Datei zeilenweise in den Ausgabekanal
def spool(path)
- unless stdin?(path)
+ unless stdin = stdin?(path)
raise FileNotFoundError.new(path) unless File.exist?(path)
inc('Anzahl Dateien')
- add('Anzahl Bytes', File.stat(path).size)
+ add('Anzahl Bytes', size = File.size(path))
+
+ size = nil unless @progress
end
forward(STR_CMD_FILE, path)
- filter(path) { |line|
- inc('Anzahl Zeilen')
+ ShowProgress.new(self, size, path) { |progress|
+ filter(path, stdin) { |line, pos|
+ inc('Anzahl Zeilen')
+ progress[pos]
- line.chomp! if @chomp
+ line.chomp! if @chomp
- if line =~ @lir
- forward(STR_CMD_RECORD, $1)
- else
- forward(line) unless line.empty?
- end
+ if line =~ @lir
+ forward(STR_CMD_RECORD, $1)
+ else
+ forward(line) unless line.empty?
+ end
+ }
}
forward(STR_CMD_EOF, path)
end
- def filter(path, &block)
- io = stdin?(path) ?
- @lingo.config.stdin.set_encoding(ENC) :
- File.open(path, 'rb', encoding: ENC)
+ def filter(path, stdin = stdin?(path))
+ io, block = stdin ? [
+ @lingo.config.stdin.set_encoding(ENC),
+ lambda { |line| yield line, 0 }
+ ] : [
+ File.open(path, 'rb', encoding: ENC),
+ lambda { |line| yield line, io.pos }
+ ]
case @filter == true ? file_type(path, io) : @filter.to_s
when /html/i then io = filter_html(io)
@@ -28,50 +28,10 @@ class Lingo
class Database
- class ShowProgress
+ class ShowProgress < ShowProgress
- def initialize(src, max, act = true)
- @out, @act = src.instance_variable_get(:@lingo).config.stderr, act
-
- # To get the length of the formatted string we have
- # to actually substitute the placeholder.
- fmt = ' [%3d%%]'
- len = (fmt % 0).length
-
- # Now we know how far to "go back" to
- # overwrite the formatted string...
- back = "\b" * len
-
- @fmt = fmt + back
- @clr = ' ' * len + back
-
- print src.instance_variable_get(:@config)['name'], ': '
-
- @rat, @cnt, @next = max / 100.0, 0, 0
- print 'convert '
- step
-
- yield self
-
- print "#{@clr}ok\n"
- end
-
- def [](value)
- @cnt = value
- step if @cnt >= @next
- end
-
- private
-
- def step
- percent = @cnt / @rat
- @next = (percent + 1) * @rat
-
- print @fmt % percent if percent.finite?
- end
-
- def print(*args)
- @out.print(*args) if @act
+ def initialize(obj, max, act = true)
+ super(obj, max, obj.instance_variable_get(:@config)['name'], act, 'convert')
end
end
View
@@ -0,0 +1,81 @@
+# encoding: utf-8
+
+#--
+###############################################################################
+# #
+# Lingo -- A full-featured automatic indexing system #
+# #
+# Copyright (C) 2005-2007 John Vorhauer #
+# Copyright (C) 2007-2012 John Vorhauer, Jens Wille #
+# #
+# Lingo is free software; you can redistribute it and/or modify it under the #
+# terms of the GNU Affero General Public License as published by the Free #
+# Software Foundation; either version 3 of the License, or (at your option) #
+# any later version. #
+# #
+# Lingo is distributed in the hope that it will be useful, but WITHOUT ANY #
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
+# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
+# more details. #
+# #
+# You should have received a copy of the GNU Affero General Public License #
+# along with Lingo. If not, see <http://www.gnu.org/licenses/>. #
+# #
+###############################################################################
+#++
+
+class Lingo
+
+ class ShowProgress
+
+ def initialize(obj, max, name = nil, doit = true, text = 'progress')
+ return yield self unless max && doit
+
+ @out = obj.instance_variable_get(:@lingo).config.stderr
+
+ # To get the length of the formatted string we have
+ # to actually substitute the placeholder.
+ fmt = ' [%3d%%]'
+ len = (fmt % 0).length
+
+ # Now we know how far to "go back" to
+ # overwrite the formatted string...
+ back = "\b" * len
+
+ @fmt = fmt + back
+ @clr = ' ' * len + back
+
+ print name, ': ' if name
+
+ @rat, @cnt, @next = max / 100.0, 0, 0
+ print text
+ step
+
+ yield self
+
+ print "#{@clr} done.\n"
+ end
+
+ def [](value)
+ if defined?(@cnt)
+ @cnt = value
+ step if @cnt >= @next
+ end
+ end
+
+ private
+
+ def step
+ percent = @cnt / @rat
+ @next = (percent + 1) * @rat
+
+ print @fmt % percent if percent.finite?
+ end
+
+ def print(*args)
+ @out.print(*args)
+ end
+
+ end
+
+end
View
@@ -11,7 +11,7 @@ meeting:
#
# Angegebene Datei zeilenweise einlesen und verarbeitet
- - text_reader: { files: '$(files)' }
+ - text_reader: { files: '$(files)', progress: true }
########################################
View
@@ -11,7 +11,7 @@ meeting:
#
# Angegebene Datei zeilenweise einlesen und verarbeitet
- - text_reader: { files: '$(files)' }
+ - text_reader: { files: '$(files)', progress: true }
########################################
View
@@ -15,7 +15,7 @@ meeting:
#
# Angegebene Datei zeilenweise einlesen und verarbeiten
- - text_reader: { files: '$(files)', lir-record-pattern: '^\[(\d+)\.\]' }
+ - text_reader: { files: '$(files)', lir-record-pattern: '^\[(\d+)\.\]', progress: true }
########################################

0 comments on commit 3babd8e

Please sign in to comment.