Files
Labstand-2.0/OKNA96/apps/terminal.py
T

135 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from PIL import Image, ImageDraw
class TerminalApp:
kind = 'terminal'
title = 'Терминал'
icon = 'T'
cmd = 'cmd'
VISIBLE_ROWS = 5
ROW_H = 10
COLS = 20
def __init__(self):
self.lines = []
self.input = ""
self.scroll_offset = 0
def _total_rows(self):
return len(self.lines) + 1
def _max_offset(self):
return max(0, self._total_rows() - self.VISIBLE_ROWS)
def _clamp(self):
if self.scroll_offset < 0:
self.scroll_offset = 0
mx = self._max_offset()
if self.scroll_offset > mx:
self.scroll_offset = mx
def _scroll(self, delta):
self.scroll_offset += delta
self._clamp()
def render(self, fonts, bg, now):
img = Image.new('1', (128, 64), 0)
d = ImageDraw.Draw(img)
font = fonts['term']
total = self._total_rows()
self._clamp()
off = self.scroll_offset
mx = self._max_offset()
if total <= self.VISIBLE_ROWS:
start = 0
else:
start = mx - off
prompt_idx = len(self.lines)
for i in range(self.VISIBLE_ROWS):
row = start + i
y = i * self.ROW_H
if row < len(self.lines):
d.text((2, y), self.lines[row][:self.COLS], font=font, fill=1)
elif row == prompt_idx:
full = "> " + self.input
shown = full if len(full) <= self.COLS else ">" + self.input[-(self.COLS - 1):]
d.text((2, y), shown, font=font, fill=1)
cx = min(122, 2 + int(d.textlength(shown, font=font)) + 1)
d.rectangle((cx, y + 8, cx + 3, y + 9), fill=1)
if off < mx:
d.polygon([(124, 3), (127, 3), (125, 0)], fill=1)
if off > 0:
d.polygon([(124, 47), (127, 47), (125, 49)], fill=1)
return img
def handle_key(self, code, mods, system):
if code == 103: self._scroll(1); return True
if code == 108: self._scroll(-1); return True
if code == 104: self._scroll(self.VISIBLE_ROWS); return True
if code == 109: self._scroll(-self.VISIBLE_ROWS); return True
if code in (28, 96):
self._execute(system)
self.scroll_offset = 0
return True
if code == 14:
self.input = self.input[:-1]
self.scroll_offset = 0
return True
ch = system._char_for(code, mods.get('shift', False))
if ch and len(ch) == 1:
self.input = (self.input + ch)[-64:]
self.scroll_offset = 0
return True
return False
def _execute(self, system):
cmd = self.input.strip()
self.input = ""
if not cmd:
return
self.lines.append("> " + cmd)
self.lines = self.lines[-50:]
if cmd == 'killall':
self.lines.append("Завершение...")
system.trigger_bsod()
return
if cmd == 'killallnet':
self.lines.append("Аварийное завершение...")
system.trigger_bsod()
system.killall_network()
return
if cmd == 'help':
self.lines.extend(["help справка", "time время", "clear очистить",
"exit выход", "killall BSOD", "killallnet BSOD всех",
"about о системе"])
return
elif cmd == 'time':
self.lines.append(system._now().strftime("%H:%M:%S"))
return
elif cmd == 'clear':
self.lines = []
self.scroll_offset = 0
return
elif cmd == 'exit':
system._close_app('terminal')
return
from OKNA96.apps import find_by_cmd
kind = find_by_cmd(cmd)
if kind:
system._open_app(kind)
else:
self.lines.append(f"Не найдена команда ({cmd})")