Skip to content

Commit 8ecd6ea

Browse files
committed
Add terminal by @Annihilator708 into nx.utils
1 parent faec46a commit 8ecd6ea

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed

nx/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .cached_properties import (cached_property, cached_property_with_ttl,
66
cached_property_ttl, timed_cached_property)
77

8+
from .terminal import Terminal
89

910
class Singleton(type):
1011
_instances = {}

nx/utils/terminal.py

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
# MAIN REPO https://github.com/Annihilator708/PyNX_Terminal
2+
3+
import imgui
4+
import imguihelper
5+
import os
6+
import _nx
7+
import sys
8+
from imgui.integrations.nx import NXRenderer
9+
from io import StringIO
10+
import contextlib
11+
import logging
12+
13+
14+
@contextlib.contextmanager
15+
def stdoutIO(stdout=None):
16+
old = sys.stdout
17+
if stdout is None:
18+
stdout = StringIO()
19+
sys.stdout = stdout
20+
yield stdout
21+
sys.stdout = old
22+
23+
@contextlib.contextmanager
24+
def stderrIO(stderr=None):
25+
old = sys.stdout
26+
if stderr is None:
27+
stderr = StringIO()
28+
sys.stderr = stderr
29+
yield stderr
30+
sys.stderr = old
31+
32+
class Terminal():
33+
def colorToFloat(self, t):
34+
nt = ()
35+
for v in t:
36+
nt += ((1 / 255) * v,)
37+
return nt
38+
39+
def __str__(self):
40+
return "Terminal for the switch, made by PuffDip"
41+
42+
def __init__(self):
43+
logging.basicConfig(filename='terminal.log', format='%(levelname)s:%(message)s', level=logging.ERROR)
44+
# (r, g, b)
45+
self.KEY_COLOR = self.colorToFloat((230, 126, 34))
46+
self.KEY_FUNC_COLOR = self.colorToFloat((196, 107, 29))
47+
48+
self.TILED_DOUBLE = 1
49+
50+
self.renderer = NXRenderer()
51+
self.currentDir = os.getcwd()
52+
53+
self.CONSOLE_TEXT = "Python {} on Nintendo Switch".format(sys.version)
54+
self.version_number = '0.1'
55+
self.keyboard_toggled = False
56+
self.setting_toggle = False
57+
self.user_input = [self.CONSOLE_TEXT]
58+
self.CAPS = False
59+
self.SYS = False
60+
self.TAB = False
61+
self.command = '\n>>>'
62+
63+
self.keyboard = [
64+
['`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '='],
65+
['TAB', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']'],
66+
['SYS', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '\\'],
67+
['SHIFT', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/']
68+
]
69+
70+
self.sys_keyboard = [
71+
['~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+'],
72+
['TAB', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '{', '}'],
73+
['SYS', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ':', '"', '|'],
74+
['SHIFT', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '<', '>', '?']
75+
]
76+
77+
def run_code(self, code):
78+
code = code.replace('>>>', '')
79+
old_stdout = sys.stdout
80+
old_stderr = sys.stderr
81+
redirected_output = sys.stdout = StringIO()
82+
redirected_error = sys.stderr = StringIO()
83+
84+
ns_globals = {}
85+
ns_locals = {}
86+
out, err, exc = None, None, None
87+
88+
try:
89+
exec(code, ns_globals, ns_locals)
90+
except:
91+
import traceback
92+
exc = traceback.format_exc()
93+
94+
out = redirected_output.getvalue()
95+
err = redirected_error.getvalue()
96+
97+
# reset outputs to the original values
98+
sys.stdout = old_stdout
99+
sys.stderr = old_stderr
100+
101+
return out, err, exc
102+
103+
def shift_key(self):
104+
if self.CAPS:
105+
self.CAPS = False
106+
else:
107+
self.CAPS = True
108+
109+
def sys_key(self):
110+
if self.SYS:
111+
self.SYS = False
112+
else:
113+
self.SYS = True
114+
115+
def settings(self):
116+
if self.setting_toggle:
117+
self.setting_toggle = False
118+
else:
119+
self.setting_toggle = True
120+
121+
def keyboard_key(self, key:str, same_line=False, *, default:str=None, color=None):
122+
if same_line:
123+
imgui.same_line()
124+
125+
if self.CAPS:
126+
key = key.upper()
127+
128+
if color is None:
129+
color = self.KEY_COLOR
130+
131+
imgui.push_style_color(imgui.COLOR_BUTTON, *color)
132+
if imgui.button(key, width=80, height=60):
133+
if default is None:
134+
if self.command == '':
135+
self.command = key
136+
else:
137+
self.command = self.command + key
138+
elif default == 'SHIFT':
139+
self.shift_key()
140+
elif default == 'SYS':
141+
self.sys_key()
142+
elif default == 'TAB':
143+
if self.TAB == False:
144+
self.command = self.command.replace('>>>', '>>>\n')
145+
self.TAB = True
146+
self.command = self.command + ' '
147+
imgui.pop_style_color(1)
148+
149+
def main(self):
150+
while True:
151+
self.renderer.handleinputs()
152+
imgui.new_frame()
153+
154+
self.width, self.height = self.renderer.io.display_size
155+
imgui.set_next_window_size(self.width, self.height)
156+
imgui.set_next_window_position(0, 0)
157+
# Header
158+
imgui.begin("",
159+
flags=imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_SAVED_SETTINGS
160+
)
161+
162+
imgui.text("PyNx Terminal By PuffDip" + " - V" + str(self.version_number))
163+
164+
# Body
165+
if self.keyboard_toggled or self.setting_toggle:
166+
imgui.begin_child("region", -5, -430, border=True)
167+
else:
168+
imgui.begin_child("region", -5, -110, border=True)
169+
imgui.text("\n\n".join(self.user_input) + self.command)
170+
imgui.end_child()
171+
172+
imgui.begin_group()
173+
if not self.setting_toggle:
174+
# Keyboard
175+
try:
176+
if self.keyboard_toggled:
177+
if self.SYS:
178+
keyboard = self.sys_keyboard
179+
else:
180+
keyboard = self.keyboard
181+
182+
for rows in keyboard:
183+
for row in rows:
184+
if row == 'TAB' or row == 'SYS' or row == 'SHIFT':
185+
self.keyboard_key(row, False, default=row, color=self.KEY_FUNC_COLOR)
186+
elif row == '`' or row == '~':
187+
self.keyboard_key(row, False)
188+
else:
189+
self.keyboard_key(row, True)
190+
191+
imgui.same_line()
192+
193+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_FUNC_COLOR)
194+
if imgui.button("ENTER", width=175, height=60):
195+
self.command = self.command + "\n"
196+
imgui.pop_style_color(1)
197+
198+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_FUNC_COLOR)
199+
if imgui.button("SPACE", width=970, height=50):
200+
self.command = self.command + " "
201+
imgui.pop_style_color(1)
202+
203+
imgui.same_line()
204+
205+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_FUNC_COLOR)
206+
if imgui.button("CLEAR", width=80, height=50):
207+
self.command = '\n>>>'
208+
imgui.pop_style_color(1)
209+
210+
imgui.same_line()
211+
212+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_FUNC_COLOR)
213+
if imgui.button("BACKSPACE", width=150, height=50):
214+
self.command = self.command[:-1]
215+
imgui.pop_style_color(1)
216+
217+
except Exception as e:
218+
logging.error(e)
219+
self.CONSOLE_TEXT = str(e)
220+
self.user_input.append(self.CONSOLE_TEXT)
221+
else:
222+
# Settings
223+
try:
224+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_FUNC_COLOR)
225+
if imgui.button("File System", width=150, height=50):
226+
break
227+
imgui.pop_style_color(1)
228+
except Exception as e:
229+
logging.error(e)
230+
self.CONSOLE_TEXT = str(e)
231+
self.user_input.append(self.CONSOLE_TEXT)
232+
233+
imgui.end_group()
234+
235+
# Command line
236+
imgui.text("Keyboard: {} | Shift: {} | SYS: {}".format(self.keyboard_toggled, self.CAPS, self.SYS))
237+
238+
imgui.begin_child(
239+
"Input", height=70, width=-500, border=True,
240+
flags=imgui.WINDOW_ALWAYS_VERTICAL_SCROLLBAR
241+
)
242+
command = self.command
243+
imgui.text(command)
244+
imgui.end_child()
245+
246+
# Buttons
247+
imgui.same_line()
248+
249+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_COLOR)
250+
if imgui.button("Keyboard", width=200, height=60):
251+
if self.keyboard_toggled:
252+
self.keyboard_toggled = False
253+
else:
254+
self.keyboard_toggled = True
255+
if self.setting_toggle:
256+
self.setting_toggle = False
257+
imgui.pop_style_color(1)
258+
259+
imgui.same_line()
260+
261+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_COLOR)
262+
if imgui.button("Confirm", width=200, height=60):
263+
if self.TAB:
264+
self.TAB = False
265+
266+
out, err, exc = self.run_code(command)
267+
268+
if out:
269+
self.CONSOLE_TEXT = out
270+
if err:
271+
self.CONSOLE_TEXT = err
272+
if exc:
273+
self.CONSOLE_TEXT = exc
274+
275+
self.user_input.append(command)
276+
self.user_input.append(self.CONSOLE_TEXT)
277+
self.command = '\n>>>'
278+
imgui.pop_style_color(1)
279+
280+
imgui.same_line()
281+
282+
imgui.push_style_color(imgui.COLOR_BUTTON, *self.KEY_FUNC_COLOR)
283+
if imgui.button("S", width=40, height=40):
284+
self.settings()
285+
imgui.pop_style_color(1)
286+
287+
imgui.end()
288+
imgui.render()
289+
self.renderer.render()

0 commit comments

Comments
 (0)