shithub: hell

ref: 3bf4db591828464cac77a8555359b38c233c3bce
dir: /commands.go/

View raw version
package main

import (
	"strings"

	"github.com/google/shlex"
)

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

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 extractInputParameters(input string) (remainder string, parameters [][]string) {
	args, _ := shlex.Split(input)

	for i, arg := range args {
		if strings.HasPrefix(arg, "?") {
			key, val, _ := strings.Cut(arg, "=")
			key = key[1:]
			parameters = append(parameters, []string{key, val})
		} else {
			remainder = strings.Join(args[i:], " ")
			break
		}
	}
	return
}