From a2f203cd2084d4fab786dae62086de9e79ef4919 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 18 Apr 2022 20:59:52 +0200 Subject: [PATCH] added support for format pattern %g --- README.md | 12 ++++++------ main.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8227086..64fdd21 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ You can use them using this syntax: `{1:%d}`. It will parse the first capturing The same method can also be applied to `%f` to further format floating point values. See a full list of formatting options at [Go's fmt spec](https://pkg.go.dev/fmt). -Currently only `%s`, `%d` and `%f` are supported though. +Currently only `%s`, `%d`, `%f` and `%g` are supported though. ### Mutators @@ -234,15 +234,15 @@ Input: Command: ```sh -format -i '(-?\d+) (-?\d+(?:.\d+)?)' -o '{1} + {2} = {1:%.2f:+(2)}' +format -i '(-?\d+) (-?\d+(?:.\d+)?)' -o '{1} + {2} = {1:%g:+(2)}' ``` Output: ``` -5 + 7 = 12.00 -3 + 2 = 5.00 -10 + 152 = 162.00 --15 + 3.7 = -11.30 +5 + 7 = 12 +3 + 2 = 5 +10 + 152 = 162 +-15 + 3.7 = -11.3 ``` ### Bulk renaming files diff --git a/main.go b/main.go index 4c29cbf..2355efd 100644 --- a/main.go +++ b/main.go @@ -118,7 +118,7 @@ func replaceVars(format string, vars ...string) string { value, _ := strconv.ParseInt(vars[varIndex], 10, 64) mutate := numMut2func[int64](replacement[3]) format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, mutate(value, vars)), 1) - } else if strings.HasSuffix(rplFmt, "f") { // replace floats + } else if strings.HasSuffix(rplFmt, "f") || strings.HasSuffix(rplFmt, "g") { // replace floats value, _ := strconv.ParseFloat(vars[varIndex], 64) mutate := numMut2func[float64](replacement[3]) format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, mutate(value, vars)), 1)