Solutions for formatter/linter complaints about template syntax #2426
Replies: 4 comments 12 replies
-
Just to mention, you can also tinker with some backspaces. Example: $ chezmoi execute-template '# {{ "\b\bfelipe" }}'
felipe |
Beta Was this translation helpful? Give feedback.
-
Note that chezmoi does not implement its own template engine, but instead uses Go templates. The idea of having I do not think that replacers make much sense, from my perspective. |
Beta Was this translation helpful? Give feedback.
-
@twpayne I'm extremely hesitant to ask for help on something that is unfinished and isn't exactly my preferred solution, but I'm afraid I've hit my limit when it comes to Go. I'm attempting to throw together a quick proof of concept for configurable delims just to see if I like the workflow. I'm trying to implement it as a config option (for now, it's just based on how interpreters were implemented) and it works, but the delims aren't inherited when nesting and I'm not sure why. (This is by no means a complete solution)
|
Beta Was this translation helpful? Give feedback.
-
Thanks to @bradenhilton and @halostatue, you can now specify per-file template delimiters by including a template directive directly in the template, like:
See #2457 for the final implementation. |
Beta Was this translation helpful? Give feedback.
-
cc @felipecrs
This is a continuation of #1426 (comment), especially #1426 (reply in thread). I'm just going to ramble and see if anything sounds like a good idea. Please do offer your thoughts if you have any.
Have chezmoi create script/env vars to replace template actions with runtime conditionals
File preprocessors/directives
chezmoi:delims "# {{" "}}"
orchezmoi:replace "# {{" "{{"
.// chezmoi:delims "// {{" "}}"
or# chezmoi:replace ""# {{" "}}"
.delims
andreplace
/regexReplaceAll
being useful, possibly more as well. Maybe a method for forcing a particular encoding or line ending? Not sure..chezmoidelims.$FORMAT
fileCan be placed anywhere in the source state, and only acts upon templates within the source state.
Would have a structure similar to the following:
.local/share/chezmoi/home/.chezmoidelims.toml
:A delim can be omitted to use the default value.
delims
section in chezmoi configLargely similar to the
.chezmoidelims
file, but with a slightly different structure, perhaps something like:Prefixing actions with comments, then fixing up files with a
run_after_
scriptrun_
andmodify_
scripts would require editing the source state with a script, which is heavily discouraged.Some thoughts
Runtime env vars are a nice solution, but they are only useful for scripts and not config files. They also require extra caution. Imagine accidentally setting
PATH
to""
because you set it to a variable which has an improper value for whatever reason etc.Delims:
Seem easier to implement than replacers.
Safer, but not very flexible:
A left delim of
# {{
would result in something like the following multi-action one-liner1:# {{ if eq .chezmoi.os "windows" }}# {{ template "powershell/windows.ps1" }}# {{ end }}
Configurable delims could prevent collisions that some users experience with the default ones. An example of this is
{{{
and}}}
being popular fold markers in vim. Currently users need to replace them with{{ "{{{" }}
and{{ "}}}" }}
, which makes chezmoi happy, but breaks the folding capability if I recall correctly.Replacers:
Not sure how difficult they would be to implement.
More destructive, but also more flexible:
Replacing
# {{
with{{
would keep templates the same as they are currently (assuming a comment prefix to prevent the formatter/linter complaints)1:# {{ if eq .chezmoi.os "windows" }}{{ template "powershell/windows.ps1" }}{{ end }}
Given that replacers edit the template data, I think they should only appear in the template itself. A
.chezmoireplace.$FORMAT
file orreplace
config section would create too much separation in my opinion. It also makes any potential support more difficult, whereas I think user configured delims would be more obvious if a template is full of e.g.<<- if .is_headless ->> ... <<- end ->>
etc.The remaining comments in the template result can likely be reduced to just one by using an intermediate template file and passing its contents through a
replace
withinclude
. Not a great solution in my opinion.Footnotes
I am aware that the
if
could be moved into a more generalpowershell/profle.ps1
template, it was just a random example. ↩ ↩2Beta Was this translation helpful? Give feedback.
All reactions