mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-22 16:48:23 +01:00
422338c04b
adds zsh support to waveterm. big change, lots going on here. lots of other improvements and bug fixes added while debugging and building out the feature. Commits: * refactor shexec parser.go into new package shellenv. separate out bash specific parsing from generic functions * checkpoint * work on refactoring shexec. created two new packages shellapi (for bash/zsh specific stuff), and shellutil (shared between shellapi and shexec) * more refactoring * create shellapi interface to abstract bash specific functionality * more refactoring, move bash shell state parsing to shellapi * move makeRcFile to shellapi. remove all of the 'client' options CLI options from waveshell * get shellType passed through to server/single paths for waveshell * add a local shelltype detector * mock out a zshapi * move shelltype through more of the code * get a command to run via zsh * zsh can now switch directories. poc, needs cleanup * working on ShellState encoding differences between zsh/bash. Working on parsing zsh decls. move utilfn package into waveshell (shouldn't have been in wavesrv) * switch to use []byte for vardecl serialization + diffs * progress on zsh environment. still have issues reconciling init environment with trap environment * fix typeset argument parsing * parse promptvars, more zsh specific ignores * fix bug with promptvar not getting set (wrong check in FeState func) * add sdk (issue #188) to list of rtnstate commands * more zsh compatibility -- working with a larger ohmyzsh environment. ignore more variables, handle exit trap better. unique path/fpath. add a processtype variable to base. * must return a value * zsh alias parsing/restoring. diff changes (and rtnstate changes). introduces linediff v1. * force zmodload of zsh/parameter * starting work on zsh functions * need a v1 of mapdiff as well (to handle null chars) * pack/unpack of ints was wrong (one used int and one use uint). turned out we only ever encoded '0' so it worked. that also means it is safe to change unpack to unpackUInt * reworking for binary encoding of aliases and functions (because of zsh allows any character, including nulls, in names and values) * fixes, working on functions, issue with line endings * zsh functions. lots of ugliness here around dealing with line dicipline and cooked stty. new runcommand function to grab output from a non-tty fd. note that we still to run the actual command in a stty to get the proper output. * write uuid tempdir, cleanup with tmprcfilename code * hack in some simple zsh function declaration finding code for rtnstate. create function diff for rtnstate that supports zsh * make sure key order is constant so shell hashes are consistent * fix problems with state diffs to support new zsh formats. add diff/apply code to shellapi (moved from shellenv), that is now specific to zsh or bash * add log packet and new shellstate packets * switch to shellstate map that's also keyed by shelltype * add shelltype to remoteinstance * remove shell argument from waveshell * added new shelltype statemap to remote.go (msh), deal with fallout * move shellstate out of init packet, and move to an explicit reinit call. try to initialize all of the active shell states * change dont always store init state (only store on demand). initialize shell states on demand (if not already initialized). allow reset to change shells * add shellpref field to remote table. use to drive the default shell choice for new tabs * show shelltag on cmdinput, pass through ri and remote (defaultshellstate) * bump mshell version to v0.4 * better version validation for shellstate. also relax compatibility requirements for diffing states (shelltype + major version need to match) * better error handling, check shellstate compatibility during run (on waveshell server) * add extra separator for bash shellstate processing to deal with spurious output from rc files * special migration for v30 -- flag invalid bash shell states and show special button in UI to fix * format * remove zsh-decls (unused) * remove test code * remove debug print * fix typo
104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
// Copyright 2023, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package comp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/utilfn"
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/remote"
|
|
)
|
|
|
|
var globalLock = &sync.Mutex{}
|
|
var simpleCompMap = map[string]SimpleCompGenFnType{
|
|
CGTypeCommand: simpleCompCommand,
|
|
CGTypeFile: simpleCompFile,
|
|
CGTypeDir: simpleCompDir,
|
|
CGTypeVariable: simpleCompVar,
|
|
}
|
|
|
|
type SimpleCompGenFnType = func(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error)
|
|
|
|
func RegisterSimpleCompFn(compType string, fn SimpleCompGenFnType) {
|
|
globalLock.Lock()
|
|
defer globalLock.Unlock()
|
|
if _, ok := simpleCompMap[compType]; ok {
|
|
panic(fmt.Sprintf("simpleCompFn %q already registered", compType))
|
|
}
|
|
simpleCompMap[compType] = fn
|
|
}
|
|
|
|
func getSimpleCompFn(compType string) SimpleCompGenFnType {
|
|
globalLock.Lock()
|
|
defer globalLock.Unlock()
|
|
return simpleCompMap[compType]
|
|
}
|
|
|
|
func DoSimpleComp(ctx context.Context, compType string, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
|
|
compFn := getSimpleCompFn(compType)
|
|
if compFn == nil {
|
|
return nil, fmt.Errorf("no simple comp fn for %q", compType)
|
|
}
|
|
crtn, err := compFn(ctx, prefix, compCtx, args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
crtn.CompType = compType
|
|
return crtn, nil
|
|
}
|
|
|
|
func compsToCompReturn(comps []string, hasMore bool) *CompReturn {
|
|
var rtn CompReturn
|
|
rtn.HasMore = hasMore
|
|
for _, comp := range comps {
|
|
rtn.Entries = append(rtn.Entries, CompEntry{Word: comp})
|
|
}
|
|
return &rtn
|
|
}
|
|
|
|
func doCompGen(ctx context.Context, prefix string, compType string, compCtx CompContext) (*CompReturn, error) {
|
|
if !packet.IsValidCompGenType(compType) {
|
|
return nil, fmt.Errorf("/_compgen invalid type '%s'", compType)
|
|
}
|
|
msh := remote.GetRemoteById(compCtx.RemotePtr.RemoteId)
|
|
if msh == nil {
|
|
return nil, fmt.Errorf("invalid remote '%s', not found", compCtx.RemotePtr)
|
|
}
|
|
cgPacket := packet.MakeCompGenPacket()
|
|
cgPacket.ReqId = uuid.New().String()
|
|
cgPacket.CompType = compType
|
|
cgPacket.Prefix = prefix
|
|
cgPacket.Cwd = compCtx.Cwd
|
|
resp, err := msh.PacketRpc(ctx, cgPacket)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = resp.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
comps := utilfn.GetStrArr(resp.Data, "comps")
|
|
hasMore := utilfn.GetBool(resp.Data, "hasmore")
|
|
return compsToCompReturn(comps, hasMore), nil
|
|
}
|
|
|
|
func simpleCompFile(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
|
|
return doCompGen(ctx, prefix, CGTypeFile, compCtx)
|
|
}
|
|
|
|
func simpleCompDir(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
|
|
return doCompGen(ctx, prefix, CGTypeDir, compCtx)
|
|
}
|
|
|
|
func simpleCompVar(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
|
|
return doCompGen(ctx, prefix, CGTypeVariable, compCtx)
|
|
}
|
|
|
|
func simpleCompCommand(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
|
|
return doCompGen(ctx, prefix, CGTypeCommand, compCtx)
|
|
}
|