Skip to content

Commit

Permalink
Merge pull request #23 from gojuukaze/v2.1.3
Browse files Browse the repository at this point in the history
V2.1.3
  • Loading branch information
gojuukaze authored Oct 21, 2022
2 parents a7941a7 + f702f10 commit 420ed04
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
changelog
=============

2.1.3
---------
* 解决 ``readkey()`` 函数在 Win PowerShell 下无法识别方向键 bug ( `#22 <https://github.com/gojuukaze/terminal_layout/issues/22>`__ )
* choice扩展适配高度不够情况,当高度不够时隐藏部分选项 ( `#21 <https://github.com/gojuukaze/terminal_layout/issues/21>`__ )
* choice扩展支持设置stop_key,默认为 ``['q']``

2.1.2
---------
* 增加input扩展,可以获取文字输入了(不支持windows)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="terminal_layout",
version="2.1.2",
version="2.1.3",
description="The project help you to quickly build layouts in terminal (命令行ui布局工具)",
long_description=open("README.rst", encoding='utf-8').read(),

Expand Down Expand Up @@ -38,6 +38,7 @@
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Development Status :: 5 - Production/Stable',
'Topic :: Terminals',
],
Expand Down
21 changes: 11 additions & 10 deletions terminal_layout/extensions/choice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ if choice:

There are several parameter you can set:

| name | default | desc |
|-----------------|---------------------------------|-------------------|
| title | | title |
| choices | | a list of choices |
| icon | '> ' | delimiter list |
| icon\_style | StringStyle\(fore=Fore\.green\) | icon style |
| choices\_style | StringStyle\(\) | choices style |
| selected\_style | StringStyle\(\) | selected style |
| loop | True | loop |
| default_index | 0 | default icon index |
| name | default | desc |
|-----------------|---------------------------------|--------------------|
| title | | title |
| choices | | a list of choices |
| icon | '> ' | delimiter list |
| icon\_style | StringStyle\(fore=Fore\.green\) | icon style |
| choices\_style | StringStyle\(\) | choices style |
| selected\_style | StringStyle\(\) | selected style |
| loop | True | loop |
| default_index | 0 | default icon index |
| stop_key | ['q'] | stop key |
42 changes: 37 additions & 5 deletions terminal_layout/extensions/choice/choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,47 @@ def to_dict(self):
class Choice(object):
@instance_variables
def __init__(self, title, choices, icon='> ', icon_style=StringStyle(fore=Fore.green), choices_style=StringStyle(),
selected_style=StringStyle(), loop=True, default_index=0):
selected_style=StringStyle(), loop=True, default_index=0, stop_key=None):
self.current = self.get_default_index(default_index)
self.result = None
if not stop_key:
self.stop_key = ['q']

def get_default_index(self, default_index):
if default_index < 0 or default_index > len(self.choices):
return 0
return default_index

h_cache = None

def hidden_choices(self):
"""
适配高度,当高度不够时隐藏部分choices
"""
if self.h_cache is None:
_, self.h_cache = self.ctl.get_terminal_size()
# 最后一行要留着显示光标,不能输出因此-1
# title不能隐藏,因此再-1
self.h_cache -= 2

if self.h_cache >= len(self.choices) + 1:
return

# 优先显示current下面部分,还有剩余高度时从current上面补
show_start = self.current
show_end = len(self.choices)
if show_end - show_start > self.h_cache:
show_end = show_start + self.h_cache
left = self.h_cache - show_end + show_start
if left > 0:
show_start -= left
row_id_p = 'root_row_'
for i in range(len(self.choices)):
if i >= show_start and i < show_end:
self.ctl.find_view_by_id(row_id_p + str(i + 1)).set_visibility(Visibility.visible)
else:
self.ctl.find_view_by_id(row_id_p + str(i + 1)).set_visibility(Visibility.gone)

def get_choice(self):
views = [[TextView('', self.title)]]
for i, c in enumerate(self.choices):
Expand All @@ -38,15 +70,15 @@ def get_choice(self):
views[self.current + 1][0].visibility = Visibility.visible
views[self.current + 1][1] = TextView('value%d' % self.current, self.choices[self.current],
**self.selected_style.to_dict())
self.ctl = LayoutCtl.quick(TableLayout,
views)
self.ctl = LayoutCtl.quick(TableLayout, views)
self.hidden_choices()
self.ctl.draw(auto_re_draw=False)

kl = KeyListener()
kl.bind_key(Key.UP, Key.DOWN, self.change_current, decorator=False)
kl.bind_key(Key.ENTER, self.select, decorator=False)

kl.listen()
kl.listen(self.stop_key)
return self.result

def update_style(self, view, style):
Expand All @@ -70,7 +102,7 @@ def change_current(self, kl, event):
self.current = temp
self.ctl.find_view_by_id('icon%d' % self.current).set_visibility(Visibility.visible)
self.update_style(self.ctl.find_view_by_id('value%d' % self.current), self.selected_style)

self.hidden_choices()
self.ctl.re_draw()

def select(self, kl, event):
Expand Down
7 changes: 6 additions & 1 deletion terminal_layout/readkey/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
16384: Key.F6.code,
16640: Key.F7.code,
16896: Key.F8.code,

# cmd
18656: Key.UP.code,
20704: Key.DOWN.code,
19424: Key.LEFT.code,
19936: Key.RIGHT.code,
# powershell
18432: Key.UP.code,
20480: Key.DOWN.code,
19200: Key.LEFT.code,
19712: Key.RIGHT.code,
}


Expand Down

0 comments on commit 420ed04

Please sign in to comment.