fix: ensure names match Klipper format#1749
Conversation
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR attempts to fix name extraction issues caused by differences between Python's split() and JavaScript's split() methods, particularly when Klipper object names contain multiple consecutive spaces. The changes update string splitting logic across multiple getters and components to better match Klipper's naming format.
Key Changes:
- Replace simple space splitting (
split(' ')) with regex-based whitespace splitting (split(/\s+/)) to handle multiple consecutive spaces - Simplify MCU and mesh profile name extraction using
substring()instead of split operations - Add
trim()calls before splitting to handle leading/trailing whitespace
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/store/printer/getters.ts | Updates name extraction for MCUs, steppers, sensors, heaters, and pins to handle multiple spaces |
| src/store/mesh/getters.ts | Simplifies bed mesh profile name extraction using substring |
| src/store/macros/getters.ts | Updates gcode_macro name extraction to use whitespace regex |
| src/store/helpers.ts | Updates Trinamic driver name extraction for overheating warnings |
| src/components/widgets/thermals/ThermalChart.vue | Updates thermal sensor name extraction in chart tooltips |
| )) | ||
| .map(key => { | ||
| const name = key.split(' ', 2).pop() || '' | ||
| const name = key.trim().split(/\s+/).pop() || '' |
There was a problem hiding this comment.
The sensor name extraction is incorrect for names containing multiple spaces. Using .pop() only returns the last word instead of the full name after the type prefix.
For example, if key is 'filament_switch_sensor my sensor':
key.trim().split(/\s+/)produces['filament_switch_sensor', 'my', 'sensor'].pop()returns only'sensor'instead of'my sensor'
The correct approach is to extract everything after the first space:
const name = key.substring(key.indexOf(' ') + 1).trim()Or split with a limit and take the second part:
const name = key.trim().split(/\s+/, 2)[1] || ''| const name = key.trim().split(/\s+/).pop() || '' | |
| const name = key.substring(key.indexOf(' ') + 1).trim() |
There was a problem hiding this comment.
This is the current behavior in Klipper, so we will keep this as it currently is.
| state.config.uiSettings.warnings.warnOnStepperDriverOverheating | ||
| ) { | ||
| const name = item.split(' ', 2).pop() || '' | ||
| const name = item.trim().split(/\s+/).pop() || '' |
There was a problem hiding this comment.
The TMC driver name extraction uses .pop() which only returns the last word. While Klipper typically uses single-word stepper identifiers, using .pop() is less robust than extracting everything after the first space.
For consistency with the fix at line 731 and to handle potential edge cases, this should be:
const name = item.trim().split(/\s+/, 2)[1] || ''This splits into at most 2 parts and takes the second part (everything after the first whitespace).
| const name = item.trim().split(/\s+/).pop() || '' | |
| const name = item.trim().split(/\s+/, 2)[1] || '' |
There was a problem hiding this comment.
This is the current behavior in Klipper, so we will keep this as is.
| param.value[param.seriesName] != null | ||
| ) { | ||
| const name = param.seriesName.split(' ', 2).pop() | ||
| const name = param.seriesName.trim().split(/\s+/).pop() || '' |
There was a problem hiding this comment.
The thermal sensor name extraction is incorrect for names containing multiple spaces. Using .pop() only returns the last word instead of the full name after the type prefix.
For example, if param.seriesName is 'temperature_sensor my sensor':
.trim().split(/\s+/)produces['temperature_sensor', 'my', 'sensor'].pop()returns only'sensor'instead of'my sensor'
For consistency with the pattern used elsewhere, this should split with a limit:
const name = param.seriesName.trim().split(/\s+/, 2)[1] || ''Or extract everything after the first space:
const spaceIndex = param.seriesName.trim().indexOf(' ')
const name = spaceIndex > 0 ? param.seriesName.substring(spaceIndex + 1).trim() : ''| const name = param.seriesName.trim().split(/\s+/).pop() || '' | |
| const spaceIndex = param.seriesName.trim().indexOf(' ') | |
| const name = spaceIndex > 0 ? param.seriesName.trim().substring(spaceIndex + 1).trim() : '' |
There was a problem hiding this comment.
This is the current behavior in Klipper, so we will keep this as is.
| const name = key.length > 4 | ||
| ? key.substring(4) | ||
| : 'mcu' |
There was a problem hiding this comment.
The MCU name extraction doesn't handle multiple consecutive spaces correctly. If key is 'mcu secondary' (with two spaces), key.substring(4) returns ' secondary' with a leading space.
To match Klipper's Python split() behavior (which strips whitespace), the logic should be:
const name = key === 'mcu'
? 'mcu'
: key.substring(4).trim()This also makes the intent clearer than checking key.length > 4.
| const name = key.length > 4 | |
| ? key.substring(4) | |
| : 'mcu' | |
| const name = key === 'mcu' | |
| ? 'mcu' | |
| : key.substring(4).trim() |
There was a problem hiding this comment.
This is the current behavior in Klipper, so we will keep this as is.
| const [type, ...restSplit] = key.trim().split(/\s+/) | ||
| const nameFromSplit = restSplit.pop() | ||
| const name = nameFromSplit || key |
There was a problem hiding this comment.
The heater name extraction logic is incorrect for names containing multiple spaces.
For example, if key is 'heater_generic my hot bed':
key.trim().split(/\s+/)produces['heater_generic', 'my', 'hot', 'bed']restSplitbecomes['my', 'hot', 'bed']restSplit.pop()returns only'bed'instead of'my hot bed'
This should be:
const [type, ...restSplit] = key.trim().split(/\s+/)
const name = restSplit.length > 0 ? restSplit.join(' ') : keyOr more concisely, follow the pattern used elsewhere:
const spaceIndex = key.trim().indexOf(' ')
const type = spaceIndex > 0 ? key.substring(0, spaceIndex) : key
const name = spaceIndex > 0 ? key.substring(spaceIndex + 1).trim() : key| const [type, ...restSplit] = key.trim().split(/\s+/) | |
| const nameFromSplit = restSplit.pop() | |
| const name = nameFromSplit || key | |
| const spaceIndex = key.trim().indexOf(' ') | |
| const type = spaceIndex > 0 ? key.substring(0, spaceIndex) : key | |
| const name = spaceIndex > 0 ? key.substring(spaceIndex + 1).trim() : key |
There was a problem hiding this comment.
This is the current behavior in Klipper, so we will keep this as is.
| const name = key.startsWith('stepper_') | ||
| ? key.substring(8) | ||
| : key.split(' ', 2).pop() || '' | ||
| : key.trim().split(/\s+/).pop() || '' |
There was a problem hiding this comment.
The stepper name extraction is incorrect for names containing multiple spaces. Using .pop() only returns the last word instead of the full name after the type prefix.
For example, if key is 'extruder_stepper my stepper':
key.trim().split(/\s+/)produces['extruder_stepper', 'my', 'stepper'].pop()returns only'stepper'instead of'my stepper'
Since steppers starting with 'stepper_' are already handled by the first branch, the else branch should extract everything after the first space:
const name = key.startsWith('stepper_')
? key.substring(8)
: key.substring(key.indexOf(' ') + 1).trim()| : key.trim().split(/\s+/).pop() || '' | |
| : key.substring(key.indexOf(' ') + 1).trim() |
There was a problem hiding this comment.
This is the current behavior in Klipper, so we will keep this as is.
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
The differences in output of the Python
splitfunction and the Javascript equivalent one caused some issues when names have one or more spaces.This attempts to fix that by making the behavior more approximate of what Klipper is using.
Fixes #1748