initial menu design, simpler patch serialization
This commit is contained in:
19
lib/snippets/database/menu/MainMenu.ts
Normal file
19
lib/snippets/database/menu/MainMenu.ts
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (C) SapphireCode - All Rights Reserved
|
||||
* This file is part of Snippeteer which is released under BSD-3-Clause.
|
||||
* See file 'LICENSE' for full license details.
|
||||
* Created by Timo Hocker <timo@scode.ovh>, June 2020
|
||||
*/
|
||||
|
||||
import { Database } from '../classes/Database';
|
||||
import { Menu } from './Menu';
|
||||
|
||||
export class MainMenu extends Menu {
|
||||
public constructor (db: Database) {
|
||||
super ('Snippeteer Database');
|
||||
this.register_option ('quit', () => { /* noop */ });
|
||||
this.register_option ('create table', () => {
|
||||
console.log ('table');
|
||||
});
|
||||
}
|
||||
}
|
35
lib/snippets/database/menu/Menu.ts
Normal file
35
lib/snippets/database/menu/Menu.ts
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) SapphireCode - All Rights Reserved
|
||||
* This file is part of Snippeteer which is released under BSD-3-Clause.
|
||||
* See file 'LICENSE' for full license details.
|
||||
* Created by Timo Hocker <timo@scode.ovh>, June 2020
|
||||
*/
|
||||
|
||||
import { StringOption } from '@sapphirecode/console-app';
|
||||
|
||||
export class Menu {
|
||||
private _options: Record<string, ()=>void|Promise<void>> = {};
|
||||
private _title: string;
|
||||
|
||||
public constructor (title: string) {
|
||||
this._title = title;
|
||||
}
|
||||
|
||||
public async run (): Promise<void> {
|
||||
const options = Object.keys (this._options);
|
||||
const selected = await (new StringOption ({
|
||||
name: 'menu_select',
|
||||
message: this._title,
|
||||
sources: { console: false },
|
||||
exit_on_interrupt: true,
|
||||
preset: options
|
||||
}))
|
||||
.parse ();
|
||||
if (typeof this._options[selected] !== 'undefined')
|
||||
await new Promise (this._options[selected]);
|
||||
}
|
||||
|
||||
public register_option (name:string, callback:()=>void|Promise<void>): void {
|
||||
this._options[name] = callback;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user