-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
196 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/*-packr.go | ||
/leader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.PHONY: install | ||
install: pack | ||
go install . | ||
|
||
.PHONY: pack | ||
pack: $(GOPATH)/bin/packr | ||
packr | ||
|
||
$(GOPATH)/bin/packr: | ||
go get -u github.com/gobuffalo/packr/... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
leader_widget() { | ||
local leader_exit leader_next | ||
leader_next=$(leader "$@") | ||
leader_exit=$? | ||
if [ "$leader_exit" = 3 ]; then | ||
READLINE_LINE="${READLINE_LINE}\\" | ||
READLINE_POINT=$((READLINE_POINT + 1)) | ||
return $leader_exit | ||
fi | ||
eval "$leader_next" | ||
} | ||
|
||
bind -x '"\\":leader_widget print' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/zsh | ||
|
||
() { | ||
leader_widget() { | ||
local leader_exit leader_next | ||
leader_next=$(SHELL=/bin/zsh BUFFER=$BUFFER CURSOR=$CURSOR leader print) | ||
leader_exit=$? | ||
if [ $leader_exit -eq 3 ]; then | ||
BUFFER="${BUFFER}${KEYS}" | ||
CURSOR=$((CURSOR + $#KEYS)) | ||
return "$leader_exit" | ||
fi | ||
eval "$leader_next" | ||
} | ||
|
||
zle -N leader_widget | ||
bindkey '\\' leader_widget | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
// ShellParser provides simple parsing methods to detect the lexical | ||
// context in which leader is invoked as part of an interactive shell. | ||
// | ||
// Using ShellParser, leader can detect whether it should run or not. | ||
type ShellParser struct{} | ||
|
||
// NewShellParser creates a new parser instance. | ||
func NewShellParser() *ShellParser { | ||
return &ShellParser{} | ||
} | ||
|
||
// InQuotedString returns true if cursor points to a position in line that is within a quoted string. | ||
func (p *ShellParser) InQuotedString(line string, cursor int) bool { | ||
textUpToCursor := line[:cursor] | ||
doubleQuotes := 0 | ||
singleQuotes := 0 | ||
escaped := false | ||
for _, char := range []rune(textUpToCursor) { | ||
switch char { | ||
case '"': | ||
if doubleQuotes == 0 { | ||
doubleQuotes++ | ||
} else if !escaped { | ||
doubleQuotes-- | ||
} | ||
case '\'': | ||
if singleQuotes == 0 { | ||
singleQuotes++ | ||
} else { | ||
singleQuotes-- | ||
} | ||
case '\\': | ||
escaped = !escaped | ||
} | ||
} | ||
return doubleQuotes != 0 || singleQuotes != 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dhamidi/leader" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
printfDate = `printf "%s\n" "$(date)"` | ||
escapedDoubleQuote = `printf "\"hello\"\n"` | ||
printfDateSingle = `printf '%s\n' "$(date)"` | ||
bindMixedQuotes = `bind -x '"\\":leader'` | ||
) | ||
|
||
func TestShellParser_InQuotedString_returns_true_if_cursor_is_inside_double_quotes(t *testing.T) { | ||
parser := main.NewShellParser() | ||
assert.True(t, parser.InQuotedString(printfDate, len(`printf "`))) | ||
} | ||
|
||
func TestShellParser_InQuotedString_returns_false_if_cursor_is_outside_of_double_quotes(t *testing.T) { | ||
parser := main.NewShellParser() | ||
assert.False(t, parser.InQuotedString(printfDate, len(`printf `))) | ||
} | ||
|
||
func TestShellParser_InQuotedString_returns_true_if_string_contains_escaped_double_quotes(t *testing.T) { | ||
parser := main.NewShellParser() | ||
assert.True(t, parser.InQuotedString(escapedDoubleQuote, len(`printf "\"h`))) | ||
} | ||
|
||
func TestShellParser_InQuotedString_returns_true_if_cursor_is_inside_single_quotes(t *testing.T) { | ||
parser := main.NewShellParser() | ||
assert.True(t, parser.InQuotedString(printfDateSingle, len(`printf '`))) | ||
} | ||
|
||
func TestShellParser_InQuotedString_returns_false_if_cursor_is_outside_of_single_quotes(t *testing.T) { | ||
parser := main.NewShellParser() | ||
assert.False(t, parser.InQuotedString(printfDateSingle, len(`printf `))) | ||
} | ||
|
||
func TestShellParser_InQuotedString_returns_true_if_cursor_is_inside_nested_quotes(t *testing.T) { | ||
parser := main.NewShellParser() | ||
assert.True(t, parser.InQuotedString(bindMixedQuotes, len(`bind -x '"\`))) | ||
} |