everything related to duct tape audio suite (dtas)
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: dtas-all@nongnu.org
Subject: Re: Line wrapping in YAML output
Date: Mon, 15 Jun 2020 00:24:34 +0000	[thread overview]
Message-ID: <20200615002434.GA30135@dcvr> (raw)
In-Reply-To: <20200613152543.owwp2ey3byvf2row@gmail.com>

James Rowe <jnrowe@gmail.com> wrote:
> Hi,
> 
>   I routinely find myself needing to use a “real language” when I want
> to perform a quick hack with dtas purely to workaround the default line
> wrapping in YAML output.  With unwrapped output sed/awk would often be
> a viable solution from the shell prompt.

Understandable.  Do you use dtas-ctl or some other socket tool?

On the flip side, one of the reasons I picked YAML over JSON is
the indented + wrapped-by-default nature made it
$EDITOR-friendly.

In retrospect, I kinda wish I'd gone with JSON for a wire format
and maybe YAML or even RFC822 for $EDITOR-friendliness.

>   Given that psych supports an options mapping where setting
> ``line_width: -1`` disables wrapping entirely, I’m wondering if there
> would be support for disabling the line wrapping.  I’m also wondering if
> I’m missing some drawbacks to doing so.

My patch below seems to work.  The dtas-*edit tools had to be
updated for $EDITOR-friendliness.

dtas-ctl output could become difficult-to-read on the terminal;
maybe it could detect stdout is a terminal and rewrap in that
case.

------8<------
From: Eric Wong <e@yhbt.net>
Subject: [PATCH] player: disable line wrapping in YAML socket output

Introduce DTAS.yaml wrapper to consistently disable
line wrapping in player output.  To keep dtas-*edit
commands from having excessively long lines, we'll
re-wrap them before spawning text editors.
---
 bin/dtas-sinkedit                 |  1 +
 bin/dtas-sourceedit               |  1 +
 lib/dtas.rb                       |  4 ++++
 lib/dtas/player.rb                |  2 +-
 lib/dtas/player/client_handler.rb | 16 ++++++++--------
 lib/dtas/state_file.rb            |  2 +-
 6 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/bin/dtas-sinkedit b/bin/dtas-sinkedit
index 3393aac..16ece0b 100755
--- a/bin/dtas-sinkedit
+++ b/bin/dtas-sinkedit
@@ -70,6 +70,7 @@
 else
   include DTAS::SpawnFix
   tmp = tmpyaml
+  buf = orig.to_yaml # re-enable line wrapping
   tmp_path = tmp.path
   do_update = lambda { commit_update.call(File.read(tmp_path)) }
   tmp.write(buf << DTAS_DISCLAIMER)
diff --git a/bin/dtas-sourceedit b/bin/dtas-sourceedit
index 23362c2..2e9dd57 100755
--- a/bin/dtas-sourceedit
+++ b/bin/dtas-sourceedit
@@ -57,6 +57,7 @@
 else
   include DTAS::SpawnFix
   tmp = tmpyaml
+  buf = orig.to_yaml # re-enable line wrapping
   tmp_path = tmp.path
   do_update = lambda { commit_update.call(File.read(tmp_path)) }
   tmp.write(buf << DTAS_DISCLAIMER)
diff --git a/lib/dtas.rb b/lib/dtas.rb
index 1eac704..23d5ea4 100644
--- a/lib/dtas.rb
+++ b/lib/dtas.rb
@@ -33,6 +33,10 @@ def self.libc
     end
   end
 
+  def self.yaml(obj)
+    obj.to_yaml(line_width: -1) # disable wrapping
+  end
+
   # String#-@ will deduplicate strings when Ruby 2.5 is released (Dec 2017)
   # https://bugs.ruby-lang.org/issues/13077
   if RUBY_VERSION.to_f >= 2.5
diff --git a/lib/dtas/player.rb b/lib/dtas/player.rb
index b39a2e7..f21b6a0 100644
--- a/lib/dtas/player.rb
+++ b/lib/dtas/player.rb
@@ -228,7 +228,7 @@ def dpc_clear(io, msg)
   end
 
   def dpc_queue(io, msg)
-    'cat' == msg[0] and io.emit(@queue.to_yaml)
+    'cat' == msg[0] and io.emit(DTAS.yaml(@queue))
   end
 
   def dpc_watch(io, _)
