Skip to content

Commit

Permalink
Merge pull request #27 from gojuukaze/v2.1.4
Browse files Browse the repository at this point in the history
V2.1.4
  • Loading branch information
gojuukaze committed Dec 18, 2022
2 parents 5ced4b0 + ad6a898 commit 093b9bd
Show file tree
Hide file tree
Showing 22 changed files with 970 additions and 104 deletions.
44 changes: 44 additions & 0 deletions demo/scroll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import platform
from terminal_layout import *
from terminal_layout.extensions.scroll import *

s = '''python2 - print "Hello, World!"
python3 - print("Hello, World!")
c - printf("Hello, World!")
c++ - cout << "Hello, World!" << endl
java - System.out.println("Hello, World!")
JS - console.log('Hello, World!');
php - echo "Hello, World!"
c# - Console.WriteLine("Hello, World!")
shell - echo "Hello, World!"
go - fmt.Println("Hello, World!")
rust - println("Hello, World!")'''

title_style = {
'back': Back.magenta if platform.system() == 'Windows' else Back.ex_plum_2,
'width': Width.fill,
'gravity': Gravity.center

}
rows = [[],
[TextView('title', 'Hello, World!', **title_style)],
[TextView('', ' ', **title_style)],
]

scroll_style = {
'back': Back.blue if platform.system() == 'Windows' else Back.ex_sky_blue_2,
'fore': Fore.black
}
for i, ss in enumerate(s.split('\n')):
lan, code = ss.split(' - ')
rows.append([
TextView('', ' ' + str(i) + '.' + lan.title(), width=10, **scroll_style),
TextView('', '| ' + code, width=Width.fill, **scroll_style)
])

ctl = LayoutCtl.quick(TableLayout, rows)
ctl.set_buffer_size(200)
ctl.enable_debug(height=12)

scroll = Scroll(ctl, stop_key='q', loop=True, more=True, scroll_box_start=3)
scroll.scroll()
21 changes: 19 additions & 2 deletions docs/View.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ View

``View`` 的概念继承自安卓,一共有两种类型的View,分别是 ``Layout`` 与 ``Widget`` 。

此项目 ``Widget`` 只有 ``TextView`` ,目前支持的 ``Layout`` 有 ``TableLayout`` , ``TableRow``
此项目 ``Widget`` 只有 ``TextView`` ; ``Layout`` 有 ``TableLayout`` , ``TableRow``



Expand Down Expand Up @@ -53,6 +53,13 @@ View
向view中每个位置插入一个view

.. py:method:: remove()
从父view中移除自身

.. py:method:: remove_view_by_id(id)
删除view

Layout
-------
Expand All @@ -66,7 +73,7 @@ TableLayout

.. py:class:: TableLayout
.. py:method:: __init__(id, width=Width.fill, height=1, visibility=Visibility.visible)
.. py:method:: __init__(id, width=Width.fill, height=1, visibility=Visibility.visible, overflow_vertical=OverflowVertical.none)
init,**需要注意TableLayout的gravity总是为left**

Expand Down Expand Up @@ -120,6 +127,16 @@ TableRow
向view中添加多个view,只支持添加TextView

.. py:method:: is_show()
view是否显示出来。

在 v2.1.4 之后, 如果 terminal 高度时会隐藏不能显示的部分,此时可通过is_show判断view是否显示。

注意只有使用 ``scroll`` 或 ``overflow_vertical`` 为 ``hidden_top`` 、 ``hidden_btm`` 时这个函数返回值才是有意义的,
且只对 ``TableRow`` 有效,对于 ``TextView`` 这个返回值一样是无意义的。


TextView
----------

Expand Down
10 changes: 10 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
changelog
=============

2.1.4
---------
* ``TableLayout`` 添加 ``overflow_vertical`` 参数,用于terminal高度不够时隐藏row(默认不隐藏)
* 运行环境检测,非Terminal下抛出错误 ( `#25 <https://github.com/gojuukaze/terminal_layout/issues/25>`__ )
* 添加 `scroll <https://github.com/gojuukaze/terminal_layout/tree/master/terminal_layout/extensions/scroll>`__ 扩展,让 ``TableLayout`` 支持滚动 ( `#24 <https://github.com/gojuukaze/terminal_layout/issues/24>`__ )
* 添加 ``remove``, ``remove_view_by_id`` 函数
* choice扩展改用scroll实现滚动
* 添加 ``is_show`` 用于 使用 ``scroll`` 或 ``overflow_vertical`` 为 ``hidden_top`` 、 ``hidden_btm`` 时判断 ``TableRow`` 是否隐藏。( **只能判断TableRow** )
* 修改一些小bug

2.1.3
---------
* 解决 ``readkey()`` 函数在 Win PowerShell 下无法识别方向键 bug ( `#22 <https://github.com/gojuukaze/terminal_layout/issues/22>`__ )
Expand Down
9 changes: 9 additions & 0 deletions docs/extensions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ input

.. image:: ../_static/input.gif

scroll
-----------------

文档查看:https://github.com/gojuukaze/terminal_layout/tree/master/terminal_layout/extensions/scroll


.. image:: ../_static/input.gif


