aboutsummaryrefslogtreecommitdiff
path: root/src/background/controllers/command.js
blob: 9ab1054429d29c293418f3e8df91240d2ce150ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import CompletionsInteractor from '../usecases/completions';
import CommandInteractor from '../usecases/command';
import Completions from '../domains/completions';

const trimStart = (str) => {
  // NOTE String.trimStart is available on Firefox 61
  return str.replace(/^\s+/, '');
};

export default class CommandController {
  constructor() {
    this.completionsInteractor = new CompletionsInteractor();
    this.commandIndicator = new CommandInteractor();
  }

  getCompletions(line) {
    let trimmed = trimStart(line);
    let words = trimmed.split(/ +/);
    let name = words[0];
    if (words.length === 1) {
      return this.completionsInteractor.queryConsoleCommand(name);
    }
    let keywords = trimStart(trimmed.slice(name.length));
    switch (words[0]) {
    case 'o':
    case 'open':
    case 't':
    case 'tabopen':
    case 'w':
    case 'winopen':
      return this.completionsInteractor.queryOpen(name, keywords);
    case 'b':
    case 'buffer':
      return this.completionsInteractor.queryBuffer(name, keywords);
    case 'bd':
    case 'bdel':
    case 'bdelete':
    case 'bdeletes':
      return this.completionsInteractor.queryBdelete(name, keywords);
    case 'bd!':
    case 'bdel!':
    case 'bdelete!':
    case 'bdeletes!':
      return this.completionsInteractor.queryBdeleteForce(name, keywords);
    case 'set':
      return this.completionsInteractor.querySet(name, keywords);
    }
    return Promise.resolve(Completions.empty());
  }

  // eslint-disable-next-line complexity
  exec(line) {
    let trimmed = trimStart(line);
    let words = trimmed.split(/ +/);
    let name = words[0];
    if (words[0].length === 0) {
      return Promise.resolve();
    }

    let keywords = trimStart(trimmed.slice(name.length));
    switch (words[0]) {
    case 'o':
    case 'open':
      return this.commandIndicator.open(keywords);
    case 't':
    case 'tabopen':
      return this.commandIndicator.tabopen(keywords);
    case 'w':
    case 'winopen':
      return this.commandIndicator.winopen(keywords);
    case 'b':
    case 'buffer':
      return this.commandIndicator.buffer(keywords);
    case 'bd':
    case 'bdel':
    case 'bdelete':
      return this.commandIndicator.bdelete(false, keywords);
    case 'bd!':
    case 'bdel!':
    case 'bdelete!':
      return this.commandIndicator.bdelete(true, keywords);
    case 'bdeletes':
      return this.commandIndicator.bdeletes(false, keywords);
    case 'bdeletes!':
      return this.commandIndicator.bdeletes(true, keywords);
    case 'addbookmark':
      return this.commandIndicator.addbookmark(keywords);
    case 'q':
    case 'quit':
      return this.commandIndicator.quit();
    case 'qa':
    case 'quitall':
      return this.commandIndicator.quitAll();
    case 'set':
      return this.commandIndicator.set(keywords);
    }
    throw new Error(words[0] + ' command is not defined');
  }
}