Skip to content

Commit

Permalink
refact consoleApp
Browse files Browse the repository at this point in the history
  • Loading branch information
kuznya committed Aug 10, 2024
1 parent fc6103b commit 7a9b319
Showing 1 changed file with 43 additions and 44 deletions.
87 changes: 43 additions & 44 deletions js/consoleApp.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,62 @@
class cLog {
constructor (area) {
this.area = area;
}
constructor(area) {
this.area = area;
}

topprint(s) {
const v = this.area.value;
this.area.value = v ? `${s}\n${v}` : s;
}
topprint(s) {
const v = this.area.value;
this.area.value = v ? `${s}\n${v}` : s;
}

print(s) {
const v = this.area.value;
this.area.value = v ? `${v}\n${s}` : s;
}
print(s) {
const v = this.area.value;
this.area.value = v ? `${v}\n${s}` : s;
}

putlines(lines) {
this.area.value = lines.join('\n')
}
putlines(lines) {
this.area.value = lines.join('\n')
}

clear() {
this.area.value = '';
}
clear() {
this.area.value = '';
}
}
const write = (s) => { document.write(s); };
const elem = (id) => document.getElementById(id);

const write = (s) => document.write(s);
const elem = (id) => document.getElementById(id);

class ConsoleApp {
constructor(renderHeader) {
this.logAreaId = 'logArea';
this.renderHeader = renderHeader ? renderHeader : () => {};
this.drawApp();
this.log = new cLog(elem(this.logAreaId));
}
constructor(renderHeader) {
this.logAreaId = 'logArea';
this.renderHeader = renderHeader ? renderHeader : () => {};
this.drawApp();
this.log = new cLog(elem(this.logAreaId));
}

drawApp () {
write('<div id="app">\n');
write(this.renderHeader());
write(`<textarea id="${this.logAreaId}" readonly></textarea>\n`);
write('</div>\n');
}
drawApp() {
write('<div id="app">\n');
write(this.renderHeader());
write(`<textarea id="${this.logAreaId}" readonly></textarea>\n`);
write('</div>\n');
}

print(s) {
this.log.print(s)
}
print(s) {
this.log.print(s)
}


formatNumber (value, places) {
const len = value.toString().length;
const spaces = Math.max(0, places - len);
return `${' '.repeat(spaces)}${value}`;
};
formatNumber(value, places) {
const len = value.toString().length;
const spaces = Math.max(0, places - len);
return `${' '.repeat(spaces)}${value}`;
};

formatDict (dict) {
formatDict(dict) {
const aa = [];
for (const k in dict) {
if (!dict.hasOwnProperty(k)) {
continue;
}
if (!dict.hasOwnProperty(k)) { continue; }
const value = dict[k];
let repr = typeof value === 'string' ? `"${value}"` : value;
const repr = typeof value === 'string' ? `"${value}"` : value;
aa.push(`${k}: ${repr}`);
}
return `{${aa.join(', ')}}`;
Expand Down

0 comments on commit 7a9b319

Please sign in to comment.