aboutsummaryrefslogtreecommitdiff
path: root/src/CommandParser.cpp
blob: 8f83575146718bb4edf2c41486ee0657ed67e162 (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
#include <QtCore/qnamespace.h>
#include <QtCore>

#include "CommandParser.hpp"

CommandParser::CommandParser() {}

Cmd CommandParser::parse(QString input) {
  // TODO: simplify this to only parse the command
  auto parts = input.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);

  if (parts.isEmpty())
    return {.command = Noop, .argsString = "", .rawInput = input};

  auto cmdStr = parts.first();
  auto cmd = toCommandType(cmdStr);
  auto rawArgs = input.slice(cmdStr.length()).trimmed();

  return {.command = cmd, .argsString = rawArgs, .rawInput = input};
}

CommandType CommandParser::toCommandType(QString cmd) {
  if (cmd == "lua")
    return LuaEval;
  if (cmd == "open")
    return Open;
  if (cmd == "tabopen")
    return TabOpen;
  if (cmd == "bn" || cmd == "bnext")
    return TabNext;
  if (cmd == "bp" || cmd == "bprevious")
    return TabPrev;

  return Noop;
}