示例#1
0
def main():
    t = Terminal()

    def update(s):
        t.style_reset()
        t.clear_line()
        fmt = ""
        try:
            col, fmt = parse_color(s)
            t.style(Color.rgb(col[0], col[1], col[2]).bg())
            t.write(" " * SWATCH_WIDTH)
            t.style_reset()
            t.write(" ")
        except ColorParseError:
            t.write(">" * SWATCH_WIDTH, " ")
        finally:
            t.cursor_save()
            t.style(Color.hex(0x909090))
            t.cursor_move(0, 1).clear_to_end().write(fmt)
            t.cursor_restore().style_reset()
            t.write(s)

    with t.managed():
        t.set_cbreak(True)

        # make space
        t.writeln()
        t.cursor_move(0, -1).flush()

        try:
            # read and parse color
            col_str = read_line(t, update)
            col, fmt = parse_color(col_str)

            t.writeln().writeln()

            # write converted colors
            formats = (("RGBA", format_rgba(col)), ("RGBA (hex)",
                                                    format_hex_rgba(col)),
                       ("RGB (hex)", format_hex_rgb(col)))
            format_name_width = max((len(f[0]) for f in formats)) + 2
            for name, value in formats:
                padding = format_name_width - (len(name) + 1)
                t.style(Color.hex(0x909090)).write(name, ":", " " * padding)
                t.style_reset().writeln(value)
            t.flush()
        except ColorParseError:
            t.writeln().writeln()
            t.writeln("Not a color.")
示例#2
0
        for i, word in enumerate(s.split(" ")):
            if i > 0:
                t.write(" ")
            try:
                int(word)
                t.style(NamedColor("red"))
            except:
                pass
            t.write(word).style_reset()

    def autocomplete(word):
        d = os.listdir(os.getcwd())
        matches = [w for w in d if w.startswith(word)]
        return matches[0] if len(matches) > 0 else None

    with t.managed():
        t.print("REPL demo -- a really dumb shell")
        t.print("Filenames in CWD are autocompleted.")
        while True:
            # render prompt
            t.style(NamedColor("yellow"))
            t.write("dumbsh> ")
            t.style_reset().flush()

            # read
            s = read_line(t, update, autocomplete)

            # eval
            if s == "help":
                # show available commands
                t.print("commands: help, ls, cd, pwd, clear, exit")