简单版more的实现,简单版more实现,more.py#!/us


more.py

#!/usr/bin/env pyton# _*_ coding: gbk _*_# 思路参考Unix/Linux编程实践教程from sys import argv, exit, stdout, stdinfrom os import path, isattyfrom getopt import getopt, GetoptErrorhelp_info = ["more.py [-h] file list...",        "\t-h\t显示帮助信息"]#下面_Getch代码来源:#http://code.activestate.com/recipes/134892/class _Getch:    """Gets a single character from standard input.  Does not echo to the screen."""    def __init__(self):        try:            self.impl = _GetchWindows()        except ImportError, err:            self.impl = _GetchUnix()    def __call__(self): return self.impl()class _GetchUnix:    def __init__(self):        import tty, sys    def __call__(self):        import sys, tty, termios        fd = sys.stdin.fileno()        old_settings = termios.tcgetattr(fd)        try:            tty.setraw(sys.stdin.fileno())            ch = sys.stdin.read(1)        finally:            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)        return chclass _GetchWindows:    def __init__(self):        import msvcrt    def __call__(self):        import msvcrt        return msvcrt.getch()getch = _Getch()def usage():    for l in help_info:        print ldef do_more(lines):     num_of_lines = 0            i = 0    while i < len(lines):        num_of_lines = num_of_lines + 1         stdout.write(lines[i])        i += 1        if num_of_lines >= 12:            num_of_lines = see_more(num_of_lines)        if num_of_lines < 0:            i += num_of_lines        if i < 0:            i = 0def see_more(num_of_lines):    answer = getch()    if answer== " ":        return 0    elif answer == "\r":        return (num_of_lines - 1)    elif answer == "q":            exit(1)     elif answer == "b":        return (-num_of_lines)      else:        return 0if __name__ == "__main__":    try:        opts, args = getopt(argv[1:], "h")                  except GetoptError, err:        print str(err)        usage()        exit(2)    for o, a in opts:        if o == "-h":            usage()            exit(0)         else:            print "Unkown options"            usage()            exit(2)    if len(args) == 0:        usage()    else:        for file in args:            if not path.exists(file):                print file + ": No such file or directory"            else:                print file + ":"                fp = open(file, 'r')                lines = fp.readlines()                do_more(lines)

评论关闭