Skip to content

Commit

Permalink
Merge pull request #996 from LeGend077/wiki-3
Browse files Browse the repository at this point in the history
Add more known string formats in json ui
  • Loading branch information
QuazChick authored Feb 9, 2025
2 parents 657dbf9 + 7176e90 commit 50d608b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/json-ui/json-ui-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Data Binding allows to bind hardcoded values/variables to an element property.

| Property Name | Type | Default Value | Description |
| ------------------------- | :--------------------: | ------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| grid_dimensions | Vector [rows, columns] | | Number of columns and rows the grid has |
| grid_dimensions | Vector [columns, rows] | | Number of columns and rows the grid has |
| maximum_grid_items | int | | Maximum number of items the grid will generate |
| grid_dimension_binding | string | | Binding name for grid dimensions |
| grid_rescaling_type | enum | | Grid rescaling orientation. <br> Possible values: <br> `vertical` <br> `horizontal` <br> `none` |
Expand Down
35 changes: 9 additions & 26 deletions docs/json-ui/json-ui-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,32 +767,15 @@ Like before, here's a more complicated example of conditional rendering with bin

## String Formatting

You can get specific part of a string by using `%.#s` format where `#` is a number by multiplying it to a string. An example:

```json
{
"label_element": {
"type": "label",
"text": "#text",
"layer": 2,
"bindings": [
{
"binding_type": "global",
"binding_name": "#hud_title_text_string"
},
{
"binding_type": "view",
"source_property_name": "('%.3s' * #hud_title_text_string)",
"target_property_name": "#text"
}
]
}
}
```

In the above example we are getting the first 3 characters of the title text. So if the title text is `abcdefghi`, the label will only have `abc` in it. Another example is where we have a variable: `"$var": "abcdefghijklmn"`,
`'%.5s' * $var` this will return abcde.
`$var - ('%.7s' * $var)` will return `hijklm`.
There are several format specifiers like formats that allows us to access or manipulate strings in JSON UI. They work by multiplying them with a string in bindings and variables.

Let's assume we have a variable `"$var": "abcdefghijklmn"`. Here are the known formats:
- `'%.ns'` can be used to get first n characters of a string. For example: `('%.7s' * $var)` will return `abcdefg`.
- `'%0ns'` returns the string if there are n or greater than n characters in the string otherwise returns 0 or false. For example: `('%04s' * $var)` will return `abcdefghijklmn` while `('%015s' * $var)` will return `0` or `false`.
- `'%n.xs'` returns x characters of a string and adds padding of empty space at start till string length becomes n. For example: `('%7.4s' * $var)` will return ` abcd`.
- `'%-n.xs'` returns x characters of a string and adds padding of empty space at end till string length becomes n. For example: `('%-7.4s' * $var)` will return `abcd `.
- `'%ns'` seems to add empty space at the start. For example: `('%15s' * $var)` will return ` abcdefghijklmn`. If n is smaller or equal to string length, it just returns the string.
- `'%-ns'` seems to add empty space at the end. For example: `('%-15s' * $var)` will return `abcdefghijklmn `. If n is smaller or equal to string length, it just returns the string.

Remember that the usage of this format is limited.

Expand Down

0 comments on commit 50d608b

Please sign in to comment.