123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package Terminal
- import (
- "golang.org/x/crypto/ssh/terminal"
- "strings"
- "github.com/swzry/go.TSmap"
- "fmt"
- "git.swzry.com/NSMCServerLauncher/Logger"
- "git.swzry.com/NSMCServerLauncher/Utils"
- )
- const HELP_MAX_LINE_WIDTH = 80
- var CommandList TSmap.TSmap
- var HelpCommandList string
- func CommandSystemInit() {
- CommandList = &TSmap.NewTSmap{
- ConMap: make(map[interface{}]interface{}),
- }
- }
- func RegistCommand(name string, reginfo RegistedCommand) {
- CommandList.Set(name,reginfo)
- }
- func GenerateHelpCommandList(){
- linecnt := 0
- CommandList.ForEach(func(k, v interface{}) {
- key,ok := k.(string)
- if(!ok){
- return
- }
- if len(key) + linecnt + 1 <= HELP_MAX_LINE_WIDTH {
- HelpCommandList += " " + key
- linecnt += (len(key) + 1)
- }else{
- HelpCommandList += "\n" + key
- linecnt = len(key)
- }
- })
- }
- type RegistedCommand struct {
- CmdFunc func(args []string, term *terminal.Terminal,context *Utils.UserContextType)
- HelpFunc func(topic string, term *terminal.Terminal)
- Intro string
- Usage string
- }
- func ExecuteCommandLine(cmdline string, term *terminal.Terminal,context *Utils.UserContextType){
- splwords := strings.Split(cmdline," ")
- if(splwords[0] == "help"){
- GetHelp(splwords[1:],term)
- return
- }
- vf,ok := CommandList.Get(splwords[0])
- if(!ok){
- term.Write([]byte("NSMC Server Launcher Shell: Invalid Command! Use 'help' command to get help.\n"))
- return
- }
- rcv,ok := vf.(RegistedCommand)
- if(!ok){
- fmt.Fprintf(&Logger.Log.SysWarning,"Command '%v' Registry Invalid\n",splwords[0])
- term.Write([]byte("NSMC Server Launcher Shell: Invalid Command!\n"))
- return
- }
- rcv.CmdFunc(splwords[1:],term,context)
- term.SetPrompt("NSMC " + context.PWD + " >")
- }
- func GetHelp(args []string, term *terminal.Terminal){
- switch len(args) {
- case 1:
- vf,ok := CommandList.Get(args[0])
- if(!ok){
- term.Write([]byte("help: Invalid Command!\n"))
- return
- }
- rcv,ok := vf.(RegistedCommand)
- if(!ok){
- fmt.Fprintf(&Logger.Log.SysWarning,"Command '%v' Registry Invalid\n",args[0])
- term.Write([]byte("NSMC Server Launcher Shell: Invalid Command!\n"))
- return
- }
- term.Write([]byte(fmt.Sprintf("%s - %s\n\nUsage:\n%s\n\n",args[0],rcv.Intro,rcv.Usage)))
- rcv.HelpFunc("",term)
- term.Write([]byte("\n"))
- break
- case 2:
- vf,ok := CommandList.Get(args[0])
- if(!ok){
- term.Write([]byte("help: Invalid Command!\n"))
- return
- }
- rcv,ok := vf.(RegistedCommand)
- if(!ok){
- fmt.Fprintf(&Logger.Log.SysWarning,"Command '%v' Registry Invalid\n",args[0])
- term.Write([]byte("NSMC Server Launcher Shell: Invalid Command!\n"))
- return
- }
- term.Write([]byte(fmt.Sprintf("Help Topic: %s -> %s\n\n",args[0],args[1])))
- rcv.HelpFunc(args[1],term)
- term.Write([]byte("\n"))
- break
- default:
- term.Write([]byte("You can get detail infomations by using 'help <command>' or 'help <commmand> <topic>'\n\n"))
- term.Write([]byte("Available Commands:\n"))
- term.Write([]byte(HelpCommandList))
- term.Write([]byte("\n\n"))
- break
- }
- }
|