Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Colorize TerminalCommandManager. #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions qdb/comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
from textwrap import dedent

from logbook import Logger
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import TerminalFormatter

from qdb.compat import range, PY3, items, Connection, gevent, input, print_
from qdb.errors import (
Expand Down Expand Up @@ -778,6 +781,8 @@ def __init__(self):
import readline
self.readline = readline
readline.parse_and_bind("tab: complete")
import sys
sys.stderr.write("\x1b[2J\x1b[H")

def pprint(self, msg):
pprint(msg)
Expand Down Expand Up @@ -882,13 +887,17 @@ def user_next_command(self, tracer):

command = getattr(self, 'do_' + cmd, None)
if command is None:
import sys
sys.stderr.write("\x1b[2J\x1b[H")
return self.do_print(' '.join(inp), tracer)
else:
try:
arg = inp[1]
except IndexError:
arg = None

import sys
sys.stderr.write("\x1b[2J\x1b[H")
return command(arg, tracer)

def do_print(self, arg, tracer):
Expand Down Expand Up @@ -1022,29 +1031,36 @@ def do_list(self, arg, tracer, recurse=True):

curline = tracer.curframe.f_lineno
if start is None and end is None and arg != ':':
start = curline - 5
start = curline - 10
if start < 0:
start = 0
end = curline + 5
end = curline + 10

def prepend(ix_l):
return (
'%s ' % ('-->' if ix_l[0] == curline else ' ')
) + ix_l[1]

self.writeln(
'\n'.join(
map(
prepend,
enumerate(
length = len(str(end))
if ix_l[0] == curline:
prefix = '-' * (length + 1) + '>'
else:
prefix = str(ix_l[0]).rjust(length) + ' '
return ' '.join([prefix, ix_l[1]])

lines = map(
prepend,
enumerate(
highlight(
'\n'.join(
tracer.get_file_lines(
tracer.curframe.f_code.co_filename,
)[start:end],
1 if start is None else start + 1,
)
)
)
),
PythonLexer(),
TerminalFormatter(),
).split('\n')[start:end],
1 if start is None else start + 1,
),
)
self.writeln('\n'.join(lines))

if recurse:
return self.next_command.tailcall(tracer)
do_l = do_list
Expand Down