Skip to content

Commit

Permalink
Merge pull request #910 from intechstudio/feat/CharLimit
Browse files Browse the repository at this point in the history
Fixed char limit calculation
  • Loading branch information
elsoazemelet authored Jan 20, 2025
2 parents c3b8168 + a08c828 commit 4a86124
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion playwright-tests/tests/actionsOperation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,6 @@ test.describe("Character limit", () => {
await configPage.removeAllActions();
await configPage.addAndEditCodeBlock(text);
await configPage.commitCode();
await expect(configPage.characterCount).toContainText("32");
await expect(configPage.characterCount).toContainText("23");
});
});
10 changes: 10 additions & 0 deletions src/renderer/lib/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { grid } from "@intechstudio/grid-protocol";

export class Grid {
static toFirstCase(value: string) {
return value[0].toUpperCase() + value.slice(1, value.length);
Expand Down Expand Up @@ -55,3 +57,11 @@ export class Grid {
return closestEvent !== Infinity ? closestEvent : 0;
}
}
export namespace Grid {
export namespace Protocol {
export const scriptStart = "<?lua ";
export const scriptEnd = " ?>";
export const maxScriptLength =
grid.getProperty("CONFIG_LENGTH") - scriptEnd.length - scriptStart.length;
}
}
4 changes: 2 additions & 2 deletions src/renderer/main/modals/Monaco.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
scriptLength = ($monaco_action.parent as GridEvent).toLua().length;
//Check the minified config length
if (scriptLength >= grid.getProperty("CONFIG_LENGTH")) {
if (scriptLength >= Grid.Protocol.maxScriptLength) {
throw new LengthError("Config limit reached.");
}
Expand Down Expand Up @@ -277,7 +277,7 @@
<span class:invisible={isDeleted($monaco_action)}
>{`Character Count: ${
typeof scriptLength === "undefined" ? "?" : scriptLength
}/${grid.getProperty("CONFIG_LENGTH") - 1} (max)`}</span
}/${Grid.Protocol.maxScriptLength - 1} (max)`}</span
>
</div>
</div>
Expand Down
11 changes: 4 additions & 7 deletions src/renderer/main/panels/DebugMonitor/DebugMonitor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import { Pane, Splitpanes } from "svelte-splitpanes";
import { MoltenPushButton, MoltenInput } from "@intechstudio/grid-uikit";
import { runtime_manager } from "../../../runtime/runtime-manager.store";
import { Grid } from "../../../lib/_utils";
let event: GridEvent;
Expand Down Expand Up @@ -181,9 +182,9 @@
<div class="text-white">
<span
class:text-error={configScriptLength >=
grid.getProperty("CONFIG_LENGTH")}
Grid.Protocol.maxScriptLength}
class:text-yellow-400={configScriptLength >
(grid.getProperty("CONFIG_LENGTH") / 3) * 2}
(Grid.Protocol.maxScriptLength / 3) * 2}
>{configScriptLength}
</span>
</div>
Expand All @@ -196,11 +197,7 @@
<MoltenInput bind:target={immediateCommand} />
<MoltenPushButton
click={() => {
runtime_manager.LUAExecImmediate(
0,
0,
"<?lua " + immediateCommand + " ?>"
);
runtime_manager.LUAExecImmediate(0, 0, immediateCommand);
}}
text="Immediate"
/>
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/main/panels/MidiMonitor/MidiMonitor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { MoltenPushButton, SvgIcon } from "@intechstudio/grid-uikit";
import { GridEvent } from "../../../runtime/runtime";
import { runtime_manager } from "../../../runtime/runtime-manager.store";
import { Grid } from "../../../lib/_utils";
let event: GridEvent;
Expand Down Expand Up @@ -453,10 +454,10 @@
<div class="flex flex-row">
<div class="pr-2">Char Count:</div>
<div
class={configScriptLength >= grid.getProperty("CONFIG_LENGTH")
class={configScriptLength >= Grid.Protocol.maxScriptLength
? "text-error"
: configScriptLength >=
(grid.getProperty("CONFIG_LENGTH") / 3) * 2
(Grid.Protocol.maxScriptLength / 3) * 2
? "text-yellow-400"
: "text-white"}
>
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/main/panels/configuration/ActionList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { grid } from "@intechstudio/grid-protocol";
import { shortcut } from "./../../_actions/shortcut.action";
import Options from "./components/Options.svelte";
import { Grid } from "../../../lib/_utils";
export let event: GridEvent;
Expand Down Expand Up @@ -101,7 +102,7 @@
<div class="flex flex-row gap-2">
<span class="text-gray-500 text-sm">Script length:</span>
<span data-testid="charCount" class="text-white text-sm">
{$event?.toLua().length ?? 0}/{grid.getProperty("CONFIG_LENGTH") - 1}
{$event?.toLua().length ?? 0}/{Grid.Protocol.maxScriptLength - 1}
</span>
</div>
</div>
Expand Down
12 changes: 7 additions & 5 deletions src/renderer/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,10 @@ export class GridAction extends RuntimeNode<ActionData> {
actionString = actionString.replace(/\s{2,10}/g, " ");
// remove lua opening and closing characters
// this function is used for both parsing full config (long complete lua) and individiual actions lua
if (actionString.startsWith("<?lua")) {
actionString = actionString.split("<?lua")[1].split("?>")[0];
if (actionString.startsWith(Grid.Protocol.scriptStart)) {
actionString = actionString
.split(Grid.Protocol.scriptStart)[1]
.split(Grid.Protocol.scriptEnd)[0];
}
// split by meta comments
configList = actionString.split(/(--\[\[@\w+(?:#|\w|\s)*\]\])/);
Expand Down Expand Up @@ -449,14 +451,14 @@ export class EventData extends NodeData {
}

public toLua(): string {
return `<?lua ${this.config
return `${this.config
.map((e) => e.toLua())
.join("")
.replace(/(\r\n|\n|\r)/gm, "")} ?>`;
.replace(/(\r\n|\n|\r)/gm, "")}`;
}

public getAvailableChars(): number {
return grid.getProperty("CONFIG_LENGTH") - this.toLua().length - 1;
return Grid.Protocol.maxScriptLength - this.toLua().length - 1;
}

public isStored() {
Expand Down
13 changes: 9 additions & 4 deletions src/renderer/serialport/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { logger } from "../runtime/runtime.store";
import { v4 as uuidv4 } from "uuid";
import { GridConnection } from "./serialport.js";
import { GridRuntime } from "../runtime/runtime.js";
import { Grid } from "../lib/_utils.js";

export namespace GridInstruction {
abstract class AbstractInstruction {
Expand Down Expand Up @@ -125,6 +126,8 @@ export namespace GridInstruction {
virtual: boolean = false
) {
super(virtual);
const actionString =
Grid.Protocol.scriptStart + config + Grid.Protocol.scriptEnd;
this.buffer_element = {
id: uuidv4(),
virtual: virtual,
Expand All @@ -142,8 +145,8 @@ export namespace GridInstruction {
PAGENUMBER: page,
ELEMENTNUMBER: element,
EVENTTYPE: event,
ACTIONLENGTH: config.length,
ACTIONSTRING: config,
ACTIONLENGTH: actionString.length,
ACTIONSTRING: actionString,
},
},
responseRequired: true,
Expand Down Expand Up @@ -183,6 +186,8 @@ export namespace GridInstruction {
virtual: boolean = false
) {
super(virtual);
const actionString =
Grid.Protocol.scriptStart + script + Grid.Protocol.scriptEnd;
this.buffer_element = {
id: uuidv4(),
virtual: virtual,
Expand All @@ -194,8 +199,8 @@ export namespace GridInstruction {
class_name: InstructionClassName.IMMEDIATE,
class_instr: InstructionClass.EXECUTE,
class_parameters: {
ACTIONLENGTH: script.length,
ACTIONSTRING: script,
ACTIONLENGTH: actionString.length,
ACTIONSTRING: actionString,
},
},
};
Expand Down

0 comments on commit 4a86124

Please sign in to comment.