mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-05 12:10:37 +00:00
feat: Add --yaml-compact-seq-indent / -c flag for compact sequence indentation (#2583)
Adds a new CLI flag that enables compact sequence indentation where '- ' is
considered part of the indentation. This leverages the CompactSeqIndent()
method from the underlying go.yaml.in/yaml/v4 library.
Example output with --yaml-compact-seq-indent:
parent:
items:
- one
- two
Instead of the default:
parent:
items:
- one
- two
Closes #1841
This commit is contained in:
parent
c4f4e6d416
commit
78192a915b
@ -203,6 +203,7 @@ yq -P -oy sample.json
|
|||||||
}
|
}
|
||||||
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.")
|
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.LeadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression.")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.FixMergeAnchorToSpec, "yaml-fix-merge-anchor-to-spec", "", false, "Fix merge anchor to match YAML spec. Will default to true in late 2025")
|
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.FixMergeAnchorToSpec, "yaml-fix-merge-anchor-to-spec", "", false, "Fix merge anchor to match YAML spec. Will default to true in late 2025")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&yqlib.ConfiguredYamlPreferences.CompactSequenceIndent, "yaml-compact-seq-indent", "c", false, "Use compact sequence indentation where '- ' is considered part of the indentation.")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter. The necessary directories will be created.")
|
rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter. The necessary directories will be created.")
|
||||||
if err = rootCmd.RegisterFlagCompletionFunc("split-exp", cobra.NoFileCompletions); err != nil {
|
if err = rootCmd.RegisterFlagCompletionFunc("split-exp", cobra.NoFileCompletions); err != nil {
|
||||||
|
|||||||
@ -52,6 +52,9 @@ func (ye *yamlEncoder) Encode(writer io.Writer, node *CandidateNode) error {
|
|||||||
var encoder = yaml.NewEncoder(destination)
|
var encoder = yaml.NewEncoder(destination)
|
||||||
|
|
||||||
encoder.SetIndent(ye.prefs.Indent)
|
encoder.SetIndent(ye.prefs.Indent)
|
||||||
|
if ye.prefs.CompactSequenceIndent {
|
||||||
|
encoder.CompactSeqIndent()
|
||||||
|
}
|
||||||
|
|
||||||
target, err := node.MarshalYAML()
|
target, err := node.MarshalYAML()
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@ type YamlPreferences struct {
|
|||||||
UnwrapScalar bool
|
UnwrapScalar bool
|
||||||
EvaluateTogether bool
|
EvaluateTogether bool
|
||||||
FixMergeAnchorToSpec bool
|
FixMergeAnchorToSpec bool
|
||||||
|
CompactSequenceIndent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultYamlPreferences() YamlPreferences {
|
func NewDefaultYamlPreferences() YamlPreferences {
|
||||||
@ -19,6 +20,7 @@ func NewDefaultYamlPreferences() YamlPreferences {
|
|||||||
UnwrapScalar: true,
|
UnwrapScalar: true,
|
||||||
EvaluateTogether: false,
|
EvaluateTogether: false,
|
||||||
FixMergeAnchorToSpec: false,
|
FixMergeAnchorToSpec: false,
|
||||||
|
CompactSequenceIndent: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,6 +33,7 @@ func (p *YamlPreferences) Copy() YamlPreferences {
|
|||||||
UnwrapScalar: p.UnwrapScalar,
|
UnwrapScalar: p.UnwrapScalar,
|
||||||
EvaluateTogether: p.EvaluateTogether,
|
EvaluateTogether: p.EvaluateTogether,
|
||||||
FixMergeAnchorToSpec: p.FixMergeAnchorToSpec,
|
FixMergeAnchorToSpec: p.FixMergeAnchorToSpec,
|
||||||
|
CompactSequenceIndent: p.CompactSequenceIndent,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user