# -*- coding: utf-8 -*-
"""messages.py
This module contains some windows for lfm use.
"""
import sys
import os.path
import curses
import curses.panel
import files
######################################################################
##### module variables
app = None
historic = []
HISTORIC_MAXLEN = 100
######################################################################
class CommonWindow:
"""A superclass for 'error' and 'win' windows"""
def __init__(self, title, text, br_att, br_bg, bd_att, bd_bg, waitkey = 1):
self.waitkey = waitkey
text = text.replace('\t', ' ' * 4)
lines = text.split('\n')
length = max(map(len, lines))
if self.waitkey:
w = min(max(max(length+6, 27+4), len(title)+6), app.maxw-2)
if length < 27 + 4 and len(title) <= length:
w -= 1
else:
w = min(max(length+6, len(title)+6), app.maxw-2)
if len(title) < length:
w -= 1
h = len(lines) + 4
for l in lines:
h += int((len(l)+1) / (app.maxw-2))
if h > app.maxh - 4:
h = app.maxh - 4
text = ''.join([l+'\n' for l in lines[-5:]])
try:
win = curses.newwin(h, w,
int((app.maxh-h)/2), int((app.maxw-w)/2))
self.pwin = curses.panel.new_panel(win)
self.pwin.top()
except curses.error:
print 'Can\'t create window'
sys.exit(-1)
win.bkgd(br_bg, br_att)
win.erase()
win.box(0, 0)
if len(title) > app.maxw-14:
title = title[:app.maxw-10] + '...' + '\''
win.addstr(0, int((w-len(title)-2)/2), ' %s ' % title, curses.A_BOLD)
if h == 5:
win.addstr(2, 2, text, bd_att)
else:
win.addstr(2, 1, text, bd_att)
if self.waitkey:
win.addstr(h-1, int((w-27)/2), ' Press any key to continue ', br_att)
win.refresh()
win.keypad(1)
def run(self):
if self.waitkey:
while not self.pwin.window().getch():
pass
self.pwin.hide()
class FixSizeCommonWindow:
"""A superclass for messages, with fixed size"""
def __init__(self, title, text, downtext,
br_att, br_bg, bd_att, bd_bg, waitkey = 1):
self.waitkey = waitkey
text = text.replace('\t', ' ' * 4)
w = app.maxw - 20
if len(title) > w - 4:
title = title[:w-4]
if len(text) > w - 4:
text = text[:w-5]
if len(downtext) > w - 4:
downtext = downtext[:w-4]
h = 5
try:
win = curses.newwin(h, w,
int((app.maxh-h)/2), int((app.maxw-w)/2))
self.pwin = curses.panel.new_panel(win)
self.pwin.top()
except curses.error:
print 'Can\'t create window'
sys.exit(-1)
win.bkgd(br_bg, br_att)
win.erase()
win.box(0, 0)
if len(title) > app.maxw - 14:
title = title[:app.maxw-10] + '...' + '\''
win.addstr(0, int((w-len(title)-2)/2), ' %s ' % title, curses.A_BOLD)
if h == 5:
win.addstr(2, 2, text, bd_att)
else:
win.addstr(2, 1, text, bd_att)
if self.waitkey:
win.addstr(h-1, int((w-27)/2),
' Press any key to continue ', br_att)
elif downtext:
win.addstr(h-1, int((w-len(downtext)-2)/2),
' %s ' % downtext, br_att)
win.refresh()
win.keypad(1)
def run(self):
if self.waitkey:
while not self.pwin.window().getch():
pass
self.pwin.hide()
class FixSizeProgressBarWindow:
"""Like FixSizeCommonWindow but with a ProgressBar"""
def __init__(self, title, text, downtext, percent,
bd_att, bd_bg, pb_att, pb_bg, waitkey = 1):
title = title[:app.maxw-14]
text = text[:app.maxw-14]
self.waitkey = waitkey
text = text.replace('\t', ' ' * 4)
w = app.maxw - 20
if len(title) > w - 4:
title = title[:w-4]
if len(text) > w - 4:
text = text[:w-5]
if len(downtext) > w - 4:
downtext = downtext[:w-4]
h = 7
self.h, self.w = h, w
self.bd_att, self.pb_att = bd_att, pb_att
try:
win = curses.newwin(h, w,
int((app.maxh-h)/2), int((app.maxw-w)/2))
self.progressbar = curses.newpad(1, w-11+1)
self.pwin = curses.panel.new_panel(win)
self.pwin.top()
except curses.error:
print 'Can\'t create window'
sys.exit(-1)
win.bkgd(bd_bg, bd_att)
self.progressbar.bkgd(curses.ACS_CKBOARD, pb_bg)
win.erase()
win.keypad(1)
def show(self, title, text, downtext, percent):
self.pwin.window().erase()
self.pwin.window().box(0, 0)
if len(title) > app.maxw - 14:
title = title[:app.maxw-10] + '...' + '\''
self.pwin.window().addstr(0, int((self.w-len(title)-2)/2),
' %s ' % title, curses.A_BOLD)
self.pwin.window().addstr(2, 2, text)
w1 = percent * (self.w-11) / 100
self.progressbar.erase()
self.progressbar.addstr(0, 0, ' ' * w1, self.pb_att | curses.A_BOLD)
self.pwin.window().addstr(self.h-3, self.w-8, '[%3d%%]' % percent)
if self.waitkey:
self.pwin.window().addstr(self.h-1, int((self.w-27)/2),
' Press any key to continue ')
elif downtext:
self.pwin.window().addstr(self.h-1, int((self.w-len(downtext)-2)/2),
' %s ' % downtext)
self.pwin.window().refresh()
y0 = int((app.maxh-self.h)/2) + 4
x0 = int((app.maxw-self.w)/2) + 2
self.progressbar.refresh(0, 0, y0, x0, y0+1, x0+self.w-12)
self.pwin.window().refresh()
######################################################################
def error(title, msg = '', file = ''):
"""show an error window"""
if file == '':
buf = msg
else:
buf = '%s: %s' % (file, msg)
CommonWindow(title, buf,
curses.color_pair(8),
curses.color_pair(8),
curses.color_pair(7) | curses.A_BOLD,
curses.color_pair(7)).run()
######################################################################
def win(title, text):
"""show a message window and wait for a key"""
CommonWindow(title, text,
curses.color_pair(1), curses.color_pair(1),
curses.color_pair(4), curses.color_pair(4)).run()
def win_nokey(title, text, downtext = ''):
"""show a message window, does not wait for a key"""
FixSizeCommonWindow(title, text, downtext,
curses.color_pair(1), curses.color_pair(1),
curses.color_pair(1), curses.color_pair(1),
waitkey = 0).run()
######################################################################
def notyet(title):
"""show a not-yet-implemented message"""
CommonWindow(title,
'Sorry, but this function\n is not implemented yet!',
curses.color_pair(1) | curses.A_BOLD, curses.color_pair(1),
curses.color_pair(4), curses.color_pair(4)).run()
######################################################################
def get_a_key(title, question):
"""show a window returning key pressed"""
question = question.replace('\t', ' ' * 4)
lines = question.split('\n')
length = max(map(len, lines))
h = min(len(lines)+4, app.maxh-2)
w = min(length+4, app.maxw-2)
try:
win = curses.newwin(h, w, int((app.maxh-h)/2), int((app.maxw-w)/2))
pwin = curses.panel.new_pane
Python库 | lfm-2.0.tar.gz
版权申诉
43 浏览量
2022-04-10
12:03:53
上传
评论
收藏 86KB GZ 举报
![avatar](https://profile-avatar.csdnimg.cn/277f6345dca0446498fbbc03843436aa_qq_38161040.jpg!1)
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
最新资源
- 305建筑结构水电欧式6套(14.5x20.2)\施工图1-11卫生间大样031026.dwg
- 305建筑结构水电欧式6套(14.5x20.2)\施工图1-09楼梯大样031026.dwg
- 基于PCL、C++编写的格网边缘点检测程序与代码
- 苹果cms源码 cms视频源码下载v10影视模板 苹果cms官方采集资源站.zip
- 305建筑结构水电欧式6套(14.5x20.2)\施工图1-1-04C型平面031026.dwg
- 【C语言课设】活动管理系统讲解(完整代码)
- 305建筑结构水电欧式6套(14.5x20.2)\施工图1-1-03总平面031026.dwg
- 探索Linux操作系统:开源世界的核心
- 305建筑结构水电欧式6套(14.5x20.2)\施工图1-1-02构造做法031026.dwg
- 基于Python实现循环神经网络(RNN)的影评情感分类
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)