cplay-ng

simple curses audio player
git clone https://git.ce9e.org/cplay-ng.git

commit
976ed25ecc6f41a3da1385b3d873c19a7f1b662d
parent
fb83a27cbbeee8ffca83f221fdcb922744022c73
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-09-03 08:31
implement search

Diffstat

M cplay.py 51 +++++++++++++++++++++++++++++++++++++++++++++++++--

1 files changed, 49 insertions, 2 deletions


diff --git a/cplay.py b/cplay.py

@@ -27,6 +27,8 @@ Tab          : switch between filelist/playlist
   27    27 n            : next track
   28    28 x, Space     : toggle play/pause
   29    29 Left, Right  : seek backward/forward
   -1    30 /            : search
   -1    31 C-s, C-r     : next/previous search match
   30    32 Esc          : cancel
   31    33 0..9         : volume control
   32    34 l            : list mode
@@ -187,6 +189,29 @@ class Player:
  187   189         return self._proc is not None and self._proc.poll() is not None
  188   190 
  189   191 
   -1   192 class Input:
   -1   193     def __init__(self):
   -1   194         self.active = False
   -1   195         self.str = ''
   -1   196 
   -1   197     def process_key(self, key):
   -1   198         if not self.active:
   -1   199             return False
   -1   200         if key == chr(27):
   -1   201             self.str = ''
   -1   202             self.active = False
   -1   203         elif key == '\n':
   -1   204             self.active = False
   -1   205         elif key == curses.KEY_BACKSPACE:
   -1   206             self.str = self.str[:-1]
   -1   207         elif isinstance(key, str):
   -1   208             self.str += key
   -1   209         else:
   -1   210             self.active = False
   -1   211             return False
   -1   212         return True
   -1   213 
   -1   214 
  190   215 class List:
  191   216     def __init__(self):
  192   217         self.items = []
@@ -210,6 +235,14 @@ class List:
  210   235     def move_cursor(self, diff):
  211   236         self.set_cursor(self.cursor + diff)
  212   237 
   -1   238     def search(self, q, diff=1, offset=0):
   -1   239         for i in range(len(self.items)):
   -1   240             pos = (self.cursor + (i + offset) * diff) % len(self.items)
   -1   241             if q.lower() in self.format_item(self.items[pos]).lower():
   -1   242                 self.set_cursor(pos)
   -1   243                 return True
   -1   244         return False
   -1   245 
  213   246     def format_item(self, item):
  214   247         if app.verbose:
  215   248             name = item
@@ -472,6 +505,7 @@ class Application:
  472   505         self.tabs = [filelist, playlist]
  473   506         self.verbose = False
  474   507         self.help = False
   -1   508         self.input = Input()
  475   509         self.old_lines = []
  476   510 
  477   511     def refresh_dimensions(self):
@@ -510,7 +544,9 @@ class Application:
  510   544         yield from self.tab.render()
  511   545         yield self.format_progress()
  512   546 
  513    -1         if player.path and player._proc:
   -1   547         if self.input.active:
   -1   548             status = '/%s' % self.input.str
   -1   549         elif player.path and player._proc:
  514   550             status = 'Playing %s' % player.path
  515   551         else:
  516   552             status = 'Stopped'
@@ -525,7 +561,9 @@ class Application:
  525   561         self.apply(list(self._render()))
  526   562 
  527   563     def process_key(self, key):
  528    -1         if self.tab.process_key(key):
   -1   564         if self.input.process_key(key):
   -1   565             self.tab.search(self.input.str)
   -1   566         elif self.tab.process_key(key):
  529   567             pass
  530   568         elif key in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
  531   569             set_volume(int(key, 10) / 9.0)
@@ -537,6 +575,15 @@ class Application:
  537   575             player.toggle()
  538   576         elif key == 'n':
  539   577             player.play(playlist.next())
   -1   578         elif key == '/':
   -1   579             self.input.str = ''
   -1   580             self.input.active = True
   -1   581         elif key == chr(19):
   -1   582             if self.input.str:
   -1   583                 self.tab.search(self.input.str, 1, 1)
   -1   584         elif key == chr(18):
   -1   585             if self.input.str:
   -1   586                 self.tab.search(self.input.str, -1, 1)
  540   587         elif key == 'h':
  541   588             self.help = True
  542   589         elif key in ['q', 'Q']: