python类COLS的实例源码

treesel.py 文件源码 项目:treesel 作者: mcchae 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def c_main(stdscr):
    cargo_cult_routine(stdscr)
    stdscr.nodelay(0)
    mydir = factory(start)
    mydir.expand()
    curidx = 3
    pending_action = None
    pending_save = False

    while True:
        stdscr.clear()
        curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
        line = 0
        offset = max(0, curidx - curses.LINES + 3)
        for data, depth in mydir.traverse():
            if line == curidx:
                stdscr.attrset(curses.color_pair(1) | curses.A_BOLD)
                if pending_action:
                    getattr(data, pending_action)()
                    pending_action = None
                elif pending_save:
                    global result
                    result = data.name
                    return
            else:
                stdscr.attrset(curses.color_pair(0))
            if 0 <= line - offset < curses.LINES - 1:
                stdscr.addstr(line - offset, 0,
                              data.render(depth, curses.COLS))
            line += 1
        stdscr.refresh()
        ch = stdscr.getch()
        if ch == curses.KEY_UP:
            curidx -= 1
        elif ch == curses.KEY_DOWN:
            curidx += 1
        elif ch == curses.KEY_PPAGE:
            curidx -= curses.LINES
            if curidx < 0: curidx = 0
        elif ch == curses.KEY_NPAGE:
            curidx += curses.LINES
            if curidx >= line: curidx = line - 1
        elif ch == curses.KEY_RIGHT:
            pending_action = 'expand'
        elif ch == curses.KEY_LEFT:
            pending_action = 'collapse'
        elif ch == ESC:
            return
        elif ch == ord('\n'):
            pending_save = True

        curidx %= line


################################################################################
game.py 文件源码 项目:wpm 作者: cslarsen 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def update(self, browse, head, quote, position, incorrect, author, title,
            typed, wpm, average):
        cols = curses.COLS
        lengths = word_wrap(quote, cols - 1)
        sx, sy = screen_coords(lengths, position)
        h = len(lengths)

        # Show header
        self.window.addstr(0, 0, head + " "*(cols - len(head)),
                curses.color_pair(2))

        if browse:
            # Display quote
            color = curses.color_pair(4 if browse == 1 else 3)
            for y, length in enumerate(lengths, 2):
                self.window.addstr(y, 0, quote[:length], color)
                quote = quote[1+length:]

            # Show author
            credit = u"— %s, %s" % (author, title)
            self.cheight = 4 + h + self.column(3+h, cols - 10, cols//2, credit,
                    curses.color_pair(6), False)
            if browse >= 2:
                typed = "You scored %.1f wpm%s " % (wpm, "!" if wpm > average
                        else ".")
            else:
                typed = ""
            typed += "Use arrows/space to browse, esc to quit, or start typing."
        elif position < len(quote):
            color = curses.color_pair(3 if incorrect == 0 else 1)
            typed = "> " + typed

            if position + incorrect < len(quote):
                sx, sy = screen_coords(lengths, position + incorrect - 1)
                self.window.chgat(2 + sy, max(sx, 0), 1, color)

                sx, sy = screen_coords(lengths, position + incorrect + 1)
                self.window.chgat(2 + sy, sx, curses.color_pair(4))

        # Show typed text
        if self.cheight < curses.LINES:
            self.window.move(self.cheight, 0)
            self.window.clrtoeol()
            self.window.addstr(self.cheight, 0, typed, curses.color_pair(7))
        if browse > 1:
            # If done, highlight score
            self.window.chgat(self.cheight, 11,
                len(str("%.1f" % wpm)), curses.color_pair(9))

        # Move cursor to current position in text before refreshing
        if browse < 1:
            sx, sy = screen_coords(lengths, position + incorrect)
            self.window.move(2 + sy, min(sx, cols - 1))
        else:
            self.window.move(2, 0)

        self.window.refresh()
