Angular web app for entering Techblox commands
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.6KB

  1. import { Component, ElementRef, ViewChild } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { FormControl } from '@angular/forms';
  4. @Component({
  5. selector: 'app-root',
  6. templateUrl: './app.component.html',
  7. styleUrls: ['./app.component.scss']
  8. })
  9. export class AppComponent {
  10. title = 'TBConsoleClient';
  11. logMessages = '';
  12. commandControl: FormControl = new FormControl('');
  13. commands: { command: string, line: string }[] = [];
  14. displayedCommands: { command: string, line: string }[] = [];
  15. commandHistory: { commands: string[], index: number } = {commands: [], index: 0};
  16. @ViewChild('logMessagesContainer') logMessagesView: ElementRef;
  17. constructor(private http: HttpClient) {
  18. this.getCommandList();
  19. this.commandControl.valueChanges.subscribe(commandText => {
  20. this.displayedCommands = this.commands.filter(cmd => cmd.command.toLowerCase().startsWith(commandText.toLowerCase()));
  21. })
  22. }
  23. async sendCommand() {
  24. try {
  25. const command = this.commandControl.value;
  26. this.commandControl.setValue('');
  27. this.addCommandToHistory(command);
  28. const res = await this.http.post('http://localhost:8019/command', command, {responseType: 'text'}).toPromise();
  29. this.logMessages += res + "\n";
  30. } catch (e) {
  31. if (e.status == 0)
  32. this.logMessages += "Failed to contact mod! Make sure it is running and listening.\n";
  33. else
  34. this.logMessages += e.message + "\n";
  35. }
  36. await this.wait(50);
  37. this.logMessagesView.nativeElement.scrollTop = this.logMessagesView.nativeElement.scrollHeight;
  38. }
  39. async getCommandList() {
  40. const res = await this.http.post('http://localhost:8019/commands', '', {responseType: 'text'}).toPromise();
  41. this.commands = res.trim().split('\n').map(cmd => ({command: cmd.split(' - ')[0], line: cmd}));
  42. }
  43. wait(ms) {
  44. return new Promise(resolve => setTimeout(resolve, ms));
  45. }
  46. previousCommand() {
  47. const hist = this.commandHistory;
  48. if (hist.index <= 0) return;
  49. hist.index--;
  50. this.commandControl.setValue(hist.commands[hist.index]);
  51. }
  52. nextCommand() {
  53. const hist = this.commandHistory;
  54. if (hist.index >= hist.commands.length - 1) return;
  55. hist.index++;
  56. this.commandControl.setValue(hist.commands[hist.index]);
  57. }
  58. addCommandToHistory(command: string) {
  59. const hist = this.commandHistory;
  60. if (hist.commands.length == 0) hist.commands.push('');
  61. hist.commands[hist.commands.length - 1] = command;
  62. hist.commands.push(''); //Empty to enter new commands
  63. if (hist.commands.length > 50) hist.commands.shift();
  64. hist.index = hist.commands.length - 1;
  65. }
  66. }