added support for format pattern %g
This commit is contained in:
parent
ed24c7842d
commit
a2f203cd20
12
README.md
12
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.
|
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).
|
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
|
### Mutators
|
||||||
|
|
||||||
@ -234,15 +234,15 @@ Input:
|
|||||||
|
|
||||||
Command:
|
Command:
|
||||||
```sh
|
```sh
|
||||||
format -i '(-?\d+) (-?\d+(?:.\d+)?)' -o '{1} + {2} = {1:%.2f:+(2)}'
|
format -i '(-?\d+) (-?\d+(?:.\d+)?)' -o '{1} + {2} = {1:%g:+(2)}'
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
```
|
```
|
||||||
5 + 7 = 12.00
|
5 + 7 = 12
|
||||||
3 + 2 = 5.00
|
3 + 2 = 5
|
||||||
10 + 152 = 162.00
|
10 + 152 = 162
|
||||||
-15 + 3.7 = -11.30
|
-15 + 3.7 = -11.3
|
||||||
```
|
```
|
||||||
|
|
||||||
### Bulk renaming files
|
### Bulk renaming files
|
||||||
|
2
main.go
2
main.go
@ -118,7 +118,7 @@ func replaceVars(format string, vars ...string) string {
|
|||||||
value, _ := strconv.ParseInt(vars[varIndex], 10, 64)
|
value, _ := strconv.ParseInt(vars[varIndex], 10, 64)
|
||||||
mutate := numMut2func[int64](replacement[3])
|
mutate := numMut2func[int64](replacement[3])
|
||||||
format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, mutate(value, vars)), 1)
|
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)
|
value, _ := strconv.ParseFloat(vars[varIndex], 64)
|
||||||
mutate := numMut2func[float64](replacement[3])
|
mutate := numMut2func[float64](replacement[3])
|
||||||
format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, mutate(value, vars)), 1)
|
format = strings.Replace(format, rplStr, fmt.Sprintf(rplFmt, mutate(value, vars)), 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user