about summary refs log tree commit homepage
path: root/bin/dtas-console
diff options
context:
space:
mode:
Diffstat (limited to 'bin/dtas-console')
-rwxr-xr-xbin/dtas-console76
1 files changed, 52 insertions, 24 deletions
diff --git a/bin/dtas-console b/bin/dtas-console
index 00b5cd8..eedd0f0 100755
--- a/bin/dtas-console
+++ b/bin/dtas-console
@@ -1,5 +1,5 @@
 #!/usr/bin/env ruby
-# Copyright (C) 2013-2019 all contributors <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 #
@@ -11,21 +11,24 @@ require 'dtas/sigevent'
 require 'dtas/process'
 require 'dtas/format'
 include DTAS::Process
-require 'yaml'
 begin
   require 'curses'
 rescue LoadError
   abort "please install the 'curses' RubyGem to use #$0"
 end
 
+# workaround https://bugs.debian.org/958973
+$VERBOSE = nil if RUBY_VERSION.to_f < 3.0
+
 tsec = false
 se = DTAS::Sigevent.new
 trap(:WINCH) { se.signal }
 w = DTAS::UNIXClient.new
 w.req_ok('watch')
 c = DTAS::UNIXClient.new
-cur = YAML.load(c.req('current'))
+cur = DTAS.yaml_load(c.req('current'))
 readable = [ se, w, $stdin ]
+set_title = (ENV['DISPLAY'] || ENV['WAYLAND_DISPLAY']) ? $stdout : nil
 
 # current rg mode
 rg_mode = DTAS::RGState::RG_MODE.keys.unshift("off")
@@ -34,6 +37,7 @@ if (rg = cur["rg"]) && (rg = rg["mode"])
 else
   rg_mode_i = 0
 end
+show_info = false
 
 def update_tfmt(prec, tsec)
   if tsec
@@ -53,8 +57,6 @@ tfmt = update_tfmt(prec_step[prec_nr], tsec)
 events = []
 interval = 1.0 / 10 ** prec_nr
 
-pause = nil
-
 def show_events(lineno, screen, events)
   Curses.setpos(lineno += 1, 0)
   Curses.clrtoeol
@@ -114,14 +116,14 @@ def rg_string(rg, current)
   rv
 end
 
-def may_fail(res, events)
+def may_fail(c, req, events)
+  res = c.req(req)
   events << res if res != "OK"
 end
 
 pre_mute_vol = 1.0
 enc_locale = Encoding.find("locale")
 $stdout.set_encoding(enc_locale)
-enc_opts = { undef: :replace, invalid: :replace, replace: '?' }
 begin
   Curses.init_screen
   Curses.nonl
@@ -135,7 +137,6 @@ begin
     pfmt = cur['format']
     elapsed = samples = 0
     fmt = total = ''
-    paused = false
     if current = cur['current']
       infile = current['infile'] || current['command']
       elapsed = DTAS.now - current['spawn_at']
@@ -150,7 +151,6 @@ begin
       end
     elsif cur['paused'] && infile = cur['current_paused']
       fmt = "[paused] (#{fmt_to_s(pfmt)})"
-      paused = true
       infile = infile['command'] if Hash === infile
       if Array === infile
         infile, elapsed = infile
@@ -169,7 +169,12 @@ begin
       # FS encoding != locale encoding, but we need to display an FS path
       # name to whatever locale the terminal is encoded to, so force it
       # and risk mojibake...
-      infile.encode(enc_locale, enc_opts)
+      infile.encode(enc_locale,
+                    undef: :replace, invalid: :replace, replace: '?')
+      if set_title
+        dir, base = File.split(infile)
+        set_title.syswrite("\033]0;#{base} dtas-console\07")
+      end
       Curses.setpos(lineno += 1, 0)
       Curses.clrtoeol
       Curses.addstr(infile)
@@ -206,6 +211,27 @@ begin
     Curses.addstr(extra.join(' '))
     pre_mute_vol = cur_vol if cur_vol != 0
 
