예제 #1
0
def header():
    s = "PrimCom {v}".format(v=__version__)
    size = len(s)
    horizontal = '+' + '-' * (size + 2) + '+'
    col = cfg.colors[cfg.g.BACKGROUND]["header"]
    print(bold(horizontal, col))
    print(bold('| ' + s + ' |', col))
    print(bold(horizontal, col))
예제 #2
0
def cat(fname, o, search_term=""):
    """
    Show file content on stdout.

    If pygmentize is available, show a syntax-highlighted output.
    Otherwise fall back to a normal "cat".
    """
    print(bold("-" * 78))
    doc = o["doc"]
    if doc:
        print(bold(doc))
        print(bold("-" * 78))
    if fs.which("pygmentize"):
        print_pcat(fname, search_term)
    else:
        with open(fname) as f:
            for line in f:
                if search_term in line:
                    print(line, end='')
    print()
예제 #3
0
def cat(fname, o):
    """
    Show file content on stdout.

    If pygmentize is available, show a syntax-highlighted output.
    Otherwise fall back to a normal "cat".
    """
    fname = "data/" + fname
    #
    print(bold("-" * 78))
    doc = o["doc"]
    if doc:
        print(bold(doc))
        print(bold("-" * 78))
    if fs.which("pygmentize"):
        os.system(pcat.format(cfg.colors[cfg.g.BACKGROUND]["pygmentize_style"], fname))
    else:
        with open(fname) as f:
            for line in f:
                print(line, end=' ')
    print()
    #
    process_extras(fname, o)
예제 #4
0
def cat(fname, o, search_term=""):
    """
    Show file content on stdout.

    If pygmentize is available, show a syntax-highlighted output.
    Otherwise fall back to a normal "cat".
    """
    p = Path(fname)
    ext = p.suffix

    print(bold("-" * 78))
    doc = o["doc"]
    if doc:
        print(bold(doc))
        print(bold("-" * 78))
    if fs.which("pygmentize") and ext != ".txt":    # .txt files were colored incorrectly, so let's switch coloring off for .txt files
        print_pcat(fname, search_term)
    else:
        with open(fname) as f:
            for line in f:
                if search_term in line:
                    print(line, end='')
    print()
예제 #5
0
def print_pcat(fname, search_term=""):
    cmd = pcat.format(cfg.colors[cfg.g.BACKGROUND]["pygmentize_style"], fname)
    #
    if cfg.SHOW_LINE_NUMBERS:
        lines = fs.file_len(fname)
        lines_len = len(str(lines))
        width = lines_len + 1
        #
        out = process.get_exitcode_stdout_stderr(cmd)[1]
        out = out.rstrip("\n")
        for i, line in enumerate(out.split("\n"), start=1):
            if search_term in line:
                num_str = bold(str(i), color=cfg.colors[cfg.g.BACKGROUND]["line_numbers"])
                num_str = pad(num_str, width, i)
                print(num_str, line)
    else:
        os.system('{0} | grep -i "{1}"'.format(cmd, search_term))
예제 #6
0
def header():
    """
    Print a fancy header. The rainbow colored logo only looks good
    with dark background. If the background is light, skip it.
    """
    rainbow_index = len(logos) - 1
    if cfg.g.BACKGROUND == cfg.DARK:
        # include the rainbow
        indexes = range(len(logos))
    else:
        # exclude the rainbow
        indexes = range(len(logos)-1)

    pos = choice(my_shuffle(indexes))
    if pos == rainbow_index:
        print(logos[pos])
    else:
        col = cfg.colors[cfg.g.BACKGROUND]["header"]
        print(bold(logos[pos], col))