main.py 文件源码 项目:Music-Scraper 作者: srivatsan-ramesh 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def main():
    """
    The entry point for the app. Called when music-scraper is typed in terminal.
    Starts the GUI and starts the scraping process after the input is given
    """
    curses.initscr()
    if curses.COLS < 80 or curses.LINES < 5:
        curses.endwin()
        print('Terminal\'s dimensions are too small')
        return

    process = CrawlerProcess({'LOG_ENABLED': False})

    def gui_input(screen):
        GUI.screen = screen
        curses.start_color()
        GUI.screen.keypad(1)
        curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_CYAN)
        GUI.high_light_text = curses.color_pair(1)
        GUI.normal_text = curses.A_NORMAL
        GUI.box = curses.newwin(curses.LINES, curses.COLS, 0, 0)
        GUI.message = GUI.get_input()

    curses.wrapper(gui_input)
    s = request.quote(GUI.message)

    MusicSpider.start_urls = [
        "http://www.google.com/search?q=" + s,
    ]
    process.crawl(MusicSpider)
    thread = GUIThread(process, start_gui)
    thread.start()
    process.start()
    if not GUI.gui_stopped:
        if len(GUI.strings) == 0:
            GUI.box.erase()
            GUI.box.addstr(1, 1, "No Results Found... Try with Some other keywords.", GUI.high_light_text)
            GUI.add_bottom_menus()
            GUI.screen.refresh()
            GUI.box.refresh()
        else:
            GUI.box.addstr(curses.LINES - 2, 1, "Completed Scraping !!", GUI.high_light_text)
            GUI.add_bottom_menus()
            GUI.screen.refresh()
            GUI.box.refresh()
zmqchat.py 文件源码 项目:zmq-chat 作者: jnthnwn 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def main(stdscr):
    config_file = args.config_file if args.config_file is not None else 'zmqchat.cfg'
    config = configparser.ConfigParser()
    config.read(config_file)
    config = config['default']

    receiver = zmq.Context().instance().socket(zmq.PAIR)
    receiver.bind("inproc://clientchat")
    sender = zmq.Context().instance().socket(zmq.PAIR)
    sender.connect("inproc://clientchat")
    client = ClientChat(args.username, config['server_host'],
                        config['chat_port'], receiver)
    client.run()

    display_receiver = zmq.Context().instance().socket(zmq.PAIR)
    display_receiver.bind("inproc://clientdisplay")
    display_sender = zmq.Context().instance().socket(zmq.PAIR)
    display_sender.connect("inproc://clientdisplay")
    display = ClientDisplay(config['server_host'], config['display_port'], display_sender)
    display.run()

    ### curses set up
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
    curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
    # ensure that user input is echoed to the screen
    curses.echo()
    curses.curs_set(0)

    window_height = curses.LINES
    window_width = curses.COLS
    division_line =  int(window_height * 0.8)

    # instaniate two pads - one for displaying received messages
    # and one for showing the message the user is about to send off
    top_pad = stdscr.subpad(division_line, window_width, 0, 0)
    bottom_pad = stdscr.subpad(window_height - division_line, window_width, division_line, 0)

    top_thread = threading.Thread(target=start_top_window, args=(top_pad, display_receiver))
    top_thread.daemon = True
    top_thread.start()

    bottom_thread = threading.Thread(target=start_bottom_window, args=(bottom_pad, sender))
    bottom_thread.daemon = True
    bottom_thread.start()

    top_thread.join()
    bottom_thread.join()
__main__.py 文件源码 项目:asciidots 作者: aaronduino 项目源码 文件源码 阅读 63 收藏 0 点赞 0 评论 0
def __init__(self, env, ticks, silent, debug, compat_debug, debug_lines, autostep_debug, output_limit):
        """

        :param dots.environment.Env env: The env of the interpreter
        :param int ticks: The max number of ticks for the program
        :param bool silent: True to turn off all outputs
        :param bool debug: True to show the execution of the program
        :param bool compat_debug: True to show the debug with only builtin functions
        :param int debug_lines: The number of lines to show the debug
        :param float autostep_debug: The timebetween automatic ticks. 0 disables the auto ticks.
        :param int output_limit: The max number of outputs for the program
        """
        super().__init__(env)

        # if it is zero or false, we don't want to stop
        self.ticks_left = ticks or float('inf')
        self.outputs_left = output_limit or float('inf')

        self.silent = silent
        self.debug = debug
        self.compat_debug = compat_debug
        self.debug_lines = debug_lines
        self.debug_cols = terminalsize.get_terminal_size()[0] - 1
        self.autostep_debug = autostep_debug

        self.compat_logging_buffer = ''
        self.compat_logging_buffer_lines = terminal_lines - debug_lines - 1

        self.first_tick = True

        if self.debug and not self.compat_debug:
            self.logging_loc = 0
            self.logging_x = 1

            self.stdscr = curses.initscr()

            curses.start_color()

            curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
            curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
            curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
            curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)

            curses.noecho()

            # hides the cursor
            curses.curs_set(False)

            # defining the two main parts of the screen: the view of the program
            self.win_program = curses.newwin(self.debug_lines, curses.COLS, 0, 0)
            # and pad for the output of the prog
            self.logging_pad = curses.newpad(1000, curses.COLS - 1)

            def signal_handler(signal, frame):
                self.on_finish()
                sys.exit(0)

            signal.signal(signal.SIGINT, signal_handler)


问题


面经


文章

微信
公众号

扫码关注公众号