Skip to content

Commit f0f0764

Browse files
authored
feat: don't show quotes on default macro params (#1487)
Signed-off-by: Pedro Lamas <[email protected]>
1 parent 8ac0297 commit f0f0764

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/components/widgets/macros/MacroBtn.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,29 @@ export default class MacroBtn extends Mixins(StateMixin) {
118118
*/
119119
get runCommand () {
120120
const command = this.macro.name.toUpperCase()
121-
const paramSeparator = this.isMacroForGcodeCommand
122-
? ''
123-
: '='
121+
const isMacroForGcodeCommand = this.isMacroForGcodeCommand
124122
125123
if (this.params) {
126124
const params = this.isMacroWithRawParam
127125
? this.params.message.value.toString()
128126
: Object.entries(this.params)
129-
.map(([key, param]) => `${key.toUpperCase()}${paramSeparator}${param.value}`)
127+
.map(([key, param]) => {
128+
const value = param.value.toString()
129+
130+
if (!value) {
131+
return null
132+
}
133+
134+
const valueDelimiter = value.includes(' ')
135+
? '"'
136+
: ''
137+
const paramSeparator = isMacroForGcodeCommand && !valueDelimiter
138+
? ''
139+
: '='
140+
141+
return `${key.toUpperCase()}${paramSeparator}${valueDelimiter}${value}${valueDelimiter}`
142+
})
143+
.filter(x => x != null)
130144
.join(' ')
131145
132146
if (params) {

src/util/__tests__/gcode-macro-params.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@ describe('gcodeMacroParamDefault', () => {
1515
it.each([
1616
[' | default(0, true) | float', '0'],
1717
[' | int | default(0.005, true) | float', '0.005'],
18-
[' | default("PLA", true)', '"PLA"'],
19-
[' | default("PLA")', '"PLA"'],
18+
[' | default("PLA", true)', 'PLA'],
19+
[' | default("PLA")', 'PLA'],
2020
['| int | default(0.2)', '0.2'],
2121
['| int | default(-0.2)', '-0.2'],
2222
['| int | default( -0.2 )', '-0.2'],
2323
['| int | default( - 0.2 )', ''],
2424
['|default(printer)|int', ''],
2525
['|default(-somevar)|int', ''],
2626
['|default(printer.configfile.settings.printer.max_velocity)|int', ''],
27-
['|default("this, \\"works,\\"" , true)|trim', '"this, \\"works,\\""'],
28-
['|default(",\\"fine,\\"",true)|trim', '",\\"fine,\\""'],
29-
['|default(\',2\',true)|trim', '\',2\'']
27+
['|default("this, \\"works,\\"" , true)|trim', 'this, \\"works,\\"'],
28+
['|default(",\\"fine,\\"",true)|trim', ',\\"fine,\\"']
3029
])('Expects param default of "%s" to be %s', (param, expected) => {
3130
expect(gcodeMacroParamDefault(param)).toBe(expected)
3231
})

src/util/gcode-macro-params.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ const defaultValueRegExp = /\|\s*default\s*\(\s*((["'])(?:\\\2|.)*?\2|-?\d[^,)]*
44
export const gcodeMacroParamDefault = (param: string) => {
55
const valueMatch = defaultValueRegExp.exec(param)
66

7-
return ((valueMatch && valueMatch[1]) || '').trim()
7+
if (!valueMatch) {
8+
return ''
9+
}
10+
11+
const value = (valueMatch[1] || '').trim()
12+
13+
return valueMatch[2]
14+
? value.substring(1, value.length - 1)
15+
: value
816
}
917

1018
const gcodeMacroParams = (gcode: string) => {

0 commit comments

Comments
 (0)