added support for format pattern %g

This commit is contained in:
Timon Ringwald 2022-04-18 20:59:52 +02:00
parent ed24c7842d
commit a2f203cd20
2 changed files with 7 additions and 7 deletions

View File

@ -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

View File

@ -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)