Skip to content

Commit

Permalink
refactor!: change config schema, add new option
Browse files Browse the repository at this point in the history
  • Loading branch information
Serpentiel committed Aug 30, 2022
1 parent 38f6eca commit 12a6d26
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 28 deletions.
23 changes: 16 additions & 7 deletions internal/pkg/assets/.betterglobekey.example.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# An array of input sources to cycle through when the Globe key is pressed once.
primary_input_sources:
- com.apple.keylayout.US
- com.apple.keylayout.Russian
# Double press
double_press:
# Maximum delay between two presses of the Globe key for them to be considered as a double press
maximum_delay: 250

# An array of input sources to cycle through when the Globe key is double pressed.
additional_input_sources:
- com.apple.keylayout.Finnish
# Input sources
input_sources:
# Primary input sources
# This is used when the Globe key has been pressed once
primary:
- com.apple.keylayout.US
- com.apple.keylayout.Russian

# Additional input sources
# This is used when the Globe key has been double pressed
additional:
- com.apple.keylayout.Finnish
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import (
"time"

"github.com/Serpentiel/betterglobekey/pkg/inputsource"
"github.com/spf13/viper"
"go.uber.org/zap"
)

// logger is the logger for the application.
var logger *zap.SugaredLogger

// doublePressThreshold is the threshold for a double press.
const doublePressThreshold = 250

// handlers is a map of rawcode to a map of kind to handler.
var handlers HandlersMap

Expand All @@ -32,38 +28,37 @@ type handler interface {

var _ handler = (*fnKeyHandler)(nil)

// fnKeyHandler is a handler for the Fn key up event.
// fnKeyHandler is a handler for the fn key up event.
type fnKeyHandler struct {
// doublePressMaximumDelay is the maximum delay between two presses of the fn key for them to be considered as a
// double press.
doublePressMaximumDelay int

// primaryInputSources is a slice of the primary input sources.
primaryInputSources []string

// additionalInputSources is a slice of the additional input sources.
additionalInputSources []string

// doublePressable is a bool that indicates if the key is double pressable.
doublePressable bool

// doublePressed is a bool that indicates if the key is double pressed.
doublePressed bool

// inputSources is a slice of all available input sources.
inputSources []string

// currentInputSource is the current input source.
currentInputSource string

// previousInputSource is the previous input source.
previousInputSource string

// primaryInputSources is a slice of the primary input sources.
primaryInputSources []string

// additionalInputSources is a slice of the additional input sources.
additionalInputSources []string
}

// newFnKeyHandler returns a new fnKeyHandler.
func newFnKeyHandler() *fnKeyHandler {
return &fnKeyHandler{
inputSources: inputsource.All(),

primaryInputSources: viper.GetStringSlice("primary_input_sources"),

additionalInputSources: viper.GetStringSlice("additional_input_sources"),
doublePressMaximumDelay: intGet("double_press.maximum_delay"),
primaryInputSources: stringSliceGet("input_sources.primary"),
additionalInputSources: stringSliceGet("input_sources.additional"),
}
}

Expand Down Expand Up @@ -97,7 +92,7 @@ func (h *fnKeyHandler) setInputSource(inputSource string) {
func (h *fnKeyHandler) KeyUp() handlerFunc {
return func() {
{
doublePressTicker := time.NewTicker(doublePressThreshold * time.Millisecond)
doublePressTicker := time.NewTicker(time.Duration(h.doublePressMaximumDelay) * time.Millisecond)

h.doublePressed = h.doublePressable

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/eventhandler/rawcode.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package eventhandler encapsulates logic to handle keyboard events.
package eventhandler

// keyFn is the rawcode for the Fn key.
// keyFn is the rawcode for the fn key.
const keyFn rawCode = 179

// rawCode is a type that holds the rawcode.
Expand Down
46 changes: 46 additions & 0 deletions internal/pkg/eventhandler/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Package eventhandler encapsulates logic to handle keyboard events.
package eventhandler

import (
"fmt"

"github.com/spf13/viper"
)

// intGet returns the value of the key as int.
func intGet(k string) int {
result, ok := viper.Get(k).(int)
if !ok {
logger.Fatalw("config key type is not int", "key", k)
}

return result
}

// interfaceSliceGet returns the value of the key as []interface{}.
func interfaceSliceGet(k string) []interface{} {
result, ok := viper.Get(k).([]interface{})
if !ok {
logger.Fatalw("config key type is not []interface{}", "key", k)
}

return result
}

// stringSlicesGet returns the value of the key as []string.
func stringSliceGet(k string) []string {
val := interfaceSliceGet(k)

result := make([]string, len(val))

for i, v := range val {
cv, ok := v.(string)
if !ok {
logger.Fatalw("config key type is not string", "key", fmt.Sprintf("%s.%d", k, i))
}

result[i] = cv
}

return result
}

0 comments on commit 12a6d26

Please sign in to comment.