Edit yaml objects in array with yq. Speed up Terminalizer's terminal cast (record)
Asked Answered
C

1

1

The goal: Speed up Terminalizer's terminal cast (record)

I have a record of terminal created with Terminalizer. cast.yaml:

# The configurations that used for the recording, feel free to edit them
config:
  # do not touch it

# Records, feel free to edit them
records:
  - delay: 841
    content: "\e]1337;RemoteHost=kyb@kyb-nuc\a\e]1337;CurrentDir=/home/kyb/devel/git-rev-label\a\e]1337;ShellIntegrationVersion=7;shell=fish\a"
  - delay: 19
    content: "\e]1337;RemoteHost=kyb@kyb-nuc\a\e]1337;CurrentDir=/home/kyb/devel/git-rev-label\a\e]0;fish /home/kyb/devel/git-rev-label\a\e[30m\e(B\e[m"
  - delay: 6
    content: "\e[?2004h"
  - delay: 28
    content: "\e]0;fish /home/kyb/devel/git-rev-label\a\e[30m\e(B\e[m\e[2m⏎\e(B\e[m                                                                                                                                                        \r⏎ \r\e[K\e]133;D;0\a\e]133;A\a\e[44m\e[30m ~/d/git-rev-label \e[42m\e[34m \e[42m\e[30m demo  \e[30m\e(B\e[m\e[32m \e[30m\e(B\e[m\e]133;B\a\e[K"
  - delay: 1202
    content: "#\b\e[38;2;231;197;71m#\e[30m\e(B\e[m"
  - delay: 134
    content: "\e[38;2;231;197;71m#\e[30m\e(B\e[m"
  - delay: 489
    content: "\e[38;2;231;197;71m \e[30m\e(B\e[m"
  - delay: 318

I want to speed up payback without passing --speed-factor to terminalizer play. To do so delays should be decreased.

So, I need to create yq-expression to make delays lower

.records.delay=.records.delay/3

but this expression won't work. Please help to write proper one.

Carl answered 23/8, 2019 at 12:53 Comment(3)
yq will strip your comments. If that is an issue use Python. You can use a small program or a oneliner: echo -e "import sys, ruamel.yaml\nyaml = ruamel.yaml.YAML()\nyaml.width=4096\ndata = yaml.load(open('cast.yaml'))\nfor x in data['records']:\n x['delay'] /= 3\nyaml.dump(data, sys.stdout)\n" | python Acetometer
That is why i don't like python. It is cumbersome for such a simple task. I know the way to write a sed expression much shorter.Carl
Only if your YAML file is a tiny subset of what YAML allows you can get away with non-parsing and use sed/awk/etc. In all non-trivial YAML data, that is not true.Acetometer
H
1

.records is an array, so you could use this filter:

.records |= map(.delay /= 3)

Or you might prefer:

.records[].delay |= (. /= 3)
Hierarchize answered 23/8, 2019 at 13:1 Comment(3)
super. Thank you.Carl
How to prevent jq removing comments?Carl
@Carl - Regarding YAML comments - it might be worthwhile asking the developer of yq about that, or perhaps asking a separate SO question. So far as jq is concerned: jq only handles JSON, so any "comments" would have to be encoded in the JSON.Hierarchize

© 2022 - 2024 — McMap. All rights reserved.