shithub: hell

ref: b985a98b4941ad9b86dbdf43ac0bf844728c7ea7
dir: /commands.go/

View raw version
package main

import (
	"strings"
)

var commands = []string{"examine", "reply", "like", "thread", "open", "preview", "dm", "rt", "parent", "children", "thread", "rm"}

func processInput(input string) (command string, arguments string) {

	if input == "" {
		command = ""
		arguments = ""
		return
	}

	if input[0] != '/' {
		command = ""
		arguments = input
		return
	}

	input = input[1:]

	command = "Command not found"
	arguments = ""
	commandInput := ""

	firstSpaceIdx := strings.Index(input, " ")

	if firstSpaceIdx == -1 {
		commandInput = input
		arguments = ""
	} else {
		commandInput = input[:firstSpaceIdx]
		arguments = strings.TrimSpace(input[firstSpaceIdx+1:])
	}

	for _, cmd := range commands {
		if strings.HasPrefix(cmd, commandInput) {
			if command == "" || len(cmd) < len(command) {
				command = cmd
			}
		}
	}
	return
}