Skip to content

Commit

Permalink
Merge pull request #77 from reeflective/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
maxlandon authored Feb 11, 2025
2 parents af875b5 + e276e41 commit 41f649b
Show file tree
Hide file tree
Showing 38 changed files with 377 additions and 135 deletions.
46 changes: 0 additions & 46 deletions .github/labeler.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ jobs:
run: go build -v ./...
shell: powershell

- name: Run coverage
run: go test -v ./...
shell: powershell
# - name: Run coverage
# run: go test -v ./...
# shell: powershell
22 changes: 0 additions & 22 deletions .github/workflows/label.yml

This file was deleted.

3 changes: 3 additions & 0 deletions inputrc/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func DefaultVars() map[string]interface{} {
"match-hidden-files": true,
"menu-complete-display-prefix": false,
"meta-flag": false,
"multiline-column": false,
"multiline-column-numbered": false,
"multiline-column-custom": "",
"output-meta": false,
"page-completions": true,
"prefer-visible-bell": true,
Expand Down
9 changes: 9 additions & 0 deletions inputrc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewDefaultConfig(opts ...ConfigOption) *Config {
for _, o := range opts {
o(cfg)
}

return cfg
}

Expand All @@ -54,6 +55,7 @@ func (cfg *Config) ReadFile(name string) ([]byte, error) {
if cfg.ReadFileFunc != nil {
return cfg.ReadFileFunc(name)
}

return nil, os.ErrNotExist
}

Expand All @@ -62,9 +64,11 @@ func (cfg *Config) Do(name, value string) error {
if f, ok := cfg.Funcs[name]; ok {
return f(name, value)
}

if f, ok := cfg.Funcs[""]; ok {
return f(name, value)
}

return nil
}

Expand All @@ -84,10 +88,12 @@ func (cfg *Config) Bind(keymap, sequence, action string, macro bool) error {
if cfg.Binds[keymap] == nil {
cfg.Binds[keymap] = make(map[string]Bind)
}

cfg.Binds[keymap][sequence] = Bind{
Action: action,
Macro: macro,
}

return nil
}

Expand All @@ -98,6 +104,7 @@ func (cfg *Config) GetString(name string) string {
return s
}
}

return ""
}

Expand All @@ -108,6 +115,7 @@ func (cfg *Config) GetInt(name string) int {
return i
}
}

return 0
}

Expand All @@ -118,6 +126,7 @@ func (cfg *Config) GetBool(name string) bool {
return b
}
}

return false
}

Expand Down
6 changes: 6 additions & 0 deletions inputrc/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !windows
// +build !windows

package inputrc

const delimiter = "####----####\n"
6 changes: 6 additions & 0 deletions inputrc/constants_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build windows
// +build windows

package inputrc

const delimiter = "####----####\n"
3 changes: 3 additions & 0 deletions inputrc/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func Example() {
if err != nil {
panic(err)
}

cfg := inputrc.NewDefaultConfig()
if err := inputrc.UserDefault(u, cfg, inputrc.WithApp("bash")); err != nil {
panic(err)
Expand All @@ -30,10 +31,12 @@ $if Usql
$endif
`

cfg := inputrc.NewDefaultConfig()
if err := inputrc.Parse(strings.NewReader(example), cfg, inputrc.WithApp("usql")); err != nil {
panic(err)
}

fmt.Println("editing mode:", cfg.GetString("editing-mode"))
fmt.Println("vi-insert:")
fmt.Printf(" %s: %s\n", inputrc.Escape(string(inputrc.Return)), cfg.Binds["vi-insert"][string(inputrc.Return)].Action)
Expand Down
13 changes: 13 additions & 0 deletions inputrc/inputrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func ParseFile(name string, h Handler, opts ...Option) error {
if err != nil {
return err
}

defer f.Close()

return New(append(opts, WithName(name))...).Parse(f, h)
}

Expand All @@ -43,27 +45,33 @@ func UserDefault(u *user.User, cfg *Config, opts ...Option) error {
if name := os.Getenv("INPUTRC"); name != "" {
files = append(files, name)
}

if u != nil {
name := ".inputrc"
if runtime.GOOS == "windows" {
name = "_inputrc"
}

files = append(files, filepath.Join(u.HomeDir, name))
}

if runtime.GOOS != "windows" {
files = append(files, "/etc/inputrc")
}
// load first available file
for _, name := range files {
buf, err := cfg.ReadFile(name)

switch {
case err != nil && errors.Is(err, os.ErrNotExist):
continue
case err != nil:
return err
}

return ParseBytes(buf, cfg, append(opts, WithName(name))...)
}

return nil
}

Expand Down Expand Up @@ -92,6 +100,7 @@ func EscapeMacro(s string) string {
// escape escapes s using m.
func escape(s string, m map[rune]string) string {
var v []string

for _, c := range s {
switch c {
case Alert:
Expand Down Expand Up @@ -120,18 +129,22 @@ func escape(s string, m map[rune]string) string {
s += `\C-`
c = Decontrol(c)
}

if IsMeta(c) {
s += `\M-`
c = Demeta(c)
}

if unicode.IsPrint(c) {
s += string(c)
} else {
s += fmt.Sprintf(`\x%2x`, c)
}

v = append(v, s)
}
}

return strings.Join(v, "")
}

Expand Down
Loading

0 comments on commit 41f649b

Please sign in to comment.