+    if show_info && current && comments = current['comments']
+      Curses.setpos(lineno += 1, 0)
+      Curses.clrtoeol
+      Curses.addstr('comments:')
+      comments.each do |k,v|
+        v = v.split(/\n+/)
+        k = k.dump if /[[:cntrl:]]/ =~ k
+        if first = v.shift
+          Curses.setpos(lineno += 1, 0)
+          Curses.clrtoeol
+          first = first.dump if /[[:cntrl:]]/ =~ first
+          Curses.addstr("  #{k}: #{first}")
+          v.each do |val|
+            val = val.dump if /[[:cntrl:]]/ =~ val
+            Curses.setpos(lineno += 1, 0)
+            Curses.clrtoeol
+            Curses.addstr("   #{val}")
+          end
+        end
+      end
+    end
     show_events(lineno, screen, events)
 
     Curses.refresh # draw and wait
@@ -220,29 +246,28 @@ begin
         case event
         when "pause"
           if current
-            pause = current['infile'] || current['command']
+            current['infile'] || current['command']
           end
         when %r{\Afile }
-          pause = nil
         end
         events << "#{Time.now.strftime(tfmt)} #{event}"
         # something happened, refresh current
         # we could be more intelligent here, maybe, but too much work.
-        cur = YAML.load(c.req('current'))
+        cur = DTAS.yaml_load(c.req('current'))
       when $stdin
         # keybindings taken from mplayer / vi
         case key = Curses.getch
-        when "j" then c.req_ok("seek -5")
-        when "k" then c.req_ok("seek +5")
+        when "j" then may_fail(c, "seek -5", events)
+        when "k" then may_fail(c, "seek +5", events)
         when "q" then exit(0)
-        when Curses::KEY_DOWN then c.req_ok("seek -60")
-        when Curses::KEY_UP then c.req_ok("seek +60")
-        when Curses::KEY_LEFT then c.req_ok("seek -10")
-        when Curses::KEY_RIGHT then c.req_ok("seek +10")
-        when Curses::KEY_BACKSPACE then c.req_ok("seek 0")
+        when Curses::KEY_DOWN then may_fail(c, "seek -60", events)
+        when Curses::KEY_UP then may_fail(c, "seek +60", events)
+        when Curses::KEY_LEFT then may_fail(c, "seek -10", events)
+        when Curses::KEY_RIGHT then may_fail(c, "seek +10", events)
+        when Curses::KEY_BACKSPACE then may_fail(c, "seek 0", events)
         # yes, some of us have long audio files
-        when Curses::KEY_PPAGE then c.req_ok("seek +600")
-        when Curses::KEY_NPAGE then c.req_ok("seek -600")
+        when Curses::KEY_PPAGE then may_fail(c, "seek +600", events)
+        when Curses::KEY_NPAGE then may_fail(c, "seek -600", events)
         when '9' then c.req_ok('rg volume-=0.01')
         when '0' then c.req_ok('rg volume+=0.01')
         when '=' then c.req_ok('rg volume=1')
@@ -253,8 +278,8 @@ begin
         when "f" then c.req_ok("rg fallback_gain-=1")
         when ">" then c.req_ok("tl next")
         when "<" then c.req_ok("tl prev")
-        when "!" then may_fail(c.req("cue prev"), events)
-        when "@" then may_fail(c.req("cue next"), events)
+        when "!" then may_fail(c, "cue prev", events)
+        when "@" then may_fail(c, "cue next", events)
         when "o" then tfmt = update_tfmt(prec_step[prec_nr], tsec = !tsec)
         when " "
           c.req("play_pause")
@@ -276,6 +301,9 @@ begin
             interval = 1.0 / 10 ** prec_nr
           end
         when 27 # TODO readline/edit mode?
+        when 'i'
+          show_info = !show_info
+          Curses.clear if !show_info
         else
           Curses.setpos(screen.maxy - 1, 0)
           Curses.clrtoeol