#!/usr/bin/python2.1 -Wall import sys, os from optik import OptionParser parser = OptionParser("usage: %prog [options] [files]") parser.add_option("-p", "--printer", default='lpr', help="use this command instead of 'lpr' to print") parser.add_option("-f", "--font", action='append', default=['fixed', 'vga', 'drift'], help="try to use this font before 'drift', 'vga' and 'fixed'") (options, args) = parser.parse_args() options.font.reverse() if len(args) == 0: file = sys.stdin else: file = open(args[0]) import re report = "".join(file.readlines()) stripped = re.sub(r'.|[ ]', "", report) lower_stripped = stripped.lower() from gtk import * font = None for fontres in options.font: try: font = load_font(fontres) break except RuntimeError: pass if font is None: print "gpager was unable to load fixed font: layout will not line up like the printed output." searched = "" n = 0L pos = -1 def test(last): global searched, pos if n==last: searched = "" status.pop(1) text.selection_remove_all() pos = -1 def key_pressed(widget, event): global searched, n, pos n += 1 ns = None timeout_add(3000, test, n) if event.string.isalnum(): ns = searched + event.string pos = lower_stripped.find(searched.lower()) if event.string is "": ns = searched pos = lower_stripped.find(searched.lower(), pos+1) if pos is -1: pos = lower_stripped.find(searched.lower()) if ns: if pos is not -1: status.pop(1) searched = ns text.select_region(pos, pos+len(searched)) scrolledwindow.get_vadjustment().set_value((font.ascent+font.descent)*(lower_stripped[:pos].count("\n"))) status.push(1, "Encontré «%s» -- pulse Ctrl+G para repetir" % searched) else: status.pop(1) status.push(1, "No encontré «%s»" % ns) def printit(*a): status.push(1, "Imprimiendo...") timeout_add(10, really_print) def really_print(*a): try: pipe = os.popen(options.printer, "w") print >> pipe, report pipe.flush() mainquit() except Exception, msg: status.pop(1) status.push(1, str(msg)) text = GtkText() text.set_editable(FALSE) text.set_line_wrap(FALSE) text.insert(font, None, None, stripped) scrolledwindow = GtkScrolledWindow() scrolledwindow.add(text) scrolledwindow.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC) print_button = GtkButton("Imprimir") print_button.connect("clicked", printit) close_button = GtkButton("Cerrar") close_button.connect("clicked", mainquit) close_button.set_flags(CAN_DEFAULT) buttonbox = GtkHButtonBox() buttonbox.add(print_button) buttonbox.add(close_button) status = GtkStatusbar() vbox = GtkVBox(FALSE, 0) vbox.pack_start(scrolledwindow, TRUE, TRUE, 0) vbox.pack_start(buttonbox, FALSE, FALSE, 0) vbox.pack_start(status, FALSE, FALSE, 0) window = GtkWindow(WINDOW_TOPLEVEL, "gpager") window.connect("destroy", mainquit) window.connect("key_press_event", key_pressed) window.add(vbox) window.set_default_size(130*font.width("m"), 50*(font.ascent + font.descent) ) window.set_position(WIN_POS_CENTER) window.show_all() close_button.grab_default() text.grab_focus() mainloop()