Commit 8f2db9c3 authored by MinchinWeb's avatar MinchinWeb

Provides a smart way to determine the number of lines to print with the '-N' parameter.

Looks at the prompt or PS1 environmental variables to determine the number of
newlines contained in them.

Fixes 102.
parent 4b8c8f09
...@@ -17,10 +17,10 @@ ...@@ -17,10 +17,10 @@
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib.ExpressionCommand import ExpressionCommand from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.Filter import InstanceFilter from topydo.lib.Filter import InstanceFilter
from topydo.lib.ListFormat import _N_lines
from topydo.lib.PrettyPrinter import pretty_printer_factory from topydo.lib.PrettyPrinter import pretty_printer_factory
from topydo.lib.prettyprinters.Format import PrettyPrinterFormatFilter from topydo.lib.prettyprinters.Format import PrettyPrinterFormatFilter
from topydo.lib.TodoListBase import InvalidTodoException from topydo.lib.TodoListBase import InvalidTodoException
from topydo.lib.Utils import get_terminal_size
class ListCommand(ExpressionCommand): class ListCommand(ExpressionCommand):
...@@ -73,7 +73,7 @@ class ListCommand(ExpressionCommand): ...@@ -73,7 +73,7 @@ class ListCommand(ExpressionCommand):
elif opt == '-N': elif opt == '-N':
# 2 lines are assumed to be taken up by printing the next prompt # 2 lines are assumed to be taken up by printing the next prompt
# display at least one item # display at least one item
self.limit = max(get_terminal_size().lines - 2, 1) self.limit = _N_lines()
elif opt == '-n': elif opt == '-n':
try: try:
self.limit = int(value) self.limit = int(value)
......
...@@ -17,7 +17,9 @@ ...@@ -17,7 +17,9 @@
""" Utilities for formatting output with "list_format" option.""" """ Utilities for formatting output with "list_format" option."""
import arrow import arrow
import os
import re import re
import sys
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib.Utils import get_terminal_size from topydo.lib.Utils import get_terminal_size
...@@ -279,3 +281,32 @@ class ListFormatParser(object): ...@@ -279,3 +281,32 @@ class ListFormatParser(object):
parsed_str = _right_align(parsed_str) parsed_str = _right_align(parsed_str)
return parsed_str.rstrip() return parsed_str.rstrip()
def _N_lines():
''' Determine how many lines to print, such that the number of items
displayed will fit on the terminal (i.e one 'screen-ful' of items)
This looks at the environmental prompt variable, and tries to determine
how many lines it takes up.
On Windows, it does this by looking for the '$_' sequence, which indicates
a new line, in the environmental variable PROMPT.
Otherwise, it looks for a newline ('\n') in the environmental variable
PS1.
'''
lines_in_prompt = 2 # prompt is assumed to take up one line, even
# without any newlines in it, plus there is a free
# line printed out after the program output
if "win32" in sys.platform:
a = re.findall('\$_', os.getenv('PROMPT', ''))
lines_in_prompt += len(a)
else:
a = re.findall('\\n', os.getenv('PS1', ''))
lines_in_prompt += len(a)
n_lines = get_terminal_size().lines - lines_in_prompt
# print a minimum of one item
n_lines = max(n_lines, 1)
return n_lines
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment