-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedcurses.py
executable file
·159 lines (122 loc) · 3.15 KB
/
medcurses.py
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
#! /usr/bin/python3
import curses
import os
from subprocess import call
from re import sub
_home = os.path.expanduser("~")
_path = "{}/roms".format(_home)
_files = []
_numFiles = 0
_cursorPos = 0
_noExit = True
_windows = []
def main(stdscr):
global _files
global _numFiles
global _noExit
screenLayout()
_files = getFiles(_path)
_numFiles = len(_files)
while _noExit:
printFiles(_files, _windows[2])
inputCheck(_windows[2])
def inputCheck(screen):
global _cursorPos
global _noExit
global _numFiles
KEY_ESC = 27
KEY_ENTER = 10
input = screen.getch()
if input == curses.KEY_DOWN:
_cursorPos = (_cursorPos + 1) % _numFiles
elif input == curses.KEY_UP:
_cursorPos = (_cursorPos - 1) % _numFiles
elif input == KEY_ENTER:
runMednafen()
cleanup()
elif input == KEY_ESC:
_noExit = False
def runMednafen():
global _home
xinitrc = "{}/.xinitrc".format(_home)
newXinitrc = xinitrcString()
if os.path.isfile(xinitrc):
os.rename(xinitrc, "{}.bak".format(xinitrc))
file = open(xinitrc, "w")
file.write(newXinitrc)
file.close()
call("startx")
def cleanup():
global _home
xinitrc = "{}/.xinitrc".format(_home)
xinitrcBak = "{}.bak".format(xinitrc)
os.remove(xinitrc)
if os.path.isfile(xinitrcBak):
os.rename(xinitrcBak, xinitrc)
def xinitrcString():
global _cursorPos
global _files
game = sub(" ", "\ ", _files[_cursorPos])
string = "exec mednafen {}".format(game)
return string
def getFiles(path):
files = []
for (dirpath, dirnames, filenames) in os.walk(path):
for name in filenames:
filename = name
files.append(os.path.join(dirpath, filename))
return sorted(files)
def printFiles(files, window):
global _cursorPos
line = 0
window.clear()
for file in files:
if line == _cursorPos:
window.addstr(line, 0, file, curses.color_pair(1))
length = len(file)
else:
window.addstr(line, 0, file)
line += 1
# Refresh only the print window
refreshOne(window)
def screenLayout():
global _windows
global _path
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
heightRemaining = curses.LINES
# Title bar
title_h = 1
title = "MedCurses ROM path: {}".format(_path)
titlebar = curses.newwin(title_h, curses.COLS, 0, 0)
titlebar.bkgd(' ', curses.color_pair(1))
titlebar.addstr(0, center(title), title)
titlebar.noutrefresh()
_windows.append(titlebar)
# Instructions
instruct_y = curses.LINES - 1
instruct_h = 1
instructStr = "Enter: Run Selected ROM Up/Down: Select ROM ESC: Return to Terminal"
instructWin = curses.newwin(instruct_h, curses.COLS, instruct_y, 0)
instructWin.addstr(0, center(instructStr), instructStr)
instructWin.noutrefresh()
_windows.append(instructWin)
# Filelist
filelist_y = 2
filelist_h = curses.LINES - title_h - instruct_h - 1
filelist = curses.newwin(filelist_h, curses.COLS, filelist_y, 0)
filelist.keypad(True)
_windows.append(filelist)
# Directory Input
# TODO
curses.doupdate()
def center(string):
return (curses.COLS // 2) - (len(string) // 2)
def refreshAll():
global _windows
for window in _windows:
window.noutrefresh()
curses.doupdate()
def refreshOne(window):
window.noutrefresh()
curses.doupdate()
curses.wrapper(main)