예제 #7
0
def menu():
    Thread(target=cache_pygmentize).start()
    #
    while True:
        try:
            inp = raw_input(bold('pc> ')).strip()
        except (KeyboardInterrupt, EOFError):
            print()
            my_exit(0)
        if len(inp) == 0:
            continue
        if inp in ('h', 'help()'):
            info()
        elif inp in ('q', 'qq', ':q', ':x', 'quit()', 'exit()'):
            my_exit(0)
        elif inp in ('c', 'clear()'):
            os.system('clear')
            header()
        elif inp in ('light()', 'dark()'):
            if inp == 'light()':
                cfg.g.BACKGROUND = cfg.LIGHT
            else:
                cfg.g.BACKGROUND = cfg.DARK
        elif inp in ('t', 'tags()', 'all()', 'd'):
            SearchHits.show_tag_list(tag2keys.keys())
        elif inp == 'p':
            os.system("python")
        elif inp == 'p3':
            os.system("python3")
        elif inp == 'bpy':
            os.system("bpython")
        elif inp == 'last()':
            print(last_key)
        elif inp == '!!':
            if last_key:
                perform_action(last_key)
        elif inp.startswith('!'):
            cmd = inp[1:]
            os.system(cmd)
        elif inp == 'edit()':
            if last_key:
                edit(last_key)
        elif inp == 'gedit()':
            if last_key:
                gedit(last_key)
        elif inp == 'less()':
            if last_key:
                less(last_key)
        elif inp in ('urls()', 'links()'):
            if last_key:
                show_urls(last_key)
        elif inp in ('cb()', 'tocb()'):
            if last_key:
                to_clipboards(last_key)
        elif inp == 'path()':
            if last_key:
                path_to_clipboards(last_key)
        elif inp == "doc()":
            if last_key:
                show_doc(last_key)
        elif inp == 'json.reload()':
            read_json()
        elif inp in ('json.view()', 'json.edit()'):
            if last_key:
                view_edit_json(last_key)
                read_json()
        elif inp in ("json.edit(this)", "jet()"):
            if last_key:
                edit_entry(last_key)
        elif inp == 'reddit()':
            reddit.reddit()
        elif inp == 'radio()':
            radio.radio_player()
        elif inp == 'conferences()':
            conferences.conferences()
        elif inp == 'mute()':
            radio.radio(None, stop=True)
        elif inp == 'myip()':
            my_ip.show_my_ip()
        elif inp in ('v', 'version()'):
            version()
        elif inp == 'commands()':
            show_commands()
        elif inp == 'add()':
            add_item()
            read_json()
        elif inp == 'hits()':
            SearchHits.show_tag_list()
        elif inp.startswith("pymotw:"):
            site = "pymotw.com"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("go:"):
            cmd_google(inp[inp.find(':')+1:])
        elif inp.startswith("go1:"):
            cmd_go1(inp[inp.find(':')+1:])
        elif inp.startswith("imdb:"):
            site = "imdb.com"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("amazon:"):
            site = "amazon.com"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("youtube:"):
            cmd_youtube(inp[inp.find(':')+1:])
        elif inp.startswith("wp:"):
            site = "wikipedia.org"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("lib:"):
            site = "docs.python.org/2/library/"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("lib3:"):
            site = "docs.python.org/3/library/"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("shorten:"):
            urlshortener.shorten_url(inp[inp.find(':')+1:])
        elif inp.startswith("def:"):
            cmd_def(inp[inp.find(':')+1:])
        elif inp.startswith("pep:"):
            open_pep(inp[inp.find(':')+1:])
        elif inp == 'pep()':
            open_pep(None)
        # disabled, always show the search hits
        #elif inp in tag2keys:
        #    tag = inp
        #    command(tag)
        elif re.search(r'^\d+$', inp):
            try:
                index = int(inp) - 1
                if index < 0:
                    raise IndexError
                tag = SearchHits.hits[index].tag
                command(tag)
            except IndexError:
                print("out of range...")
        elif re.search(r'^\d+\.(doc|action|tags|json|url|link|key|jet|edit)(\(\))?$', inp):
            try:
                pos = inp.find('.')
                index = int(inp[:pos]) - 1
                what = inp[pos+1:].rstrip("()")
                if index < 0:
                    raise IndexError
                hit = SearchHits.hits[index]
                hit.inspect(what)
            except IndexError:
                print("out of range...")
        elif re.search(r'^this.(doc|action|tags|json|url|link|key|jet|edit)(\(\))?$', inp):
            try:
                if not last_key:
                    raise NoLastKeyError
                pos = inp.find('.')
                what = inp[pos+1:].rstrip("()")
                hit = Hit(tag=None, key=last_key)
                hit.inspect(what)
            except NoLastKeyError:
                pass
        elif inp == 'pid()':
            pidcheck.pid_alert()
        elif inp == 'debug()':
            debug()
        elif inp in ('slay()', 'song()'):
            print("Playing:", radio.get_song()['current'])
        else:
            if len(inp) == 1:
                print("too short...")
            else:
                inp = inp.lower()
                SearchHits.show_hint(inp)
