Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bulletin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Personal Projects
bulletin
Commits
f93c53c5
Unverified
Commit
f93c53c5
authored
2 months ago
by
Kevin Lyda
Browse files
Options
Downloads
Patches
Plain Diff
Add completer
parent
0dd6536b
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
dclish/completer.go
+81
-0
81 additions, 0 deletions
dclish/completer.go
dclish/dclish.go
+5
-0
5 additions, 0 deletions
dclish/dclish.go
repl/repl.go
+5
-3
5 additions, 3 deletions
repl/repl.go
with
91 additions
and
3 deletions
dclish/completer.go
0 → 100644
+
81
−
0
View file @
f93c53c5
package
dclish
import
(
"strings"
"unicode"
)
// Completer command completer type.
type
Completer
struct
{
commands
[]
string
flags
map
[
string
][]
string
}
// NewCompleter creates a new completer.
func
NewCompleter
(
commands
Commands
)
Completer
{
comps
:=
[]
string
{}
flags
:=
map
[
string
][]
string
{}
for
c
:=
range
commands
{
comps
=
append
(
comps
,
c
)
if
len
(
commands
[
c
]
.
Commands
)
>
0
{
subcommands
:=
commands
[
c
]
.
Commands
for
subc
:=
range
subcommands
{
fullc
:=
c
+
" "
+
subc
comps
=
append
(
comps
,
fullc
)
flags
[
fullc
]
=
[]
string
{}
if
subcommands
[
subc
]
.
Flags
!=
nil
{
for
f
:=
range
subcommands
[
subc
]
.
Flags
{
flags
[
fullc
]
=
append
(
flags
[
fullc
],
f
)
}
}
}
}
flags
[
c
]
=
[]
string
{}
if
commands
[
c
]
.
Flags
!=
nil
{
for
f
:=
range
commands
[
c
]
.
Flags
{
flags
[
c
]
=
append
(
flags
[
c
],
f
)
}
}
}
return
Completer
{
commands
:
comps
,
flags
:
flags
,
}
}
// Do return a list of possible completions.
func
(
c
Completer
)
Do
(
line
[]
rune
,
pos
int
)
([][]
rune
,
int
)
{
// Nothing typed in.
if
pos
==
0
{
newline
:=
make
([][]
rune
,
len
(
c
.
commands
))
for
i
:=
range
c
.
commands
{
newline
[
i
]
=
[]
rune
(
c
.
commands
[
i
])
}
return
newline
,
pos
}
// Command partially typed in.
newline
:=
[][]
rune
{}
cmd
:=
strings
.
ToUpper
(
string
(
line
[
0
:
pos
]))
lower
:=
false
if
unicode
.
IsLower
(
line
[
0
])
{
lower
=
true
}
for
i
:=
range
c
.
commands
{
if
strings
.
HasPrefix
(
c
.
commands
[
i
],
cmd
)
{
rest
:=
strings
.
Replace
(
c
.
commands
[
i
],
cmd
,
""
,
1
)
if
lower
{
newline
=
append
(
newline
,
[]
rune
(
strings
.
ToLower
(
rest
)))
}
else
{
newline
=
append
(
newline
,
[]
rune
(
rest
))
}
}
}
if
len
(
newline
)
>
0
{
return
newline
,
pos
}
// Command completely typed in.
// TODO: figure out flags.
return
newline
,
pos
}
This diff is collapsed.
Click to expand it.
dclish/dclish.go
+
5
−
0
View file @
f93c53c5
...
@@ -10,6 +10,10 @@ import (
...
@@ -10,6 +10,10 @@ import (
// ActionFunc is the function that a command runs.
// ActionFunc is the function that a command runs.
type
ActionFunc
func
(
*
Command
)
error
type
ActionFunc
func
(
*
Command
)
error
// CompleterFunc is a function to provide completions for arguments for
// a given command.
type
CompleterFunc
func
()
[]
string
// Flag is a flag for a command.
// Flag is a flag for a command.
type
Flag
struct
{
type
Flag
struct
{
OptArg
bool
OptArg
bool
...
@@ -30,6 +34,7 @@ type Command struct {
...
@@ -30,6 +34,7 @@ type Command struct {
MinArgs
int
MinArgs
int
Commands
Commands
Commands
Commands
Action
ActionFunc
Action
ActionFunc
Completer
CompleterFunc
Description
string
Description
string
}
}
...
...
This diff is collapsed.
Click to expand it.
repl/repl.go
+
5
−
3
View file @
f93c53c5
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"strings"
"strings"
"unicode"
"unicode"
"git.lyda.ie/kevin/bulletin/dclish"
"git.lyda.ie/kevin/bulletin/this"
"git.lyda.ie/kevin/bulletin/this"
"github.com/adrg/xdg"
"github.com/adrg/xdg"
"github.com/chzyer/readline"
"github.com/chzyer/readline"
...
@@ -15,6 +16,7 @@ import (
...
@@ -15,6 +16,7 @@ import (
// Loop is the main event loop.
// Loop is the main event loop.
func
Loop
()
error
{
func
Loop
()
error
{
completer
:=
dclish
.
NewCompleter
(
commands
)
histdir
:=
path
.
Join
(
xdg
.
ConfigHome
,
"BULLETIN"
)
histdir
:=
path
.
Join
(
xdg
.
ConfigHome
,
"BULLETIN"
)
os
.
MkdirAll
(
histdir
,
0700
)
os
.
MkdirAll
(
histdir
,
0700
)
histfile
:=
path
.
Join
(
histdir
,
fmt
.
Sprintf
(
"%s.history"
,
this
.
User
.
Login
))
histfile
:=
path
.
Join
(
histdir
,
fmt
.
Sprintf
(
"%s.history"
,
this
.
User
.
Login
))
...
@@ -22,7 +24,7 @@ func Loop() error {
...
@@ -22,7 +24,7 @@ func Loop() error {
&
readline
.
Config
{
&
readline
.
Config
{
Prompt
:
"BULLETIN> "
,
Prompt
:
"BULLETIN> "
,
HistoryFile
:
histfile
,
HistoryFile
:
histfile
,
// TODO:
AutoComplete: completer,
AutoComplete
:
completer
,
InterruptPrompt
:
"^C"
,
InterruptPrompt
:
"^C"
,
EOFPrompt
:
"EXIT"
,
EOFPrompt
:
"EXIT"
,
HistorySearchFold
:
true
,
HistorySearchFold
:
true
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment