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"
|
|
|
|
"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-12-21 15:46:58 -05:00
|
|
|
charWidth = 6
|
|
|
|
charHeight = 16
|
|
|
|
charDoubleWidth = charWidth * 2
|
|
|
|
rowCount = 24
|
|
|
|
rowCountMax = 32
|
|
|
|
colCountMax = 128
|
|
|
|
animLength = 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
|
|
|
)
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
type visibility int
|
2019-12-26 11:13:05 -05:00
|
|
|
|
|
|
|
const (
|
2020-12-21 15:46:58 -05:00
|
|
|
visHidden visibility = iota
|
|
|
|
visShowing
|
|
|
|
visShown
|
|
|
|
visHiding
|
2019-12-26 11:13:05 -05:00
|
|
|
)
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
const (
|
|
|
|
maxVisAnim = 1.0
|
|
|
|
minVisAnim = 0.0
|
|
|
|
)
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
type historyEntry 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
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
type commandEntry struct {
|
2019-12-26 11:13:05 -05:00
|
|
|
description string
|
2020-12-21 15:46:58 -05:00
|
|
|
arguments []string
|
|
|
|
fn func([]string) error
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Terminal handles the in-game terminal
|
|
|
|
type Terminal struct {
|
|
|
|
outputHistory []historyEntry
|
2019-12-26 11:13:05 -05:00
|
|
|
outputIndex int
|
|
|
|
|
|
|
|
command string
|
|
|
|
commandHistory []string
|
|
|
|
commandIndex int
|
|
|
|
|
|
|
|
lineCount int
|
2020-12-21 15:46:58 -05:00
|
|
|
visState visibility
|
2019-12-26 11:13:05 -05:00
|
|
|
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
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
commands map[string]commandEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTerminal creates and returns a terminal
|
|
|
|
func NewTerminal() (*Terminal, error) {
|
|
|
|
term := &Terminal{
|
|
|
|
lineCount: rowCount,
|
|
|
|
bgColor: d2util.Color(darkGrey),
|
|
|
|
fgColor: d2util.Color(lightGrey),
|
|
|
|
infoColor: d2util.Color(lightBlue),
|
|
|
|
warningColor: d2util.Color(yellow),
|
|
|
|
errorColor: d2util.Color(red),
|
|
|
|
commands: make(map[string]commandEntry),
|
|
|
|
}
|
|
|
|
|
|
|
|
term.Infof("::: OpenDiablo2 Terminal :::")
|
|
|
|
term.Infof("type \"ls\" for a list of commands")
|
|
|
|
|
|
|
|
if err := term.Bind("ls", "list available commands", nil, term.commandList); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := term.Bind("clear", "clear terminal", nil, term.commandClear); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return term, nil
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Bind binds commands to the terminal
|
|
|
|
func (t *Terminal) Bind(name, description string, arguments []string, fn func(args []string) error) error {
|
|
|
|
if name == "" || description == "" {
|
|
|
|
return fmt.Errorf("missing name or description")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := t.commands[name]; ok {
|
|
|
|
t.Warningf("rebinding command with name: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.commands[name] = commandEntry{description, arguments, fn}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unbind unbinds commands from the terminal
|
|
|
|
func (t *Terminal) Unbind(names ...string) error {
|
|
|
|
for _, name := range names {
|
|
|
|
delete(t.commands, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance advances the terminal animation
|
|
|
|
func (t *Terminal) Advance(elapsed float64) error {
|
2019-12-26 11:13:05 -05:00
|
|
|
switch t.visState {
|
2020-12-21 15:46:58 -05:00
|
|
|
case visShowing:
|
|
|
|
t.visAnim = math.Min(maxVisAnim, t.visAnim+elapsed/animLength)
|
2020-07-02 13:55:43 -04:00
|
|
|
if t.visAnim == maxVisAnim {
|
2020-12-21 15:46:58 -05:00
|
|
|
t.visState = visShown
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-12-21 15:46:58 -05:00
|
|
|
case visHiding:
|
|
|
|
t.visAnim = math.Max(minVisAnim, t.visAnim-elapsed/animLength)
|
2020-07-02 13:55:43 -04:00
|
|
|
if t.visAnim == minVisAnim {
|
2020-12-21 15:46:58 -05:00
|
|
|
t.visState = visHidden
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
if !t.Visible() {
|
2019-12-26 11:13:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:28:21 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// OnKeyDown handles key down in the terminal
|
|
|
|
func (t *Terminal) OnKeyDown(event d2interface.KeyEvent) bool {
|
2020-07-06 21:26:08 -04:00
|
|
|
if event.Key() == d2enum.KeyGraveAccent {
|
2020-12-21 15:46:58 -05:00
|
|
|
t.toggle()
|
2020-02-02 21:26:08 -05:00
|
|
|
}
|
2020-01-25 23:28:21 -05:00
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
if !t.Visible() {
|
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-12-21 15:46:58 -05:00
|
|
|
func (t *Terminal) processCommand() {
|
2020-06-30 17:04:41 -04:00
|
|
|
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)
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
t.Printf(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 {
|
2020-12-21 15:46:58 -05:00
|
|
|
t.Errorf(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-12-21 15:46:58 -05: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-12-21 15:46:58 -05:00
|
|
|
t.lineCount = d2math.MinInt(t.lineCount+1, rowCountMax)
|
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-12-21 15:46:58 -05:00
|
|
|
// OnKeyChars handles char key in terminal
|
|
|
|
func (t *Terminal) OnKeyChars(event d2interface.KeyCharsEvent) bool {
|
|
|
|
if !t.Visible() {
|
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-12-21 15:46:58 -05:00
|
|
|
// Render renders the terminal
|
|
|
|
func (t *Terminal) Render(surface d2interface.Surface) error {
|
|
|
|
if !t.Visible() {
|
2019-12-26 11:13:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
totalWidth, _ := surface.GetSize()
|
2020-12-21 15:46:58 -05:00
|
|
|
outputHeight := t.lineCount * charHeight
|
|
|
|
totalHeight := outputHeight + charHeight
|
2019-12-26 11:13:05 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
entry := t.outputHistory[historyIndex]
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
surface.PushTranslation(charDoubleWidth, outputHeight-(i+1)*charHeight)
|
|
|
|
surface.DrawTextf(entry.text)
|
|
|
|
surface.PushTranslation(-charDoubleWidth, 0)
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
switch entry.category {
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.TermCategoryInfo:
|
2020-12-21 15:46:58 -05:00
|
|
|
surface.DrawRect(charWidth, charHeight, t.infoColor)
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.TermCategoryWarning:
|
2020-12-21 15:46:58 -05:00
|
|
|
surface.DrawRect(charWidth, charHeight, t.warningColor)
|
2020-07-06 21:26:08 -04:00
|
|
|
case d2enum.TermCategoryError:
|
2020-12-21 15:46:58 -05:00
|
|
|
surface.DrawRect(charWidth, charHeight, 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-12-21 15:46:58 -05:00
|
|
|
surface.DrawRect(totalWidth, charHeight, 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-12-21 15:46:58 -05:00
|
|
|
// Execute executes a command with arguments
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
name := params[0]
|
|
|
|
args := params[1:]
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
entry, ok := t.commands[name]
|
2019-12-26 11:13:05 -05:00
|
|
|
if !ok {
|
2020-12-21 15:46:58 -05:00
|
|
|
return errors.New("command not found")
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
if len(args) != len(entry.arguments) {
|
|
|
|
return errors.New("command requires different argument count")
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
if err := entry.fn(args); err != nil {
|
2020-07-02 13:55:43 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Rawf writes a raw message to the terminal
|
|
|
|
func (t *Terminal) Rawf(category d2enum.TermCategory, format string, params ...interface{}) {
|
|
|
|
text := fmt.Sprintf(format, params...)
|
|
|
|
lines := d2util.SplitIntoLinesWithMaxWidth(text, colCountMax)
|
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-21 15:46:58 -05:00
|
|
|
t.outputHistory = append(t.outputHistory, historyEntry{line, category})
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Printf writes a message to the terminal
|
|
|
|
func (t *Terminal) Printf(format string, params ...interface{}) {
|
|
|
|
t.Rawf(d2enum.TermCategoryNone, format, params...)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Infof writes a warning message to the terminal
|
|
|
|
func (t *Terminal) Infof(format string, params ...interface{}) {
|
|
|
|
t.Rawf(d2enum.TermCategoryInfo, format, params...)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Warningf writes a warning message to the terminal
|
|
|
|
func (t *Terminal) Warningf(format string, params ...interface{}) {
|
|
|
|
t.Rawf(d2enum.TermCategoryWarning, format, params...)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Errorf writes a error message to the terminal
|
|
|
|
func (t *Terminal) Errorf(format string, params ...interface{}) {
|
|
|
|
t.Rawf(d2enum.TermCategoryError, format, params...)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Clear clears the terminal
|
|
|
|
func (t *Terminal) Clear() {
|
2019-12-26 11:13:05 -05:00
|
|
|
t.outputHistory = nil
|
|
|
|
t.outputIndex = 0
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Visible returns visible state
|
|
|
|
func (t *Terminal) Visible() bool {
|
|
|
|
return t.visState != visHidden
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Hide hides the terminal
|
|
|
|
func (t *Terminal) Hide() {
|
|
|
|
if t.visState != visHidden {
|
|
|
|
t.visState = visHiding
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// Show shows the terminal
|
|
|
|
func (t *Terminal) Show() {
|
|
|
|
if t.visState != visShown {
|
|
|
|
t.visState = visShowing
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
func (t *Terminal) toggle() {
|
|
|
|
if t.visState == visHiding || t.visState == visHidden {
|
|
|
|
t.Show()
|
|
|
|
return
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
t.Hide()
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:46:58 -05:00
|
|
|
// BindLogger binds a log.Writer to the output
|
|
|
|
func (t *Terminal) BindLogger() {
|
2020-06-28 21:40:52 -04:00
|
|
|
log.SetOutput(&terminalLogger{writer: log.Writer(), terminal: t})
|
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
|
|
|
|
}
|