예제 #8
0
파일: h.py 프로젝트: acutesoftware/PrimCom
def menu():
    print("[{0:.3f}s]".format(time.time() - start_time), end='\n')
    #
    while True:
        try:
            #inp = raw_input(bold('pc> ')).strip()
            inp = raw_input(bold('{prompt}> '.format(prompt=os.getcwd()))).strip()
        except (KeyboardInterrupt, EOFError):
            print()
            my_exit(0)
        if len(inp) == 0:
            continue
        if inp in ('h', 'help()'):
            info()
        elif inp in ('q', 'qq', ':q', ':x', 'quit()', 'exit()'):
            my_exit(0)
        elif inp in ('c', 'clear()'):
            os.system('clear')
            print_header()
        elif inp in ('light()', 'dark()'):
            if inp == 'light()':
                cfg.g.BACKGROUND = cfg.LIGHT
            else:
                cfg.g.BACKGROUND = cfg.DARK
        elif inp in ('t', 'tags()', 'all()', 'd'):
            SearchHits.show_tag_list(tag2keys.keys())
        elif inp == 'p':
            os.system("python")
        elif inp == 'p3':
            os.system("python3")
        elif inp == 'bpy':
            os.system("bpython")
        elif inp == 'last()':
            print(last_key)
        elif inp == '!!':
            if last_key:
                perform_action(last_key)
        elif inp.startswith('!'):
            cmd = inp[1:]
            os.system(cmd)
        elif inp == 'edit()':
            if last_key:
                edit(last_key)
        elif inp == 'gedit()':
            if last_key:
                gedit(last_key)
        elif inp == 'less()':
            if last_key:
                less(last_key)
        elif inp in ('urls()', 'links()'):
            if last_key:
                show_urls(last_key)
        elif inp in ('cb()', 'tocb()'):
            if last_key:
                to_clipboards(last_key)
        elif inp == 'path()':
            if last_key:
                path_to_clipboards(last_key)
        elif inp == "doc()":
            if last_key:
                show_doc(last_key)
        elif inp == 'json.reload()':
            read_json()
        elif inp in ('json.view()', 'json.edit()'):
            if last_key:
                view_edit_json(last_key)
                read_json()
        elif inp in ("json.edit(this)", "jet()"):
            if last_key:
                edit_entry(last_key)
        elif inp == 'reddit()':
            reddit.reddit()
        elif inp == 'conferences()':
            conferences.conferences()
        elif inp == 'mute()':
            apps.radio.radio(None, stop=True)
        elif inp == 'myip()':
            my_ip.show_my_ip()
        elif inp in ('v', 'version()'):
            version()
        elif inp == 'commands()':
            show_commands()
        elif inp == 'add()':
            add_item()
            read_json()
        elif inp == 'hits()':
            SearchHits.show_tag_list()
        elif inp.startswith("pymotw:"):
            site = "pymotw.com"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("go:"):
            cmd_google(inp[inp.find(':')+1:])
        elif inp.startswith("go1:"):
            cmd_go1(inp[inp.find(':')+1:])
        elif inp.startswith("imdb:"):
            site = "imdb.com"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("amazon:"):
            site = "amazon.com"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("youtube:"):
            cmd_youtube(inp[inp.find(':')+1:])
        elif inp.startswith("wp:"):
            site = "wikipedia.org"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("lib:") or inp.startswith("lib2:"):
            site = "docs.python.org/2/library/"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("lib3:"):
            site = "docs.python.org/3/library/"
            cmd_go1(inp[inp.find(':')+1:], site=site)
        elif inp.startswith("golib:"):
            site = "http://golang.org/pkg/"
            lib = inp[inp.find(':')+1:]
            open_url(urljoin(site, lib))
        elif inp.startswith("shorten:"):
            urlshortener.shorten_url(inp[inp.find(':')+1:])
        elif inp.startswith("def:"):
            cmd_def(inp[inp.find(':')+1:])
        elif inp.startswith("pep:"):
            open_pep(inp[inp.find(':')+1:])
        elif inp == 'pep()':
            open_pep(None)
        elif inp == 'show()':
            show.show()
        elif inp == 'numbers()':
            toggle_line_numbers()
        elif re.search(r"^l([\d,-]+)\.(sh|py|py2|py3|cb|cb\(>\))$", inp):
            fname = key_to_file(last_key)
            selected_lines.process_selected_lines(inp, fname)
        elif inp == 'cd' or inp.startswith('cd '):
            change_dir(inp)
        elif inp == 'pwd()':
            print(os.getcwd())
        elif inp == 'userpass()':
            username_password()
        elif inp == 'apps()':
            apps.menu.main()
        elif inp == 'k':
            os.system("konsole 2>/dev/null &")
        elif inp.startswith("filter:"):
            term = inp[inp.find(':')+1:]
            if last_key:
                perform_action(last_key, term)
        elif inp.startswith("app:"):
            val = inp[inp.find(':')+1:]
            if not val:
                apps.menu.main()
            else:
                apps.menu.start_app(val)
        # shortcuts
        elif inp == 'radio()':
            apps.menu.start_app_by_shortcut('radio')
        # disabled, always show the search hits
        #elif inp in tag2keys:
        #    tag = inp
        #    command(tag)
        elif re.search(r'^\d+$', inp):
            try:
                index = int(inp) - 1
                if index < 0:
                    raise IndexError
                tag = SearchHits.hits[index].tag
                command(tag)
            except IndexError:
                print("out of range...")
        elif re.search(r'^\d+\.(doc|action|tags|json|url|link|key|jet|edit)(\(\))?$', inp):
            try:
                pos = inp.find('.')
                index = int(inp[:pos]) - 1
                what = inp[pos+1:].rstrip("()")
                if index < 0:
                    raise IndexError
                hit = SearchHits.hits[index]
                hit.inspect(what)
            except IndexError:
                print("out of range...")
        elif re.search(r'^this.(doc|action|tags|json|url|link|key|jet|edit)(\(\))?$', inp):
            try:
                if not last_key:
                    raise NoLastKeyError
                pos = inp.find('.')
                what = inp[pos+1:].rstrip("()")
                hit = Hit(tag=None, key=last_key)
                hit.inspect(what)
            except NoLastKeyError:
                pass
        elif inp == 'pid()':
            pidcheck.pid_alert()
        elif inp == 'debug()':
            debug(None)
        elif inp == 'song()':
            print("Playing:", apps.radio.get_song())
        else:
            if len(inp) == 1:
                print("too short...")
            else:
                inp = inp.lower()
                SearchHits.show_hint(inp)