2019-12-26 11:13:05 -05:00
|
|
|
package d2term
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"image/color"
|
2020-06-28 21:40:52 -04:00
|
|
|
"log"
|
2019-12-26 11:13:05 -05:00
|
|
|
"math"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-11 11:24:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-28 21:40:52 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-08-05 22:32:23 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
2020-12-14 05:40:19 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2019-12-26 11:13:05 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-05 22:32:23 -04:00
|
|
|
termCharWidth = 6
|
|
|
|
termCharHeight = 16
|
|
|
|
termCharDoubleWidth = termCharWidth * 2
|
|
|
|
termRowCount = 24
|
|
|
|
termRowCountMax = 32
|
|
|
|
termColCountMax = 128
|
|
|
|
termAnimLength = 0.5
|
2019-12-26 11:13:05 -05:00
|
|
|
)
|
|
|
|
|
2020-07-18 00:02:04 -04:00
|
|
|
const (
|
2020-07-23 12:56:50 -04:00
|
|
|
darkGrey = 0x2e3436b0
|
2020-07-18 00:02:04 -04:00
|
|
|
lightGrey = 0x555753b0
|
|
|
|
lightBlue = 0x3465a4b0
|
2020-07-23 12:56:50 -04:00
|
|
|
yellow = 0xfce94fb0
|
|
|
|
red = 0xcc0000b0
|
2020-07-18 00:02:04 -04:00
|
|
|
)
|
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
type termVis int
|
|
|
|
|
|
|
|
const (
|
|
|
|
termVisHidden termVis = iota
|
|
|
|
termVisShowing
|
|
|
|
termVisShown
|
|
|
|
termVisHiding
|
|
|
|
)
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
const (
|
|
|
|
maxVisAnim = 1.0
|
|
|
|
minVisAnim = 0.0
|
|
|
|
)
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
type termHistoryEntry struct {
|
2019-12-26 11:13:05 -05:00
|
|
|
text string
|
2020-07-06 21:26:08 -04:00
|
|
|
category d2enum.TermCategory
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type termActionEntry struct {
|
|
|
|
action interface{}
|
|
|
|
description string
|
|
|
|
}
|
|
|
|
|
|
|
|
type terminal struct {
|
2020-06-30 17:04:41 -04:00
|
|
|
outputHistory []termHistoryEntry
|
2019-12-26 11:13:05 -05:00
|
|
|
outputIndex int
|
|
|
|
|
|
|
|
command string
|
|
|
|
commandHistory []string
|
|
|
|
commandIndex int
|
|
|
|
|
|
|
|
lineCount int
|
|
|
|
visState termVis
|
|
|
|
visAnim float64
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
bgColor color.RGBA
|
|
|
|
fgColor color.RGBA
|
|
|
|
infoColor color.RGBA
|
|
|
|
warningColor color.RGBA
|
|
|
|
errorColor color.RGBA
|
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
actions map[string]termActionEntry
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) Advance(elapsed float64) error {
|
2019-12-26 11:13:05 -05:00
|
|
|
switch t.visState {
|
|
|
|
case termVisShowing:
|
2020-07-02 13:55:43 -04:00
|
|
|
t.visAnim = math.Min(maxVisAnim, t.visAnim+elapsed/termAnimLength)
|
|
|
|
if t.visAnim == maxVisAnim {
|
2019-12-26 11:13:05 -05:00
|
|
|
t.visState = termVisShown
|
|
|
|
}
|
|
|
|
case termVisHiding:
|
2020-07-02 13:55:43 -04:00
|
|
|
t.visAnim = math.Max(minVisAnim, t.visAnim-elapsed/termAnimLength)
|
|
|
|
if t.visAnim == minVisAnim {
|
2019-12-26 11:13:05 -05:00
|
|
|
t.visState = termVisHidden
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
if !t.IsVisible() {
|
2019-12-26 11:13:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:28:21 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (t *terminal) OnKeyDown(event d2interface.KeyEvent) bool {
|
2020-07-06 21:26:08 -04:00
|
|
|
if event.Key() == d2enum.KeyGraveAccent {
|
2020-06-30 17:04:41 -04:00
|
|
|
t.toggleTerminal()
|
2020-02-02 21:26:08 -05:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
if !t.IsVisible() {
|
2020-02-01 14:35:55 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
switch event.Key() {
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyEscape:
|
2020-01-25 23:28:21 -05:00
|
|
|
t.command = ""
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyEnd:
|
2019-12-26 11:13:05 -05:00
|
|
|
t.outputIndex = 0
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyHome:
|
2020-08-05 00:03:33 -04:00
|
|
|
t.outputIndex = d2math.MaxInt(0, len(t.outputHistory)-t.lineCount)
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyPageUp:
|
2020-08-05 00:03:33 -04:00
|
|
|
maxOutputIndex := d2math.MaxInt(0, len(t.outputHistory)-t.lineCount)
|
2020-06-30 17:04:41 -04:00
|
|
|
if t.outputIndex += t.lineCount; t.outputIndex >= maxOutputIndex {
|
|
|
|
t.outputIndex = maxOutputIndex
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyPageDown:
|
2019-12-26 11:13:05 -05:00
|
|
|
if t.outputIndex -= t.lineCount; t.outputIndex < 0 {
|
|
|
|
t.outputIndex = 0
|
|
|
|
}
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyUp, d2enum.KeyDown:
|
2020-07-03 15:09:16 -04:00
|
|
|
t.handleControlKey(event.Key(), event.KeyMod())
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyEnter:
|
2020-06-30 17:04:41 -04:00
|
|
|
t.processCommand()
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyBackspace:
|
2020-06-30 17:04:41 -04:00
|
|
|
if len(t.command) > 0 {
|
|
|
|
t.command = t.command[:len(t.command)-1]
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
return true
|
|
|
|
}
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
func (t *terminal) processCommand() {
|
|
|
|
if t.command == "" {
|
|
|
|
return
|
|
|
|
}
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
n := 0
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
for _, command := range t.commandHistory {
|
|
|
|
if command != t.command {
|
|
|
|
t.commandHistory[n] = command
|
|
|
|
n++
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
}
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
t.commandHistory = t.commandHistory[:n]
|
|
|
|
t.commandHistory = append(t.commandHistory, t.command)
|
|
|
|
|
|
|
|
t.Outputf(t.command)
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
if err := t.Execute(t.command); err != nil {
|
|
|
|
t.OutputErrorf(err.Error())
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
t.commandIndex = len(t.commandHistory) - 1
|
|
|
|
t.command = ""
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:26:08 -04:00
|
|
|
func (t *terminal) handleControlKey(eventKey d2enum.Key, keyMod d2enum.KeyMod) {
|
2020-07-02 13:55:43 -04:00
|
|
|
switch eventKey {
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyUp:
|
|
|
|
if keyMod == d2enum.KeyModControl {
|
2020-08-05 00:03:33 -04:00
|
|
|
t.lineCount = d2math.MaxInt(0, t.lineCount-1)
|
2020-07-02 13:55:43 -04:00
|
|
|
} else if len(t.commandHistory) > 0 {
|
|
|
|
t.command = t.commandHistory[t.commandIndex]
|
|
|
|
if t.commandIndex == 0 {
|
|
|
|
t.commandIndex = len(t.commandHistory) - 1
|
|
|
|
} else {
|
|
|
|
t.commandIndex--
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.KeyDown:
|
|
|
|
if keyMod == d2enum.KeyModControl {
|
2020-08-05 00:03:33 -04:00
|
|
|
t.lineCount = d2math.MinInt(t.lineCount+1, termRowCountMax)
|
2020-06-30 17:04:41 -04:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
func (t *terminal) toggleTerminal() {
|
|
|
|
if t.visState == termVisHiding || t.visState == termVisHidden {
|
|
|
|
t.Show()
|
|
|
|
} else {
|
|
|
|
t.Hide()
|
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
func (t *terminal) OnKeyChars(event d2interface.KeyCharsEvent) bool {
|
2020-06-28 21:40:52 -04:00
|
|
|
if !t.IsVisible() {
|
2020-02-02 21:26:08 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:28:21 -05:00
|
|
|
var handled bool
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2020-07-03 15:09:16 -04:00
|
|
|
for _, c := range event.Chars() {
|
2019-12-26 11:13:05 -05:00
|
|
|
if c != '`' {
|
|
|
|
t.command += string(c)
|
2020-01-25 23:28:21 -05:00
|
|
|
handled = true
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:28:21 -05:00
|
|
|
return handled
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (t *terminal) Render(surface d2interface.Surface) error {
|
2020-06-28 21:40:52 -04:00
|
|
|
if !t.IsVisible() {
|
2019-12-26 11:13:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
totalWidth, _ := surface.GetSize()
|
|
|
|
outputHeight := t.lineCount * termCharHeight
|
|
|
|
totalHeight := outputHeight + termCharHeight
|
|
|
|
|
|
|
|
offset := -int((1.0 - easeInOut(t.visAnim)) * float64(totalHeight))
|
|
|
|
surface.PushTranslation(0, offset)
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
surface.DrawRect(totalWidth, outputHeight, t.bgColor)
|
2019-12-26 11:13:05 -05:00
|
|
|
|
|
|
|
for i := 0; i < t.lineCount; i++ {
|
|
|
|
historyIndex := len(t.outputHistory) - i - t.outputIndex - 1
|
|
|
|
if historyIndex < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
historyEntry := t.outputHistory[historyIndex]
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2020-08-05 22:32:23 -04:00
|
|
|
surface.PushTranslation(termCharDoubleWidth, outputHeight-(i+1)*termCharHeight)
|
2020-07-17 18:51:44 -04:00
|
|
|
surface.DrawTextf(historyEntry.text)
|
2020-08-05 22:32:23 -04:00
|
|
|
surface.PushTranslation(-termCharDoubleWidth, 0)
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
switch historyEntry.category {
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.TermCategoryInfo:
|
2020-06-30 17:04:41 -04:00
|
|
|
surface.DrawRect(termCharWidth, termCharHeight, t.infoColor)
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.TermCategoryWarning:
|
2020-06-30 17:04:41 -04:00
|
|
|
surface.DrawRect(termCharWidth, termCharHeight, t.warningColor)
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.TermCategoryError:
|
2020-06-30 17:04:41 -04:00
|
|
|
surface.DrawRect(termCharWidth, termCharHeight, t.errorColor)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
surface.Pop()
|
|
|
|
surface.Pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
surface.PushTranslation(0, outputHeight)
|
2020-06-30 17:04:41 -04:00
|
|
|
surface.DrawRect(totalWidth, termCharHeight, t.fgColor)
|
2020-07-17 18:51:44 -04:00
|
|
|
surface.DrawTextf("> " + t.command)
|
2019-12-26 11:13:05 -05:00
|
|
|
surface.Pop()
|
|
|
|
|
|
|
|
surface.Pop()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) Execute(command string) error {
|
2019-12-26 11:13:05 -05:00
|
|
|
params := parseCommand(command)
|
|
|
|
if len(params) == 0 {
|
|
|
|
return errors.New("invalid command")
|
|
|
|
}
|
|
|
|
|
|
|
|
actionName := params[0]
|
|
|
|
actionParams := params[1:]
|
|
|
|
|
|
|
|
actionEntry, ok := t.actions[actionName]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("action not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
actionType := reflect.TypeOf(actionEntry.action)
|
|
|
|
if actionType.Kind() != reflect.Func {
|
|
|
|
return errors.New("action is not a function")
|
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
if len(actionParams) != actionType.NumIn() {
|
|
|
|
return errors.New("action requires different argument count")
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
paramValues, err := parseActionParams(actionType, actionParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
actionValue := reflect.ValueOf(actionEntry.action)
|
|
|
|
actionReturnValues := actionValue.Call(paramValues)
|
|
|
|
|
|
|
|
if actionReturnValueCount := len(actionReturnValues); actionReturnValueCount > 0 {
|
|
|
|
t.OutputInfof("function returned %d values:", actionReturnValueCount)
|
|
|
|
|
|
|
|
for _, actionReturnValue := range actionReturnValues {
|
|
|
|
t.OutputInfof("%v: %s", actionReturnValue.Interface(), actionReturnValue.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseActionParams(actionType reflect.Type, actionParams []string) ([]reflect.Value, error) {
|
2019-12-26 11:13:05 -05:00
|
|
|
var paramValues []reflect.Value
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
for i := 0; i < actionType.NumIn(); i++ {
|
|
|
|
actionParam := actionParams[i]
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
switch actionType.In(i).Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
paramValues = append(paramValues, reflect.ValueOf(actionParam))
|
|
|
|
case reflect.Int:
|
|
|
|
value, err := strconv.ParseInt(actionParam, 10, 64)
|
|
|
|
if err != nil {
|
2020-07-02 13:55:43 -04:00
|
|
|
return nil, err
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
paramValues = append(paramValues, reflect.ValueOf(int(value)))
|
|
|
|
case reflect.Uint:
|
|
|
|
value, err := strconv.ParseUint(actionParam, 10, 64)
|
|
|
|
if err != nil {
|
2020-07-02 13:55:43 -04:00
|
|
|
return nil, err
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
paramValues = append(paramValues, reflect.ValueOf(uint(value)))
|
|
|
|
case reflect.Float64:
|
|
|
|
value, err := strconv.ParseFloat(actionParam, 64)
|
|
|
|
if err != nil {
|
2020-07-02 13:55:43 -04:00
|
|
|
return nil, err
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
paramValues = append(paramValues, reflect.ValueOf(value))
|
|
|
|
case reflect.Bool:
|
|
|
|
value, err := strconv.ParseBool(actionParam)
|
|
|
|
if err != nil {
|
2020-07-02 13:55:43 -04:00
|
|
|
return nil, err
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
paramValues = append(paramValues, reflect.ValueOf(value))
|
|
|
|
default:
|
2020-07-02 13:55:43 -04:00
|
|
|
return nil, errors.New("action has unsupported arguments")
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
return paramValues, nil
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:26:08 -04:00
|
|
|
func (t *terminal) OutputRaw(text string, category d2enum.TermCategory) {
|
2020-12-14 07:45:06 -05:00
|
|
|
lines := d2util.SplitIntoLinesWithMaxWidth(text, termColCountMax)
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2020-12-14 07:45:06 -05:00
|
|
|
for _, line := range lines {
|
|
|
|
// removes color token (this token ends with [0m )
|
2020-12-16 02:56:09 -05:00
|
|
|
l := strings.Split(line, "\033[0m")
|
2020-12-14 07:45:06 -05:00
|
|
|
line = l[len(l)-1]
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-12-14 07:45:06 -05:00
|
|
|
t.outputHistory = append(t.outputHistory, termHistoryEntry{line, category})
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
func (t *terminal) Outputf(format string, params ...interface{}) {
|
2020-07-06 21:26:08 -04:00
|
|
|
t.OutputRaw(fmt.Sprintf(format, params...), d2enum.TermCategoryNone)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
func (t *terminal) OutputInfof(format string, params ...interface{}) {
|
2020-07-06 21:26:08 -04:00
|
|
|
t.OutputRaw(fmt.Sprintf(format, params...), d2enum.TermCategoryInfo)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
func (t *terminal) OutputWarningf(format string, params ...interface{}) {
|
2020-07-06 21:26:08 -04:00
|
|
|
t.OutputRaw(fmt.Sprintf(format, params...), d2enum.TermCategoryWarning)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
func (t *terminal) OutputErrorf(format string, params ...interface{}) {
|
2020-07-06 21:26:08 -04:00
|
|
|
t.OutputRaw(fmt.Sprintf(format, params...), d2enum.TermCategoryError)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) OutputClear() {
|
2019-12-26 11:13:05 -05:00
|
|
|
t.outputHistory = nil
|
|
|
|
t.outputIndex = 0
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) IsVisible() bool {
|
2019-12-26 11:13:05 -05:00
|
|
|
return t.visState != termVisHidden
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) Hide() {
|
2019-12-26 11:13:05 -05:00
|
|
|
if t.visState != termVisHidden {
|
|
|
|
t.visState = termVisHiding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) Show() {
|
2019-12-26 11:13:05 -05:00
|
|
|
if t.visState != termVisShown {
|
|
|
|
t.visState = termVisShowing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) BindAction(name, description string, action interface{}) error {
|
2019-12-26 11:13:05 -05:00
|
|
|
actionType := reflect.TypeOf(action)
|
|
|
|
if actionType.Kind() != reflect.Func {
|
|
|
|
return errors.New("action is not a function")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < actionType.NumIn(); i++ {
|
|
|
|
switch actionType.In(i).Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
case reflect.Int:
|
|
|
|
case reflect.Uint:
|
|
|
|
case reflect.Float64:
|
|
|
|
case reflect.Bool:
|
|
|
|
default:
|
|
|
|
return errors.New("action has unsupported arguments")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.actions[name] = termActionEntry{action, description}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) BindLogger() {
|
|
|
|
log.SetOutput(&terminalLogger{writer: log.Writer(), terminal: t})
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
func (t *terminal) UnbindAction(name string) error {
|
|
|
|
delete(t.actions, name)
|
|
|
|
return nil
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func easeInOut(t float64) float64 {
|
|
|
|
t *= 2
|
|
|
|
if t < 1 {
|
|
|
|
return 0.5 * t * t * t * t
|
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
|
|
|
t -= 2
|
|
|
|
|
|
|
|
return -0.5 * (t*t*t*t - 2)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseCommand(command string) []string {
|
|
|
|
var (
|
|
|
|
quoted bool
|
|
|
|
escape bool
|
|
|
|
param string
|
|
|
|
params []string
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, c := range command {
|
|
|
|
switch c {
|
|
|
|
case '"':
|
|
|
|
if escape {
|
|
|
|
param += string(c)
|
|
|
|
escape = false
|
|
|
|
} else {
|
|
|
|
quoted = !quoted
|
|
|
|
}
|
|
|
|
case ' ':
|
|
|
|
if quoted {
|
|
|
|
param += string(c)
|
|
|
|
} else if len(param) > 0 {
|
|
|
|
params = append(params, param)
|
|
|
|
param = ""
|
|
|
|
}
|
|
|
|
case '\\':
|
|
|
|
if escape {
|
|
|
|
param += string(c)
|
|
|
|
escape = false
|
|
|
|
} else {
|
|
|
|
escape = true
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
param += string(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(param) > 0 {
|
|
|
|
params = append(params, param)
|
|
|
|
}
|
|
|
|
|
|
|
|
return params
|
|
|
|
}
|
2020-06-28 21:40:52 -04:00
|
|
|
|
|
|
|
func createTerminal() (*terminal, error) {
|
|
|
|
terminal := &terminal{
|
2020-06-30 17:04:41 -04:00
|
|
|
lineCount: termRowCount,
|
2020-12-14 05:40:19 -05:00
|
|
|
bgColor: d2util.Color(darkGrey),
|
|
|
|
fgColor: d2util.Color(lightGrey),
|
|
|
|
infoColor: d2util.Color(lightBlue),
|
|
|
|
warningColor: d2util.Color(yellow),
|
|
|
|
errorColor: d2util.Color(red),
|
2020-06-30 17:04:41 -04:00
|
|
|
actions: make(map[string]termActionEntry),
|
2020-06-28 21:40:52 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
terminal.OutputInfof("::: OpenDiablo2 Terminal :::")
|
|
|
|
terminal.OutputInfof("type \"ls\" for a list of actions")
|
2020-06-28 21:40:52 -04:00
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
err := terminal.BindAction("ls", "list available actions", func() {
|
2020-06-28 21:40:52 -04:00
|
|
|
var names []string
|
|
|
|
for name := range terminal.actions {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(names)
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
terminal.OutputInfof("available actions (%d):", len(names))
|
2020-06-28 21:40:52 -04:00
|
|
|
for _, name := range names {
|
|
|
|
entry := terminal.actions[name]
|
2020-06-30 17:04:41 -04:00
|
|
|
terminal.OutputInfof("%s: %s; %s", name, entry.description, reflect.TypeOf(entry.action).String())
|
2020-06-28 21:40:52 -04:00
|
|
|
}
|
|
|
|
})
|
2020-06-30 17:04:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to bind the '%s' action, err: %w", "ls", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = terminal.BindAction("clear", "clear terminal", func() {
|
2020-06-28 21:40:52 -04:00
|
|
|
terminal.OutputClear()
|
|
|
|
})
|
2020-06-30 17:04:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to bind the '%s' action, err: %w", "clear", err)
|
|
|
|
}
|
2020-06-28 21:40:52 -04:00
|
|
|
|
|
|
|
return terminal, nil
|
|
|
|
}
|