[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Line wrapping in YAML output
From: |
Eric Wong |
Subject: |
Re: Line wrapping in YAML output |
Date: |
Mon, 15 Jun 2020 00:24:34 +0000 |
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