Code Monkey home page Code Monkey logo

terminal_layout's Introduction

terminal_layout

The project help you to quickly build layouts in terminal
(这个一个命令行ui布局工具)

demo_v2_1.gif

demo.gif


Some extensions base on terminal_layout

progress.gif

choice.gif


** video demo **

asciicast

link

install

pip install terminal-layout

Dependencies

  • Python 3.5+ (maybe 3.4)
  • Linux, OS X, and Windows systems.

Python Support

Python terminal_layout
2.7 2.1.x
3.5+ 3.x

Usage

  • easy demo:
import time
from terminal_layout import *

ctl = LayoutCtl.quick(TableLayout,
                      # table id: root
                      [
                          [TextView('t1', 'Hello World!', width=Width.fill, back=Back.blue)],  # <- row id: root_row_0,
                          [TextView('t2', '', fore=Fore.magenta)],  # <- row id: root_row_1,
                      ],
                      )

# or layout=ctl.get_layout()
layout = ctl.find_view_by_id('root')
layout.set_width(20)

# default: auto_re_draw=True
ctl.draw()

# call delay_set_text() must be set auto_re_draw=True,
# otherwise you must start a thread to call re_draw() by yourself
ctl.find_view_by_id('t2').delay_set_text('你好,世界!', delay=0.2)

time.sleep(0.5)
row3 = TableRow.quick_init('', [TextView('t3', 'こんにちは、世界!')])
layout.add_view(row3)

# If you call draw() with auto_re_draw=True, you must stop()
ctl.stop()

  • disable auto_re_draw
import time
from terminal_layout import *

ctl = LayoutCtl.quick(TableLayout,
                      # table id: root
                      [
                          [TextView('t1', 'Hello World!', width=Width.fill, back=Back.blue)],  # <- row id: root_row_1,
                          [TextView('t2', '', fore=Fore.magenta)],  # <- row id: root_row_2,
                      ],
                      )


layout = ctl.find_view_by_id('root')
layout.set_width(20)

ctl.draw(auto_re_draw=False)

ctl.find_view_by_id('t2').set_text('你好,世界!')
ctl.re_draw()

time.sleep(0.5)
row3 = TableRow.quick_init('', [TextView('t3', 'こんにちは、世界!')])
layout.add_view(row3)
ctl.re_draw()

# don't need call stop()
# ctl.stop()
  • use python2 unicode
# -*- coding: utf-8 -*-
from terminal_layout import *
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

ctl = LayoutCtl.quick(TableLayout,
                      [
                          [TextView('', u'中文,你好', back=Back.cyan, width=Width.wrap)],
                          [TextView('', u'中文,你好', back=Back.cyan, width=6)],
                          [TextView('', u'日本語,こんにちは', back=Back.cyan, width=Width.wrap)],
                      ]

                      )

ctl.draw()

Properties

  • fore & back
TextView('','fore',fore=Fore.red)
TextView('','back',back=Back.red)

  • style
TextView('','style',style=Style.dim)

  • width
TextView('','width',width=10)

  • weight
TextView('','weight',weight=1)

  • gravity
TextView('','gravity',gravity=Gravity.left)

  • visibility
TextView('','',visibility=Visibility.visible)

  • ex_style (not support windows)
TextView('','ex_style',style=Style.ex_blink)

  • ex_fore & ex_back (not support windows)
TextView('','ex_fore',fore=Fore.ex_red_1)
TextView('','ex_back',back=Back.ex_red_1)

LICENSE

GPLv3

Thanks

  • colorama : Simple cross-platform colored terminal text in Python
  • colored : Very simple Python library for color and formatting in terminal

捐赠 / Sponsor

开源不易,如果你觉得对你有帮助,求打赏个一块两块的

terminal_layout's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

terminal_layout's Issues

在某些Linux设备上,无法显示中文

同样的代码,我在一个Ubuntu Desktop上面运行的时候,中文显示是正常的,但是放到某arm的Linux系统上的时候,中文报错
下图是正常显示的样子:
Snipaste_2020-10-20_21-58-09

下图是报错:
Snipaste_2020-10-20_21-58-22

关于刷新出现的问题

from terminal_layout import *
from time import sleep
from threading import Thread

msg_layout = TableLayout("msg_Table", width=Width.wrap)
for i in range(6):
    msg_layout.add_view(TextView("msg_text_%i" % i, " ",
                                 fore=Fore.magenta, style=Style.dim))
msg_ctl = LayoutCtl(msg_layout)
msg_ctl.draw(auto_re_draw=False)
print_msg = [None] * 6


def Input():
    print("\n\n")
    while True:
        print_msg.append(input("输入"))
        del print_msg[0]
        msg_ctl.re_draw()


