From cac9754fcfeb5728354d260665c7f9205d61fa0c Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 31 Jan 2015 13:43:24 +0300 Subject: [PATCH] Add pdb-specific segments and make default theme use them --- docs/source/configuration/listers.rst | 6 ++ docs/source/configuration/segments/pdb.rst | 7 +++ docs/source/develop/segments.rst | 9 +++ .../colorschemes/pdb/__main__.json | 5 ++ .../colorschemes/pdb/default.json | 8 +++ .../config_files/themes/pdb/default.json | 28 +++++---- powerline/listers/pdb.py | 37 +++++++++++ powerline/renderers/pdb.py | 5 ++ powerline/segments/pdb.py | 61 +++++++++++++++++++ 9 files changed, 155 insertions(+), 11 deletions(-) create mode 100644 docs/source/configuration/segments/pdb.rst create mode 100644 powerline/config_files/colorschemes/pdb/__main__.json create mode 100644 powerline/config_files/colorschemes/pdb/default.json create mode 100644 powerline/listers/pdb.py create mode 100644 powerline/segments/pdb.py diff --git a/docs/source/configuration/listers.rst b/docs/source/configuration/listers.rst index dc544b91..04e5371e 100644 --- a/docs/source/configuration/listers.rst +++ b/docs/source/configuration/listers.rst @@ -23,3 +23,9 @@ Vim listers .. automodule:: powerline.listers.vim :members: + +Pdb listers +----------- + +.. automodule:: powerline.listers.pdb + :members: diff --git a/docs/source/configuration/segments/pdb.rst b/docs/source/configuration/segments/pdb.rst new file mode 100644 index 00000000..b9a104b2 --- /dev/null +++ b/docs/source/configuration/segments/pdb.rst @@ -0,0 +1,7 @@ +************ +PDB segments +************ + +.. automodule:: powerline.segments.pdb + :members: + diff --git a/docs/source/develop/segments.rst b/docs/source/develop/segments.rst index a16f067d..3fea00a2 100644 --- a/docs/source/develop/segments.rst +++ b/docs/source/develop/segments.rst @@ -475,6 +475,15 @@ Pdb ``pdb`` Currently active :py:class:`pdb.Pdb` instance. +``curframe`` + Frame which will be run next. Note: due to the existence of + :py:func:`powerline.listers.pdb.frame_lister` one must not use + ``segment_info['pdb'].curframe``. + +``initial_stack_length`` + Equal to the length of :py:attr:`pdb.Pdb.stack` at the first invocation of + the prompt decremented by one. + Segment class ============= diff --git a/powerline/config_files/colorschemes/pdb/__main__.json b/powerline/config_files/colorschemes/pdb/__main__.json new file mode 100644 index 00000000..b7ce28f0 --- /dev/null +++ b/powerline/config_files/colorschemes/pdb/__main__.json @@ -0,0 +1,5 @@ +{ + "groups": { + "current_context": "current_code_name" + } +} diff --git a/powerline/config_files/colorschemes/pdb/default.json b/powerline/config_files/colorschemes/pdb/default.json new file mode 100644 index 00000000..56f9020f --- /dev/null +++ b/powerline/config_files/colorschemes/pdb/default.json @@ -0,0 +1,8 @@ +{ + "groups": { + "current_line": { "fg": "gray10", "bg": "gray4", "attrs": ["bold"] }, + "current_file": { "fg": "gray10", "bg": "gray4", "attrs": ["bold"] }, + "current_code_name": { "fg": "gray9", "bg": "gray4", "attrs": ["bold"] }, + "stack_depth": { "fg": "gray1", "bg": "gray10", "attrs": ["bold"] } + } +} diff --git a/powerline/config_files/themes/pdb/default.json b/powerline/config_files/themes/pdb/default.json index 55505f67..dcae1080 100644 --- a/powerline/config_files/themes/pdb/default.json +++ b/powerline/config_files/themes/pdb/default.json @@ -1,20 +1,26 @@ { + "default_module": "powerline.segments.pdb", "segments": { "left": [ { - "function": "powerline.segments.common.env.virtualenv", - "priority": 10 + "function": "stack_depth" }, { - "type": "string", - "contents": "In [", - "draw_soft_divider": false, - "highlight_groups": ["virtualenv"] - }, - { - "type": "string", - "contents": "]", - "highlight_groups": ["virtualenv"] + "type": "segment_list", + "function": "powerline.listers.pdb.frame_lister", + "segments": [ + { + "function": "current_file", + "after": ":" + }, + { + "function": "current_line", + "after": " " + }, + { + "function": "current_code_name" + } + ] } ] } diff --git a/powerline/listers/pdb.py b/powerline/listers/pdb.py new file mode 100644 index 00000000..24e11eaf --- /dev/null +++ b/powerline/listers/pdb.py @@ -0,0 +1,37 @@ +# vim:fileencoding=utf-8:noet +from __future__ import (unicode_literals, division, absolute_import, print_function) + +from powerline.theme import requires_segment_info + + +@requires_segment_info +def frame_lister(pl, segment_info, full_stack=False, maxframes=3): + '''List all frames in segment_info format + + :param bool full_stack: + If true, then all frames in the stack are listed. Normally N first + frames are discarded where N is a number of frames present at the first + invocation of the prompt minus one. + :param int maxframes: + Maximum number of frames to display. + ''' + if full_stack: + initial_stack_length = 0 + frames = segment_info['pdb'].stack + else: + initial_stack_length = segment_info['initial_stack_length'] + frames = segment_info['pdb'].stack[initial_stack_length:] + + if len(frames) > maxframes: + frames = frames[-maxframes:] + + return ( + ( + { + 'curframe': frame[0], + 'initial_stack_length': initial_stack_length, + }, + {} + ) + for frame in frames + ) diff --git a/powerline/renderers/pdb.py b/powerline/renderers/pdb.py index d514e490..828223a5 100644 --- a/powerline/renderers/pdb.py +++ b/powerline/renderers/pdb.py @@ -9,10 +9,13 @@ class PDBRenderer(ReadlineRenderer): '''PDB-specific powerline renderer ''' pdb = None + initial_stack_length = None def get_segment_info(self, segment_info, mode): r = self.segment_info.copy() r['pdb'] = self.pdb + r['initial_stack_length'] = self.initial_stack_length + r['curframe'] = self.pdb.curframe return r def set_pdb(self, pdb): @@ -28,6 +31,8 @@ class PDBRenderer(ReadlineRenderer): self.pdb = pdb def render(self, **kwargs): + if self.initial_stack_length is None: + self.initial_stack_length = len(self.pdb.stack) - 1 return Renderer.render(self, **kwargs) diff --git a/powerline/segments/pdb.py b/powerline/segments/pdb.py new file mode 100644 index 00000000..bd6a38b7 --- /dev/null +++ b/powerline/segments/pdb.py @@ -0,0 +1,61 @@ +# vim:fileencoding=utf-8:noet +from __future__ import (unicode_literals, division, absolute_import, print_function) + +import os + +from powerline.theme import requires_segment_info + + +@requires_segment_info +def current_line(pl, segment_info): + '''Displays line number that is next to be run + ''' + return str(segment_info['curframe'].f_lineno) + + +@requires_segment_info +def current_file(pl, segment_info, basename=True): + '''Displays current file name + + :param bool basename: + If true only basename is displayed. + ''' + filename = segment_info['curframe'].f_code.co_filename + if basename: + filename = os.path.basename(filename) + return filename + + +@requires_segment_info +def current_code_name(pl, segment_info): + '''Displays name of the code object of the current frame + ''' + return segment_info['curframe'].f_code.co_name + + +@requires_segment_info +def current_context(pl, segment_info): + '''Displays currently executed context name + + This is similar to :py:func:`current_code_name`, but gives more details. + + Currently it only gives module file name if code_name happens to be + ````. + ''' + name = segment_info['curframe'].f_code.co_name + if name == '': + name = os.path.basename(segment_info['curframe'].f_code.co_filename) + return name + + +@requires_segment_info +def stack_depth(pl, segment_info, full_stack=False): + '''Displays current stack depth + + Result is relative to the stack depth at the time prompt was first run. + + :param bool full_stack: + If true then absolute depth is used. + ''' + return str(len(segment_info['pdb'].stack) - ( + 0 if full_stack else segment_info['initial_stack_length']))