waveterm/waveshell/pkg/utilfn/syncmap.go
Mike Sawka b136c915df
Restart command (#253)
* working on cmd restart logic

* button to restart command

* bind Cmd-R to restart selected command, and Cmd-Shift-R to restart last command.  Browser Refresh is now Option-R.  also fix 'clear' command to not delete running commands (like archive).  some small changes to keyboard utility code to always set 'alt' and 'meta' appropriately.  use 'cmd' and 'option' for crossplatform bindings

* focus restarted line

* update termopts, use current winsize to set termopts for new command

* add cmd.restartts to track restart time

* display restarted time in line w/ tooltip with original time

* add restartts to line:show
2024-01-26 16:25:21 -08:00

85 lines
1.4 KiB
Go

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package utilfn
import (
"sync"
)
type SyncMap[K comparable, V any] struct {
lock *sync.Mutex
m map[K]V
}
func MakeSyncMap[K comparable, V any]() *SyncMap[K, V] {
return &SyncMap[K, V]{
lock: &sync.Mutex{},
m: make(map[K]V),
}
}
func (sm *SyncMap[K, V]) Set(k K, v V) {
sm.lock.Lock()
defer sm.lock.Unlock()
sm.m[k] = v
}
func (sm *SyncMap[K, V]) Get(k K) V {
sm.lock.Lock()
defer sm.lock.Unlock()
return sm.m[k]
}
func (sm *SyncMap[K, V]) GetEx(k K) (V, bool) {
sm.lock.Lock()
defer sm.lock.Unlock()
v, ok := sm.m[k]
return v, ok
}
func (sm *SyncMap[K, V]) Delete(k K) {
sm.lock.Lock()
defer sm.lock.Unlock()
delete(sm.m, k)
}
func (sm *SyncMap[K, V]) Clear() {
sm.lock.Lock()
defer sm.lock.Unlock()
sm.m = make(map[K]V)
}
func (sm *SyncMap[K, V]) Len() int {
sm.lock.Lock()
defer sm.lock.Unlock()
return len(sm.m)
}
func (sm *SyncMap[K, V]) Keys() []K {
sm.lock.Lock()
defer sm.lock.Unlock()
keys := make([]K, len(sm.m))
i := 0
for k := range sm.m {
keys[i] = k
i++
}
return keys
}
func (sm *SyncMap[K, V]) Replace(newMap map[K]V) {
sm.lock.Lock()
defer sm.lock.Unlock()
sm.m = make(map[K]V, len(newMap))
for k, v := range newMap {
sm.m[k] = v
}
}
func IncSyncMap[K comparable, V int | int64](sm *SyncMap[K, V], key K, incAmt V) {
sm.lock.Lock()
defer sm.lock.Unlock()
sm.m[key] += incAmt
}