Skip to content

Commit

Permalink
feat: don't show quotes on default macro params (#1487)
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas authored Aug 29, 2024
1 parent 8ac0297 commit f0f0764
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
22 changes: 18 additions & 4 deletions src/components/widgets/macros/MacroBtn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,29 @@ export default class MacroBtn extends Mixins(StateMixin) {
*/
get runCommand () {
const command = this.macro.name.toUpperCase()
const paramSeparator = this.isMacroForGcodeCommand
? ''
: '='
const isMacroForGcodeCommand = this.isMacroForGcodeCommand
if (this.params) {
const params = this.isMacroWithRawParam
? this.params.message.value.toString()
: Object.entries(this.params)
.map(([key, param]) => `${key.toUpperCase()}${paramSeparator}${param.value}`)
.map(([key, param]) => {
const value = param.value.toString()
if (!value) {
return null
}
const valueDelimiter = value.includes(' ')
? '"'
: ''
const paramSeparator = isMacroForGcodeCommand && !valueDelimiter
? ''
: '='
return `${key.toUpperCase()}${paramSeparator}${valueDelimiter}${value}${valueDelimiter}`
})
.filter(x => x != null)
.join(' ')
if (params) {
Expand Down
9 changes: 4 additions & 5 deletions src/util/__tests__/gcode-macro-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ describe('gcodeMacroParamDefault', () => {
it.each([
[' | default(0, true) | float', '0'],
[' | int | default(0.005, true) | float', '0.005'],
[' | default("PLA", true)', '"PLA"'],
[' | default("PLA")', '"PLA"'],
[' | default("PLA", true)', 'PLA'],
[' | default("PLA")', 'PLA'],
['| int | default(0.2)', '0.2'],
['| int | default(-0.2)', '-0.2'],
['| int | default( -0.2 )', '-0.2'],
['| int | default( - 0.2 )', ''],
['|default(printer)|int', ''],
['|default(-somevar)|int', ''],
['|default(printer.configfile.settings.printer.max_velocity)|int', ''],
['|default("this, \\"works,\\"" , true)|trim', '"this, \\"works,\\""'],
['|default(",\\"fine,\\"",true)|trim', '",\\"fine,\\""'],
['|default(\',2\',true)|trim', '\',2\'']
['|default("this, \\"works,\\"" , true)|trim', 'this, \\"works,\\"'],
['|default(",\\"fine,\\"",true)|trim', ',\\"fine,\\"']
])('Expects param default of "%s" to be %s', (param, expected) => {
expect(gcodeMacroParamDefault(param)).toBe(expected)
})
Expand Down
10 changes: 9 additions & 1 deletion src/util/gcode-macro-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ const defaultValueRegExp = /\|\s*default\s*\(\s*((["'])(?:\\\2|.)*?\2|-?\d[^,)]*
export const gcodeMacroParamDefault = (param: string) => {
const valueMatch = defaultValueRegExp.exec(param)

return ((valueMatch && valueMatch[1]) || '').trim()
if (!valueMatch) {
return ''
}

const value = (valueMatch[1] || '').trim()

return valueMatch[2]
? value.substring(1, value.length - 1)
: value
}

const gcodeMacroParams = (gcode: string) => {
Expand Down

0 comments on commit f0f0764

Please sign in to comment.