Binary file added pic/cal_scroll.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pic/scroll.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 34 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
[build-system]
requires = [
"setuptools>=42",
"wheel"
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "terminal_layout"
version = "2.1.4"
authors = [
{ name="gojuukaze", email="ikaze_email@163.com" },
]
build-backend = "setuptools.build_meta"
description = "The project help you to quickly build layouts in terminal (命令行ui布局工具)"
readme = "README.rst"
requires-python = ">=2.7"
classifiers = [
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
"Environment :: Console",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Development Status :: 5 - Production/Stable",
"Topic :: Terminals",
]

dependencies = [
"colorama==0.4.4",
'colored==1.3.93',
]

[project.urls]
"Homepage" = "https://github.com/gojuukaze/terminal_layout"
"Bug Tracker" = "https://github.com/gojuukaze/terminal_layout/issues"
"Documentation"="https://doc.ikaze.cn/terminal_layout"

[tool.setuptools.packages.find]
include = ["terminal_layout*"]
exclude = ["demo", "tests"]
2 changes: 1 addition & 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.3",
version="2.1.4",
description="The project help you to quickly build layouts in terminal (命令行ui布局工具)",
long_description=open("README.rst", encoding='utf-8').read(),

Expand Down
76 changes: 54 additions & 22 deletions terminal_layout/ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
import threading
import time

try:
# py3
from os import get_terminal_size
except:
# py2
from backports.shutil_get_terminal_size import get_terminal_size

from terminal_layout.helper.helper import get_terminal_size
from terminal_layout.ansi import term_init
from terminal_layout.view import *
from terminal_layout.view.base import View
Expand All @@ -24,7 +19,13 @@ class LayoutCtl(object):
_stop_flag = False
auto_re_draw = True

def __init__(self, layout=None):
def check(self):
if not sys.stdout.isatty():
raise RuntimeError('terminal_layout can only run on Terminal')

def __init__(self, layout=None,skip_check=False):
if not skip_check:
self.check()
self.layout = layout # type:View
self.refresh_lock = threading.Lock()
self.init_refresh_thread()
Expand All @@ -44,7 +45,8 @@ def set_buffer_size(self, size):
建议在draw之前调用
"""
self.buffering = size
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', self.buffering, encoding='utf-8')
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w',
self.buffering, encoding='utf-8')

def is_stop(self):
return self._stop_flag
Expand All @@ -54,18 +56,18 @@ def quick(cls, layout_class, data):
"""
:param layout_class:
:param data:
:param data: TableRow的data为 [textView, ...] ;
TableLayout的data为 [ [textView], ]
:return:
:rtype :LayoutCtl
"""
if layout_class is TableLayout:

table_layout = TableLayout('root', Width.fill)
i = 0
for row_data in data:
table_row = TableRow.quick_init('root_row_' + str(i), row_data, width=Width.fill)
for i, row_data in enumerate(data):
table_row = TableRow.quick_init(
'root_row_' + str(i), row_data, width=Width.fill)
table_layout.add_view(table_row)
i += 1
return cls(table_layout)
elif layout_class is TableRow:
row = TableRow.quick_init('root', data, width=Width.fill)
Expand All @@ -89,17 +91,23 @@ def get_layout(self):
"""
return LayoutProxy(self, self.layout)

def enable_debug(self, width=50, height=10):
self.debug = True
self.debug_width = width
self.debug_height = height

def get_terminal_size(self):
if self.debug:
self.height = 10
self.width = 50
self.height = self.debug_height
self.width = self.debug_width
else:
size = get_terminal_size()
self.height = size.lines
self.width = size.columns

size = get_terminal_size()
self.height = size.lines
self.width = size.columns
if platform.system() == 'Windows':
self.width -= 1

if platform.system() == 'Windows':
self.width -= 1
return self.width, self.height

def update_width(self):
Expand All @@ -111,6 +119,7 @@ def draw(self, auto_re_draw=True):
self.auto_re_draw = auto_re_draw
self.version += 1
self.update_width()
self.layout.set_terminal_size(*self.get_terminal_size())
self.layout.draw()

sys.stdout.write('\n')
Expand All @@ -126,6 +135,8 @@ def re_draw(self):
self.clear()

self.update_width()
self.layout.set_terminal_size(*self.get_terminal_size())

self.layout.draw()

sys.stdout.write('\n')
Expand Down Expand Up @@ -211,7 +222,7 @@ def delay_set_text(self, text, delay=0.3):

def get(self, k, default):
"""
When default == NULL , it is raised an error when the attribute doesn't exist
if default == NULL , it is raised an error when the attribute doesn't exist
如果default为NULL,当不存在变量时会抛错
"""
if default == NULL:
Expand Down Expand Up @@ -255,6 +266,15 @@ def get_parent(self, default=NULL):
"""
self.get('parent', default)

def remove(self):
"""
:rtype: bool
"""
return self.view.remove()

def is_show(self):
return self.view.is_show()


class TextViewProxy(BaseViewProxy):

Expand Down Expand Up @@ -316,4 +336,16 @@ def add_view_list(self, views):
self.view.add_view_list(views)

def insert_view(self, i, view):
self.view.insert(i, view)
return self.view.insert(i, view)

def remove_view_by_id(self, id):
"""
:rtype: bool
"""
return self.view.remove_view_by_id(id)

def set_overflow_vertical(self, overflow_vertical, raise_error=False):
self.set('overflow_vertical', overflow_vertical, raise_error)

def get_overflow_vertical(self, default=NULL):
return self.get('overflow_vertical', default)

0 comments on commit 093b9bd

Please sign in to comment.