diff --git a/lib/dtas/player/client_handler.rb b/lib/dtas/player/client_handler.rb
index cf5442d..c11ed69 100644
--- a/lib/dtas/player/client_handler.rb
+++ b/lib/dtas/player/client_handler.rb
@@ -176,7 +176,7 @@ def dpc_sink(io, msg)
       io.emit("OK")
     when "cat"
       sink = @sinks[name] or return io.emit("ERR #{name} not found")
-      io.emit(sink.to_hash.to_yaml)
+      io.emit(DTAS.yaml(sink.to_hash))
     else
       io.emit("ERR unknown sink op #{msg[0]}")
     end
@@ -259,7 +259,7 @@ def current_expect_samples(in_samples) # @current.samples
   end
 
   def dpc_rg(io, msg)
-    return io.emit(@rg.to_hsh.to_yaml) if msg.empty?
+    return io.emit(DTAS.yaml(@rg.to_hsh)) if msg.empty?
     before = @rg.to_hsh
     msg.each do |kv|
       k, v = kv.split('=', 2)
@@ -333,7 +333,7 @@ def dpc_current(io, msg)
       end
     end
     tmp['tracklist'] = @tl.to_hsh(false)
-    io.emit(tmp.to_yaml)
+    io.emit(DTAS.yaml(tmp))
   end
 
   def __buf_reset(buf) # buf is always @sink_buf for now
@@ -351,12 +351,12 @@ def dpc_skip(io, msg)
   def play_pause_handler(io, command)
     prev = @paused
     __send__("do_#{command}")
-    io.emit({
+    io.emit(DTAS.yaml({
       "paused" => {
         "before" => prev,
         "after" => @paused,
       }
-    }.to_yaml)
+    }))
   end
 
   def do_pause
@@ -515,7 +515,7 @@ def dpc_source(io, msg)
     src = map[name] or return io.emit("ERR non-existent source name")
     case op
     when "cat"
-      io.emit(src.to_source_cat.to_yaml)
+      io.emit(DTAS.yaml(src.to_source_cat))
     when "ed"
       before = src.to_state_hash.inspect
       sd = src.source_defaults
@@ -642,7 +642,7 @@ def _dpc_tl_shuffle(io, msg)
     prev = (!!@tl.shuffle).to_s
     v = msg.shift
     case v
-    when 'debug' then return io.emit(@tl.shuffle.to_yaml) # TODO: remove
+    when 'debug' then return io.emit(DTAS.yaml(@tl.shuffle)) # TODO: remove
     when nil
     else
       set_bool(io, 'tl shuffle', v) do |b|
@@ -844,7 +844,7 @@ def dpc_cue(io, msg)
       case msg[0]
       when nil
         tmp = { "infile" => cur.infile, "cue" => bp.map(&:to_hash) }
-        io.emit(tmp.to_yaml)
+        io.emit(DTAS.yaml(tmp))
       when "next", "prev"
         return __bp_prev_next(io, msg, cur, bp)
       when 'seek' then return __bp_seek(io, msg, cur, bp)
diff --git a/lib/dtas/state_file.rb b/lib/dtas/state_file.rb
index eac3e2f..8d7d413 100644
--- a/lib/dtas/state_file.rb
+++ b/lib/dtas/state_file.rb
@@ -18,7 +18,7 @@ def tryload
   end
 
   def dump(obj, force_fsync = false)
-    yaml = obj.to_hsh.to_yaml.b
+    yaml = DTAS.yaml(obj.to_hsh).b
 
     # do not replace existing state file if there are no changes
     # this will be racy if we ever do async dumps or shared state


  reply	other threads:[~2020-06-15  0:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-13 15:25 Line wrapping in YAML output James Rowe
2020-06-15  0:24 ` Eric Wong [this message]
2020-06-15 11:17   ` James Rowe
2020-06-16  4:19     ` Eric Wong
2020-06-16  9:53       ` James Rowe
2020-06-28 23:39         ` Eric Wong
2020-09-23 10:37           ` James Rowe
2020-09-25 19:14             ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://80x24.org/dtas/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200615002434.GA30135@dcvr \
    --to=e@80x24.org \
    --cc=dtas-all@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/dtas.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).