Commit 87b8b017 authored by MinchinWeb's avatar MinchinWeb

Move `_N_lines` to be a private function of the `ListCommand` class

parent 9d2ae1c1
......@@ -340,7 +340,7 @@ class ListCommandTest(CommandTest):
self.assertEqual(self.output, "| 1| (C) 2015-11-05 Foo @Context2 Not@Context +Project1 Not+Project\n| 4| (C) Drink beer @ home\n| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2\n")
self.assertEqual(self.errors, "")
@mock.patch('topydo.lib.ListFormat.get_terminal_size')
@mock.patch('topydo.commands.ListCommand.get_terminal_size')
@mock.patch.dict(os.environ, {'PROMPT':'', 'PS1':''})
def test_list44(self, mock_terminal_size):
"""
......@@ -358,7 +358,7 @@ class ListCommandTest(CommandTest):
self.assertEqual(self.output, "| 1| (A) item 1\n| 27| (A) item 27\n| 2| (B) item 2\n| 28| (B) item 28\n| 3| (C) item 3\n| 29| (C) item 29\n| 4| (D) item 4\n| 30| (D) item 30\n| 5| (E) item 5\n| 31| (E) item 31\n| 6| (F) item 6\n| 32| (F) item 32\n| 7| (G) item 7\n| 33| (G) item 33\n| 8| (H) item 8\n| 34| (H) item 34\n| 9| (I) item 9\n| 35| (I) item 35\n| 10| (J) item 10\n| 36| (J) item 36\n| 11| (K) item 11\n")
self.assertEqual(self.errors, "")
@mock.patch('topydo.lib.ListFormat.get_terminal_size')
@mock.patch('topydo.commands.ListCommand.get_terminal_size')
@mock.patch.dict(os.environ, {'PROMPT':'', 'PS1':''})
def test_list45(self, mock_terminal_size):
"""Test basic 'N' parameter with nine line terminal."""
......@@ -376,7 +376,7 @@ class ListCommandTest(CommandTest):
self.assertEqual(self.output, "| 1| (A) item 1\n| 27| (A) item 27\n| 2| (B) item 2\n| 28| (B) item 28\n| 3| (C) item 3\n| 29| (C) item 29\n| 4| (D) item 4\n")
self.assertEqual(self.errors, "")
@mock.patch('topydo.lib.ListFormat.get_terminal_size')
@mock.patch('topydo.commands.ListCommand.get_terminal_size')
@mock.patch.dict(os.environ, {'PROMPT':'', 'PS1':''})
def test_list46(self, mock_terminal_size):
"""Test basic 'N' parameter with zero height terminal."""
......@@ -390,7 +390,7 @@ class ListCommandTest(CommandTest):
self.assertEqual(self.output, "| 1| (A) item 1\n")
self.assertEqual(self.errors, "")
@mock.patch('topydo.lib.ListFormat.get_terminal_size')
@mock.patch('topydo.commands.ListCommand.get_terminal_size')
@mock.patch.dict(os.environ, {'PROMPT':'$E[1;34m%username%@%computername%$E[0m$S$E[1;32m$P$E[0m$_$E[1;37m--$E[0m$S',
'PS1':'username@hostname\n--'})
def test_list47(self, mock_terminal_size):
......
......@@ -14,13 +14,17 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import sys
import os
from topydo.lib.Config import config
from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.Filter import InstanceFilter
from topydo.lib.ListFormat import _N_lines
from topydo.lib.PrettyPrinter import pretty_printer_factory
from topydo.lib.prettyprinters.Format import PrettyPrinterFormatFilter
from topydo.lib.TodoListBase import InvalidTodoException
from topydo.lib.Utils import get_terminal_size
class ListCommand(ExpressionCommand):
......@@ -73,7 +77,7 @@ class ListCommand(ExpressionCommand):
elif opt == '-N':
# 2 lines are assumed to be taken up by printing the next prompt
# display at least one item
self.limit = _N_lines()
self.limit = self._N_lines()
elif opt == '-n':
try:
self.limit = int(value)
......@@ -130,6 +134,36 @@ class ListCommand(ExpressionCommand):
self.out(self.printer.print_list(self._view().todos))
def _N_lines(self):
''' 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 = 1 # prompt is assumed to take up one line, even
# without any newlines in it
if "win32" in sys.platform:
lines_in_prompt += 1 # Windows will typically print a free line after
# the program output
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
def execute(self):
if not super().execute():
return False
......
......@@ -17,9 +17,7 @@
""" Utilities for formatting output with "list_format" option."""
import arrow
import os
import re
import sys
from topydo.lib.Config import config
from topydo.lib.Utils import get_terminal_size
......@@ -281,33 +279,3 @@ class ListFormatParser(object):
parsed_str = _right_align(parsed_str)
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 = 1 # prompt is assumed to take up one line, even
# without any newlines in it
if "win32" in sys.platform:
lines_in_prompt += 1 # Windows will typically print a free line after
# the program output
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