input_thread = Thread(target=Input)
input_thread.start()
while True:
    for i in range(len(print_msg)):
        msg_ctl.find_view_by_id("msg_text_%i" % i).set_text(print_msg[i])
    sleep(2)
msg_ctl.stop()

话说类似这种程序,会出现行数偏移,原来的信息还在,等神奇的现象,所以现在有没有程序可以刷新掉指定的第几行到第几行?或者刷新时不清除input?类似这种的可以做到吗?

TextView 设置 Overflow.hidden_left 报错

from terminal_layout import *

ctl = LayoutCtl.quick(TableLayout,
                      [[TextView('', '123456789', back=Back.blue, width=7, overflow=Overflow.hidden_left)]]
                      )

ctl.draw(auto_re_draw=False)

choice 中导入后会自动产生 termina_layout.log 日志,实际上没有用到日志

from terminal_layout import *
from terminal_layout.helper.class_helper import instance_variables
# 日志没有用到,但导入模块时会自动创建日志文件到当前目录,若无日志,希望去除日志文件
from terminal_layout.logger import logger


class StringStyle(object):

    def __init__(self, fore=None, back=None, style=None):
        self.fore = fore or ''
        self.back = back or ''
        self.style = style or ''

    def to_dict(self):
        return {
            'fore': self.fore,
            'back': self.back,
            'style': self.style
        }

为什么无法使用set_text?

刚刚该在找自动刷新解释器等程序的时候找到了这个很棒的工具
但是我尝试更改,我已经输出东西的时候"AttributeError: 'TextViewProxy' object has no attribute 'setText'"
TxetView 没有 setText属性?但是作者你的范例也是用TxetView做的所以说why?
但是我还要再补充一个很神奇的一点,我运行你的代码的时候是没有问题的
所以说必须要:LayoutCtl.quick(TableLayout,...)?

能添加自定义样式吗?

比如说

╔════════╗
║> Css   ║
║  Bash  ║
║  C++   ║
║  Python║
║  HTML  ║
╚════════╝

get_terminal_size 函数报错

    不按Stackoverflow增加VM选项时,提示这个:

`Traceback (most recent call last):
File "C:\Users\HideUserName\PycharmProjects\Select_Project_To_Build\Select_Project_To_Build.py", line 22, in
phase_choice = Phase.get_choice()

File "D:\Python38\lib\site-packages\terminal_layout\extensions\choice\choice.py", line 74, in get_choice
self.hidden_choices()

File "D:\Python38\lib\site-packages\terminal_layout\extensions\choice\choice.py", line 41, in hidden_choices
_, self.h_cache = self.ctl.get_terminal_size()

File "D:\Python38\lib\site-packages\terminal_layout\ctl.py", line 97, in get_terminal_size
size = get_terminal_size()

OSError: [WinError 6] 句柄无效。`

Originally posted by @Femtometer in #22 (comment)


Python版本3.8.13,Pycharm社区版:2022.2.3。
只在Pycharm报这个异常,try正常走的py3分支。

句柄无效

Traceback (most recent call last):
File "F:/PythonCode/Other/sanic-cli/sanic-cli/file_creater.py", line 20, in
ctl.draw()
File "E:\software\anaconda\lib\site-packages\terminal_layout\ctl.py", line 109, in draw
self.update_width()
File "E:\software\anaconda\lib\site-packages\terminal_layout\ctl.py", line 103, in update_width
self.get_terminal_size()
File "E:\software\anaconda\lib\site-packages\terminal_layout\ctl.py", line 94, in get_terminal_size
size = get_terminal_size()
OSError: [WinError 6] 句柄无效。
大佬, 这个问题应该怎么破呢?

一屏显示不够或一行折行的情况

1、当小窗口使用时, list 比较多时显示不全

from terminal_layout.extensions.choice import *
from terminal_layout import *

c = Choice('Which is the Best Programming Language? (press <esc> to exit) ',
           ['Python', 'C/C++', 'Java', 'PHP', 'Go', 'JS', 'XXX', 'YYY', 'ZZZ',
            'AAAA', 'BBBB', 'CCC', 'DDDD', 'EEEE', 'FFFF', 'GGG', '...'],
           icon_style=StringStyle(fore=Fore.blue),
           selected_style=StringStyle(fore=Fore.blue))

choice = c.get_choice()
if choice:
    index, value = choice
    print(value, 'is the Best Programming Language')

image

2、当一行显示宽带大于窗口时,每一 行折行显示时,也有问题,多次输出

install error

  long_description=open("README.rst").read(),
 UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 104: illegal multibyte sequence
 ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.