-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjamp
executable file
·278 lines (235 loc) · 6.89 KB
/
jamp
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
#!/usr/bin/python
'''
Name:
jamp (just another music player)
Description:
a curses based music player for the OSX terminal.
Misc:
audio_types = ['.mp3', '.mp4', '.wav']
directory = os.walk(os.getcwd()).next()
children = directory[1]
files = directory[2]
directory = directory[0]
'''
import curses, traceback, time, subprocess, os, sys
from AppKit import NSSound
UP = curses.KEY_UP
DOWN = curses.KEY_DOWN
RESIZE = curses.KEY_RESIZE
PAGE_DOWN = 100 # D
PAGE_UP = 115 # S
QUIT = 113 # Q
JUMP = 106 # J
RETURN = 10 # ENTER
PAUSE = 32 # SPACEBAR
sound = None
y, x = 2, 1
screen_height = 0
screen_width = 0
content_position = 0
content_height = 0
pad_position = 0
pad_height = 0
directory = []
currently_playing = ''
playing = False
sub_directories = []
contents = []
pad = object
audio_types = ['.mp3', '.mp4', '.m4a', '.wav', '.ogg']
home = subprocess.check_output('echo ~', shell=True).strip('\n')
os.chdir(home + '/Music/iTunes/iTunes Media/Music')
def play(filename):
global sound, playing
sound = NSSound.alloc();
path_to_file = os.getcwd() + '/' + filename
sound.initWithContentsOfFile_byReference_(path_to_file, True)
sound.play()
playing = True
def stop():
global sound, playing
sound.stop()
playing = False
def pause():
global sound, playing
sound.pause()
playing = False
def resume():
global sound, playing
sound.resume()
playing = True
def current_directory():
global sub_directories, contents
curr_dir = os.walk(os.getcwd()).next()
sub_directories = curr_dir[1]
contents = curr_dir[2]
def main(screen):
global y, x, pad, screen_height, screen_width, pad_height, pad_width, sub_directories, contents
current_directory()
screen_height, screen_width = screen.getmaxyx()
pad = curses.newpad(screen_height - 4, screen_width)
pad.scrollok(1)
pad.idlok(1)
pad_height, pad_width = pad.getmaxyx()
# curses.start_color()
# curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_CYAN)
# pad.bkgd(1, curses.color_pair(1))
pad.setscrreg(0, pad_height - 1)
draw()
screen.nodelay(1)
while True:
key = screen.getch()
if key == QUIT: # q
break
else:
handle_key_press(key)
curses.doupdate()
time.sleep(0.1)
def draw():
global screen_height, screen_width, content_position, content_height, pad, y, x, pad_height, directory, sub_directories, contents, currently_playing
down_ctr = 0
try:
screen.addstr(0, 1, 'jamp 0.1b', curses.A_BOLD)
screen.addstr(1, 1, ('-' * (screen_width - 2)))
#screen.addstr(screen_height - 2, 1, "Changed directory to %s" % directory[content_position])
screen.addstr(screen_height - 1, 1, currently_playing, curses.A_BOLD)
#screen.addstr(screen_height - 2, 1, 'user action', curses.A_BOLD)
screen.addstr(screen_height - 3, 1, ('-' * (screen_width - 2)))
sub_directories.insert(0, '..')
for subdir in sub_directories:
directory.append(subdir + '/')
for filename in contents:
if filename[-4:] in audio_types:
directory.append(filename)
for item in directory[content_position:pad_height - 1]:
pad.addstr(down_ctr, 0, item)
down_ctr += 1
content_height += 1
screen.refresh()
refresh_pad()
screen.move(2, 1)
except:
traceback.print_exc()
def refresh_pad():
global screen_height, screen_width
pad.refresh(0, 0, 2, 1, pad_height, screen_width - 2)
def handle_key_press(key):
global screen_height, screen_width, x, y, content_position, content_height, pad_position, pad_height, pad_width, pad, screen, sub_directories, directory, playing, currently_playing
y, x = screen.getyx()
y_prev, x_prev = y, x
dir_len = len(directory) - 1
stats()
if key == DOWN and y <= content_height and y <= pad_height:
y += 1
pad_position += 1
content_position += 1
elif key == PAGE_DOWN and y <= content_height and y <= pad_height:
y = content_height + 1
pad_position = content_height + 1
content_position = content_height - 1
elif key == PAGE_UP and y >= content_height and y >= pad_height:
# y = content_height + 1
y = 2
# pad_position = content_height + 1
pad_position = 2
# content_position = content_height - 1
content_position = content_position - content_height + 1
elif key == DOWN and dir_len > pad_height and content_position < dir_len:
scroll_down()
elif key == UP and y > 2:
y -= 1
pad_position -= 1
content_position -= 1
elif (key == UP and
dir_len > pad_height and
y == 2 and
content_position > 0):
scroll_up()
elif key == RESIZE:
screen.clear()
pad.clear()
screen_height, screen_width = screen.getmaxyx()
curses.resizeterm(screen_height, screen_width)
pad.resize(screen_height - 4, screen_width - 1)
pad_height, pad_width = pad.getmaxyx()
reset_counters()
directory = []
sub_directories.remove('..')
y, x = 2, 1
draw()
elif key == RETURN:
if directory[content_position] in contents:
clear_line(screen_height - 1, 1)
if playing:
stop()
play(directory[content_position])
currently_playing = "Playing: Artist - %s" % directory[content_position]
screen.addstr(screen_height - 1, 1, currently_playing, curses.A_BOLD)
elif directory[content_position][0:-1] in sub_directories:
clear_line(screen_height - 2, 1)
os.chdir(directory[content_position][0:-1])
screen.addstr(screen_height - 2, 1, "Changed directory to %s" % directory[content_position])
pad.clear()
directory = []
y, x = 2, 1
reset_counters()
current_directory()
draw()
elif key == 32: # SPACEBAR
if playing:
pause()
else:
resume()
screen.move(y, x)
def scroll_up():
global pad, content_position, pad_height, directory
pad.scroll(-1)
content_position -= 1
pad.addstr(0, 0, directory[content_position])
refresh_pad()
def scroll_down():
global pad, content_position, pad_height, directory
pad.scroll(1)
content_position += 1
pad.addstr(pad_height - 2, 0, directory[content_position])
refresh_pad()
def reset_counters():
global content_position, content_height, pad_position, down_ctr
content_position = 0
content_height = 0
pad_position = 0
down_ctr = 0
def clear_line(x_line, y_line):
screen.move(x_line, y_line)
screen.clrtoeol()
screen.move(y, x)
def stats():
screen.move(0, 15)
screen.clrtoeol()
screen.move(y, x)
unit = screen_width / 6
#screen.addstr(0, 0, ("[screen height: " + str(screen_height) + "]"))
#screen.addstr(0, unit * 1, ("[playing: " + str(playing) + "]"))
#screen.addstr(0, unit * 2, ("[song: " + directory[content_position] + "]"))
screen.addstr(0, unit * 3, ("[pad position: " + str(pad_position) + "]"))
screen.addstr(0, unit * 4, ("[content position: " + str(content_position) + "]"))
#screen.addstr(0, 62, ("[dir length: " + str(len(directory)) + "]"))
if __name__=='__main__':
try:
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(1)
screen.keypad(1)
main(screen)
screen.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
except:
# In event of error, restore terminal to sane state.
screen.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
traceback.print_exc()