Skip to content

Commit

Permalink
Add continuous evaluation to key binding table
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed Jun 9, 2018
1 parent 54efd07 commit 0de2cbe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
18 changes: 12 additions & 6 deletions key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
PreviousConflict = "previous"
QuitApplication = "quit"
ShowHelp = "help"
ContinuousEvaluation = "cont_eval"
)

// defaultBinding is used when the user has not specified any of the
Expand All @@ -52,6 +53,7 @@ var defaultBinding = Binding{
PreviousConflict: "p",
QuitApplication: "q",
ShowHelp: "h",
ContinuousEvaluation: "false",
}

// LoadSettings looks for a user specified key-binding settings file - `$HOME/.fac.yml`
Expand Down Expand Up @@ -118,22 +120,20 @@ func (b Binding) consolidate() {

if !ok || userValue == "" {
b[key] = defaultValue
} else if len(userValue) > 1 {
} else if len(userValue) > 1 && userValue != "false" && userValue != "true" {
b[key] = string(userValue[0])
}
}
}

// verify looks through the user's key-binding settings and looks for any infractions such as..
// 1. Invalid/ignored key-binding keys
// 2. Multi-character key-mappings
// 2. Multi-character key-mappings (except for `cont_eval`)
// 3. Duplicate key-mappings
func (b Binding) verify() (warnings []string, fatals []string) {
bindTable := map[string][]string{}

for k, v := range b {
bindTable[string(v[0])] = append(bindTable[string(v[0])], k)

// Check for "1. Invalid/ignored key-binding keys"
if _, ok := defaultBinding[k]; !ok {
warnings = append(warnings, fmt.Sprintf("Invalid key: \"%s\" will be ignored", k))
Expand All @@ -142,9 +142,15 @@ func (b Binding) verify() (warnings []string, fatals []string) {
}

// Check for "2. Multi-character key-mappings"
if len(v) > 1 {
warnings = append(warnings, fmt.Sprintf("Illegal multi-character mapping: \"%s\" will be interpreted as '%s'", v, string(v[0])))
if k == ContinuousEvaluation && v != "false" && v != "true" {
fatals = append(fatals, fmt.Sprintf("Invalid value: value for key '%s' must either be true or false", ContinuousEvaluation))
continue
} else if len(v) > 1 && k != ContinuousEvaluation {
abbreviated := string(v[0])
warnings = append(warnings, fmt.Sprintf("Illegal multi-character mapping: \"%s\" will be interpreted as '%s'", v, abbreviated))
bindTable[abbreviated] = append(bindTable[abbreviated], k)
} else {
bindTable[v] = append(bindTable[v], k)
}
}

Expand Down
18 changes: 14 additions & 4 deletions key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,27 @@ var tests = []struct {
SelectLocal: "i",
SelectIncoming: "incoming",
},
expected: Binding{
SelectLocal: "l",
SelectIncoming: "i",
},
expected: nil,
warnings: []string{
"Illegal multi-character mapping: \"incoming\" will be interpreted as 'i'",
},
fatals: []string{
"Duplicate key-mapping: \"select_incoming, select_local\" are all represented by 'i'",
},
},
{
settings: Binding{
ContinuousEvaluation: "kinda",
SelectIncoming: "i",
SelectLocal: "i",
},
expected: nil,
warnings: nil,
fatals: []string{
"Invalid value: value for key 'cont_eval' must either be true or false",
"Duplicate key-mapping: \"select_incoming, select_local\" are all represented by 'i'",
},
},
{
settings: Binding{
ShowLinesDown: "d",
Expand Down

0 comments on commit 0de2cbe

Please sign in to comment.