shithub: hell

ref: fc9854d5225f65a8dade6efba46da532952d33a5
dir: /commands.go/

View raw version
package main

import (
	"strings"
)

var commands = []string{"examine", "reply", "like", "thread", "open", "preview", "download", "dm", "rt", "parent", "children", "rm", "mark", "unmark", "account", "vim", "import", "pause", "resume", "url", "fpost", "ufpost"}

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

	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, " ")
			break
		}
	}

	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
}