-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneptune.py
More file actions
295 lines (250 loc) · 8.73 KB
/
Copy pathneptune.py
File metadata and controls
295 lines (250 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
import os
import sys
from nepterm import exec
# For Linux/Wayland users.
if os.getenv("XDG_SESSION_TYPE") == "wayland":
os.environ["XDG_SESSION_TYPE"] = "x11"
import glfw
import OpenGL.GL as gl
import imgui
from json import load
from imgui.integrations.glfw import GlfwRenderer
FILE_EXPLORER_DEFAULT_FILE_PATH = os.getcwd()
with open("parameters.json") as file:
parameters = load(file)
try:
FILE_EXPLORER_DEFAULT_FILE_PATH = parameters["file explorer"]["default file path"]
except:
pass
path_to_font = None # "path/to/font.ttf"
active_file_explorer = [FILE_EXPLORER_DEFAULT_FILE_PATH]
active_document_viewer = []
active_text_editor = []
active_terminal = []
active_settings = False
def open_settings():
imgui.begin("Settings")
imgui.text("Hello, World!")
imgui.end()
def open_terminal(file_path: str, window_name: str):
pass
def open_text_editor(file_path: str, window_name: str = None):
if window_name == None:
window_name = f"T @ {file_path}"
global active_text_editor
_, is_active = imgui.begin(window_name, closable=True)
imgui.begin_group()
if not is_active:
active_text_editor.remove(file_path)
else:
with open(file_path, "r") as file:
value = "".join(file.readlines())
is_changed, text = imgui.input_text_multiline(
"##label",
value,
width=-1,
height=-1,
flags=imgui.INPUT_TEXT_AUTO_SELECT_ALL | imgui.INPUT_TEXT_ALLOW_TAB_INPUT,
)
io = imgui.get_io()
# save
if io.key_ctrl and io.keys_down[glfw.KEY_S]:
if is_changed:
with open(file_path, "w") as file:
parsed = text.split("\n")
for i in range(len(parsed) - 1):
parsed[i] = parsed[i].rstrip() + "\n"
file.writelines(parsed)
imgui.end_group()
imgui.end()
def open_document_viewer(file_path: str, window_name: str = None):
if window_name == None:
window_name = f"V @ {file_path}"
global active_document_viewer, active_text_editor
_, is_active = imgui.begin(window_name, closable=True)
if not is_active:
active_document_viewer.remove(file_path)
else:
if imgui.button("Edit"):
active_text_editor.append(file_path)
imgui.same_line()
imgui.text("Read only mode")
with open(file_path, "r") as file:
value = "".join(file.readlines())
imgui.input_text_multiline(
"##label",
value,
width=-1,
height=-1,
flags=imgui.INPUT_TEXT_AUTO_SELECT_ALL | imgui.INPUT_TEXT_READ_ONLY,
)
imgui.end()
def open_file_explorer(file_path: str, window_name: str = None):
if window_name == None:
window_name = f"X @ {file_path}"
global active_file_explorer, active_document_viewer
window_name = f"X @ {file_path}"
_, is_active = imgui.begin(
window_name,
closable=True,
flags=imgui.WINDOW_NO_SCROLLBAR | imgui.WINDOW_ALWAYS_AUTO_RESIZE,
)
if not is_active:
active_file_explorer.remove(file_path)
imgui.end()
return
imgui.begin_group()
imgui.begin_child(
"files",
height=350,
border=True,
flags=imgui.WINDOW_ALWAYS_VERTICAL_SCROLLBAR
| imgui.WINDOW_ALWAYS_HORIZONTAL_SCROLLBAR,
)
files = [file for file in os.listdir(file_path)]
# for current working directory
imgui.push_style_color(imgui.COLOR_BUTTON, r=0.396, g=0.454, b=0.196)
imgui.push_style_color(imgui.COLOR_BUTTON_HOVERED, r=0.36, g=0.66, b=0.02)
imgui.push_style_color(imgui.COLOR_BUTTON_ACTIVE, r=0.396, g=0.454, b=0.196)
if imgui.button("."):
pass
imgui.pop_style_color()
imgui.pop_style_color()
imgui.pop_style_color()
# for parent working directory
imgui.push_style_color(imgui.COLOR_BUTTON, r=0.396, g=0.454, b=0.196)
imgui.push_style_color(imgui.COLOR_BUTTON_HOVERED, r=0.36, g=0.66, b=0.02)
imgui.push_style_color(imgui.COLOR_BUTTON_ACTIVE, r=0.396, g=0.454, b=0.196)
if imgui.button(".."):
os.chdir(f"{file_path}/..")
active_file_explorer.remove(file_path)
file_path = os.getcwd()
active_file_explorer.append(file_path)
imgui.pop_style_color()
imgui.pop_style_color()
imgui.pop_style_color()
for file in files:
if file == "imgui.ini":
continue
if not os.path.isfile(file):
imgui.push_style_color(imgui.COLOR_BUTTON, r=0.396, g=0.454, b=0.196)
imgui.push_style_color(imgui.COLOR_BUTTON_HOVERED, r=0.36, g=0.66, b=0.02)
imgui.push_style_color(imgui.COLOR_BUTTON_ACTIVE, r=0.396, g=0.454, b=0.196)
if imgui.button(file):
os.chdir(f"{file_path}/{file}")
active_file_explorer.remove(file_path)
file_path = os.getcwd()
active_file_explorer.append(file_path)
imgui.pop_style_color()
imgui.pop_style_color()
imgui.pop_style_color()
else:
if imgui.button(file):
active_document_viewer.append(f"{file_path}/{file}")
imgui.end_child()
enter_pressed, command = imgui.input_text(
"Console", "", flags=imgui.INPUT_TEXT_ENTER_RETURNS_TRUE
)
if enter_pressed:
os.system(command)
imgui.same_line()
if imgui.button("Open in terminal"):
pass
imgui.end_group()
imgui.end()
def handle_key_press():
io = imgui.get_io()
# exit
if io.key_ctrl and io.keys_down[glfw.KEY_W]:
sys.exit(0)
# new file explorer
if io.key_ctrl and io.keys_down[glfw.KEY_X]:
# active_file_explorer.append(FILE_EXPLORER_DEFAULT_FILE_PATH)
pass
def frame_commands():
global active_file_explorer, active_text_editor
gl.glClearColor(0.1, 0.1, 0.1, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
handle_key_press()
if imgui.begin_main_menu_bar():
if imgui.begin_menu("File", True):
clicked_settings, selected_settings = imgui.menu_item(
"Settings", "Ctrl+S", False, True
)
clicked_exit, _ = imgui.menu_item("exit", "Ctrl+W", False, True)
if clicked_exit:
sys.exit(0)
if clicked_settings:
open_settings()
imgui.end_menu()
if imgui.begin_menu("New", True):
if imgui.menu_item("File explorer", "Ctrl+X", False, True)[0]:
# active_file_explorer.append(FILE_EXPLORER_DEFAULT_FILE_PATH)
pass
if imgui.menu_item("Terminal", "Ctrl+T", False, True)[0]:
pass
if imgui.menu_item("Text editor", "Ctrl+E", False, True)[0]:
pass
imgui.end_menu()
imgui.end_main_menu_bar()
file_explorer_dict = dict()
for fe in active_file_explorer:
if fe not in file_explorer_dict:
open_file_explorer(fe)
file_explorer_dict[fe] = 1
else:
open_file_explorer(fe, window_name=f"X @ {fe} ({file_explorer_dict[fe]})")
file_explorer_dict[fe] += 1
for te in active_text_editor:
open_text_editor(te)
for dv in active_document_viewer:
open_document_viewer(dv)
def render_frame(impl, window, font):
glfw.poll_events()
impl.process_inputs()
imgui.new_frame()
gl.glClearColor(0.1, 0.1, 0.1, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
if font is not None:
imgui.push_font(font)
frame_commands()
if font is not None:
imgui.pop_font()
imgui.render()
impl.render(imgui.get_draw_data())
glfw.swap_buffers(window)
def impl_glfw_init():
width, height = 1600, 900
window_name = "Neptune"
if not glfw.init():
print("Could not initialize OpenGL context")
sys.exit(1)
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
window = glfw.create_window(int(width), int(height), window_name, None, None)
glfw.make_context_current(window)
if not window:
glfw.terminate()
print("Could not initialize Window")
sys.exit(1)
return window
def main():
imgui.create_context()
window = impl_glfw_init()
impl = GlfwRenderer(window)
io = imgui.get_io()
jb = (
io.fonts.add_font_from_file_ttf(path_to_font, 30)
if path_to_font is not None
else None
)
impl.refresh_font_texture()
while not glfw.window_should_close(window):
render_frame(impl, window, jb)
impl.shutdown()
glfw.terminate()
if __name__ == "__main__":
main()