Improve cropping in asm component

This commit is contained in:
natsoni
2025-06-18 21:18:08 +02:00
parent 9c1025fa7c
commit 6c8ac6cd5c

View File

@@ -36,14 +36,35 @@ export class AsmComponent {
parseASM(): void {
let instructions = this.asm.split('OP_');
// trim instructions to a whole number of instructions with at most `crop` characters total
if (this.crop) {
if (this.crop && this.asm.length > this.crop) {
let chars = 0;
for (let i = 0; i < instructions.length; i++) {
chars += instructions[i].length + 3;
if (chars > this.crop) {
if (chars + instructions[i].length + 3 > this.crop) {
let croppedInstruction = instructions[i];
instructions = instructions.slice(0, i);
// add cropped instruction
let remainingChars = this.crop - chars;
let parts = croppedInstruction.split(' ');
// only render this instruction if there is space for the instruction name and a few args
if (remainingChars > parts[0].length + 10) {
remainingChars -= parts[0].length + 1;
for (let j = 1; j < parts.length; j++) {
const arg = parts[j];
if (remainingChars >= arg.length) {
remainingChars -= arg.length + 1;
} else {
// crop this argument
parts[j] = arg.slice(0, remainingChars);
// and remove all following arguments
parts = parts.slice(0, j + 1);
break;
}
}
instructions.push(`${parts.join(' ')}`);
}
break;
}
chars += instructions[i].length + 3;
}
}
this.instructions = instructions.filter(instruction => instruction.trim() !== '').map(instruction => {