shithub: hell

ref: 322ee3085e1f5fd98207bcb6f18cc1cdfca36aaf
dir: /commands.go/

View raw version
package main

import (
	"strings"
)

var commands = []string{"examine", "reply", "like", "thread", "open", "prev", "download", "dm", "rt", "parent", "children", "rm", "mark", "unmark", "account", "import", "pause", "resume", "url", "fpost", "ufpost", "edit", "notice", "stats", "next", "view", "bookmarks", "follow", "unfollow", "likes", "help", "reload", "attach", "detach", "pinned", "cat", "play"}

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

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

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

	inputcommand, _, hasargument := strings.Cut(input[1:], " ")

	if !hasargument {
		inputcommand = input[1:]
	}

	for _, choice := range commands {
		if strings.HasPrefix(choice, inputcommand) {
			command = choice
			_, arguments, _ = strings.Cut(input, " ")
			found = true
			break
		}
	}

	if command == "" {
		command = inputcommand
	}

	return
}

func pullParameter(input string) (parameter string, remainder string) {
	if strings.HasPrefix(input, "?") {
		parameter, _, _ = strings.Cut(input, " ")
		if len(parameter) == len(input) {
			remainder = ""
			parameter = input
		} else {
			remainder = input[len(parameter)+1:]
		}
	} else {
		return "", input
	}

	prefix, _, cut := strings.Cut(parameter, "\"")

	if cut {
		quote, _, _ := strings.Cut(input[len(prefix)+1:], "\"")
		parameter = prefix + quote
		if len(parameter)+3 <= len(input) {
			remainder = input[len(parameter)+3:]
		} else {
			remainder = input[len(parameter)+2:]
		}
	}
	return
}

func splitParameter(input string) (parameter []string) {
	if len(input) < 2 {
		//nothing here
		return nil
	}
	input = input[1:]
	key, value, _ := strings.Cut(input, "=")
	return []string{key, value}
}

func extractInputParameters(input string) (remainder string, parameters [][]string) {
	parameter := " "
	remainder = input
	for parameter != "" {
		parameter, remainder = pullParameter(remainder)
		if parameter != " " && parameter != "" {
			parameters = append(parameters, splitParameter(parameter))
		}
	}
	return
}