aboutsummaryrefslogtreecommitdiff
path: root/src/CommandParser.cpp
blob: 2b9b42236e87760a16898c91f038ad98070d297f (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
#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 == "tn" || cmd == "tabnext")
    return TabNext;
  if (cmd == "tp" || cmd == "tabprev")
    return TabPrev;
  if (cmd == "tabs")
    return TabSelect;

  return Noop;
}