cplay-ng

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

commit
5c2145bce0cf453a6fd13cd593f51dc0ec0471ad
parent
35a37e600b1b6eee84614cac13ac7b118cfa0867
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-09-05 07:52
refactor: single input service

Diffstat

M cplay.py 55 +++++++++++++++++++++++++++++++------------------------

1 files changed, 31 insertions, 24 deletions


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

@@ -199,6 +199,13 @@ class Input:
  199   199         self.active = False
  200   200         self.str = ''
  201   201 
   -1   202     def start(self, prompt, on_input):
   -1   203         self.str = ''
   -1   204         self.prompt = prompt
   -1   205         self.on_input = on_input
   -1   206         self.active = True
   -1   207         self.on_input(self.str)
   -1   208 
  202   209     def process_key(self, key):
  203   210         if not self.active:
  204   211             return False
@@ -214,6 +221,7 @@ class Input:
  214   221         else:
  215   222             self.active = False
  216   223             return False
   -1   224         self.on_input(self.str)
  217   225         return True
  218   226 
  219   227 
@@ -223,6 +231,7 @@ class List:
  223   231         self.position = 0
  224   232         self.cursor = 0
  225   233         self.active = -1
   -1   234         self.search_str = ''
  226   235 
  227   236     @property
  228   237     def rows(self):
@@ -241,6 +250,7 @@ class List:
  241   250         self.set_cursor(self.cursor + diff)
  242   251 
  243   252     def search(self, q, diff=1, offset=0):
   -1   253         self.search_str = q
  244   254         for i in range(len(self.items)):
  245   255             pos = (self.cursor + (i + offset) * diff) % len(self.items)
  246   256             if str_match(q, self.format_item(self.items[pos])):
@@ -284,6 +294,14 @@ class List:
  284   294             self.set_cursor(len(self.items))
  285   295         elif key in [curses.KEY_HOME, 'g']:
  286   296             self.set_cursor(0)
   -1   297         elif key == '/':
   -1   298             app.input.start('/', self.search)
   -1   299         elif key == chr(19):
   -1   300             if self.search_str:
   -1   301                 self.search(self.search_str, 1, 1)
   -1   302         elif key == chr(18):
   -1   303             if self.search_str:
   -1   304                 self.search(self.search_str, -1, 1)
  287   305         else:
  288   306             return False
  289   307         return True
@@ -312,17 +330,17 @@ class Filelist(List):
  312   330     def __init__(self):
  313   331         super().__init__()
  314   332         self.path = None
  315    -1         self.input = Input()
   -1   333         self.rsearch_str = ''
  316   334         self.set_path(os.getcwd())
  317   335 
  318   336     def get_title(self):
  319   337         title = 'Filelist: %s/' % self.path
  320    -1         if self.input.str:
  321    -1             title += 'search "%s"/' % self.input.str
   -1   338         if self.rsearch_str:
   -1   339             title += 'search "%s"/' % self.rsearch_str
  322   340         return title
  323   341 
  324   342     def format_item(self, item):
  325    -1         return super().format_item(item, force_verbose=self.input.str)
   -1   343         return super().format_item(item, force_verbose=self.rsearch_str)
  326   344 
  327   345     def set_path(self, path, prev=None):
  328   346         if path != self.path:
@@ -330,7 +348,7 @@ class Filelist(List):
  330   348             os.chdir(path)
  331   349             self.search_cache = []
  332   350         self.all_items = []
  333    -1         self.input.str = ''
   -1   351         self.rsearch_str = ''
  334   352 
  335   353         for p, is_dir in listdir(path):
  336   354             ext = p.rsplit('.', 1)[-1]
@@ -359,6 +377,8 @@ class Filelist(List):
  359   377         return results
  360   378 
  361   379     def filter(self, query):
   -1   380         self.rsearch_str = query
   -1   381 
  362   382         if not self.search_cache:
  363   383             self.search_cache = self.build_search_cache(self.path)
  364   384 
@@ -373,16 +393,14 @@ class Filelist(List):
  373   393         self.set_cursor(self.cursor)
  374   394 
  375   395     def process_key(self, key):
  376    -1         if self.input.process_key(key):
  377    -1             self.filter(self.input.str)
  378    -1         elif key == 'a':
   -1   396         if key == 'a':
  379   397             if not self.items:
  380   398                 return True
  381   399             if playlist.add(self.items[self.cursor]):
  382   400                 self.move_cursor(1)
  383   401         elif key == 's':
  384    -1             self.input.active = True
  385    -1             self.filter(self.input.str)
   -1   402             app.input.start('search: ', self.filter)
   -1   403             self.filter(self.rsearch_str)
  386   404         elif key == '\n':
  387   405             if not self.items:
  388   406                 return True
@@ -394,7 +412,7 @@ class Filelist(List):
  394   412                 playlist.active = -1
  395   413                 player.play(item)
  396   414         elif key == curses.KEY_BACKSPACE:
  397    -1             if self.input.str:
   -1   415             if self.rsearch_str:
  398   416                 self.set_path(self.path)
  399   417             else:
  400   418                 self.set_path(os.path.dirname(self.path), prev=self.path)
@@ -599,9 +617,7 @@ class Application:
  599   617         yield self.format_progress()
  600   618 
  601   619         if self.input.active:
  602    -1             status = '/%s' % self.input.str
  603    -1         elif self.tab == filelist and filelist.input.active:
  604    -1             status = 'search: %s' % filelist.input.str
   -1   620             status = self.input.prompt + self.input.str
  605   621         elif player.path and player._proc:
  606   622             status = 'Playing %s' % os.path.relpath(player.path)
  607   623         else:
@@ -618,7 +634,7 @@ class Application:
  618   634 
  619   635     def process_key(self, key):
  620   636         if self.input.process_key(key):
  621    -1             self.tab.search(self.input.str)
   -1   637             pass
  622   638         elif self.tab.process_key(key):
  623   639             pass
  624   640         elif key in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
@@ -631,15 +647,6 @@ class Application:
  631   647             player.toggle()
  632   648         elif key == 'n':
  633   649             player.play(playlist.next())
  634    -1         elif key == '/':
  635    -1             self.input.str = ''
  636    -1             self.input.active = True
  637    -1         elif key == chr(19):
  638    -1             if self.input.str:
  639    -1                 self.tab.search(self.input.str, 1, 1)
  640    -1         elif key == chr(18):
  641    -1             if self.input.str:
  642    -1                 self.tab.search(self.input.str, -1, 1)
  643   650         elif key == 'h':
  644   651             self.help = True
  645   652         elif key in ['q', 'Q']: