Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoCLI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
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
Dojo Project (HES-SO)
Projects
UI
DojoCLI
Commits
8d09a116
Verified
Commit
8d09a116
authored
1 year ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added functions to make the string composition a bit nicier. still ugly imo
parent
b005f201
No related branches found
No related tags found
1 merge request
!8
Draft: Resolve "Add bash completion helper function"
Pipeline
#29369
passed
1 year ago
Stage: code_quality
Stage: test
Changes
1
Pipelines
3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
NodeApp/src/helpers/AutoCompletionHelper.ts
+37
-33
37 additions, 33 deletions
NodeApp/src/helpers/AutoCompletionHelper.ts
with
37 additions
and
33 deletions
NodeApp/src/helpers/AutoCompletionHelper.ts
+
37
−
33
View file @
8d09a116
...
@@ -121,26 +121,21 @@ function flatten(tree: CmdNode): Array<CmdNode> {
...
@@ -121,26 +121,21 @@ function flatten(tree: CmdNode): Array<CmdNode> {
}
}
}
}
function
addWords
(
cmd
:
CmdNode
,
prefix
:
string
):
string
{
return
`
${
prefix
}
words="
${
cmd
.
children
.
map
(
c
=>
c
.
name
).
join
(
"
"
)}
--help -h"\n`
}
function
printWordsGrandParents
(
node
:
CmdNode
):
string
{
function
printWordsGrandParents
(
node
:
CmdNode
):
string
{
let
parentWords
=
""
let
parentWords
=
""
let
prefix
=
""
let
ident
=
3
if
(
node
.
parent
!==
undefined
)
{
if
(
node
.
parent
!==
undefined
)
{
parentWords
=
parentWords
+=
openCase
(
ident
,
'
grand_parent
'
)
` case "
\$
{grand_parent}" in
ident
+=
1
${
node
.
parent
}
)\n`
parentWords
+=
addLine
(
ident
,
`
${
node
.
parent
}
)`
)
prefix
=
"
"
ident
+=
1
}
}
parentWords
+=
addWords
(
node
,
prefix
)
parentWords
+=
addLine
(
ident
,
`words="
${
node
.
children
.
map
(
c
=>
c
.
name
).
join
(
"
"
)}
--help -h"`
)
ident
-=
1
parentWords
+=
addLine
(
ident
,
'
;;
'
)
if
(
node
.
parent
!==
undefined
)
{
if
(
node
.
parent
!==
undefined
)
{
parentWords
+=
ident
-=
1
` ;;
parentWords
+=
`
${
closeCase
(
ident
)}
`
*)
;;
esac\n`
}
}
return
parentWords
return
parentWords
}
}
...
@@ -164,37 +159,46 @@ function buildCmdNode(root: Command): CmdNode {
...
@@ -164,37 +159,46 @@ function buildCmdNode(root: Command): CmdNode {
return
createCmdNode
(
root
.
name
(),
root
.
commands
.
map
(
subCmd
=>
getSubCmdNode
(
subCmd
,
root
)).
concat
(
getOptions
(
root
)))
return
createCmdNode
(
root
.
name
(),
root
.
commands
.
map
(
subCmd
=>
getSubCmdNode
(
subCmd
,
root
)).
concat
(
getOptions
(
root
)))
}
}
function
openCase
(
identLevel
:
number
,
pattern
:
string
):
string
{
return
`
${
'
'
.
repeat
(
identLevel
)}
case "
\$
{
${
pattern
}
}" in\n`
}
function
closeCase
(
identLevel
:
number
):
string
{
return
`
${
'
'
.
repeat
(
identLevel
+
1
)}
*)\n`
+
`
${
'
'
.
repeat
(
identLevel
+
1
)}
;;\n`
+
`
${
'
'
.
repeat
(
identLevel
)}
esac\n`
}
function
addLine
(
identLevel
:
number
,
pattern
:
string
):
string
{
return
`
${
'
'
.
repeat
(
identLevel
)}${
pattern
}
\n`
}
export
function
getBashCompletion
(
root
:
Command
,
filename
:
string
)
{
export
function
getBashCompletion
(
root
:
Command
,
filename
:
string
)
{
const
tree
=
buildCmdNode
(
root
)
const
tree
=
buildCmdNode
(
root
)
let
data
=
let
data
=
`#/usr/bin/env bash\nfunction _dojo_completions()
`
${
addLine
(
0
,
'
#/usr/bin/env bash
\n
function _dojo_completions()
'
)}
`
{
+
`
${
addLine
(
0
,
'
{
'
)}${
addLine
(
1
,
'
latest="${COMP_WORDS[$COMP_CWORD]}"
'
)}
`
latest="
\$
{COMP_WORDS[$COMP_CWORD]}"
+
`
${
addLine
(
1
,
'
parent="${COMP_WORDS[$COMP_CWORD - 1]}"
'
)}
`
parent="
\$
{COMP_WORDS[$COMP_CWORD - 1]}"
+
`
${
addLine
(
1
,
'
grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"
'
)}
`
grand_parent="
\$
{COMP_WORDS[$COMP_CWORD - 2]}"
+
`
${
addLine
(
1
,
'
words=""
'
)}${
openCase
(
1
,
"
parent
"
)}
`
words=""
case "
\$
{parent}" in\n`
const
commands
=
Array
.
from
(
new
Set
(
flatten
(
tree
).
filter
(
cmd
=>
!
isLeaf
(
cmd
)).
map
(
cmd
=>
cmd
.
name
))).
map
(
name
=>
search
(
tree
,
name
))
const
commands
=
Array
.
from
(
new
Set
(
flatten
(
tree
).
filter
(
cmd
=>
!
isLeaf
(
cmd
)).
map
(
cmd
=>
cmd
.
name
))).
map
(
name
=>
search
(
tree
,
name
))
for
(
const
node
of
commands
)
{
for
(
const
node
of
commands
)
{
const
cmd
=
node
[
0
]
const
cmd
=
node
[
0
]
data
+=
`
${
cmd
.
name
}
)
\n
`
data
+=
addLine
(
2
,
`
${
cmd
.
name
}
)`
)
if
(
cmd
!==
undefined
&&
node
.
length
==
1
)
{
if
(
cmd
!==
undefined
&&
node
.
length
==
1
)
{
data
+=
add
Words
(
cmd
,
""
)
data
+=
add
Line
(
3
,
`words="
${
cmd
.
children
.
map
(
c
=>
c
.
name
).
join
(
"
"
)}
--help -h"`
)
}
else
if
(
cmd
!==
undefined
&&
node
.
length
>
1
)
{
}
else
if
(
cmd
!==
undefined
&&
node
.
length
>
1
)
{
node
.
forEach
(
n
=>
data
+=
printWordsGrandParents
(
n
))
node
.
forEach
(
n
=>
data
+=
printWordsGrandParents
(
n
))
}
}
data
+=
"
;;
\n
"
data
+=
addLine
(
2
,
'
;;
'
)
}
}
data
+=
data
+=
` *)
`
${
closeCase
(
1
)}
`
;;
+
`
${
addLine
(
1
,
'
COMPREPLY=($(compgen -W "$words" -- $latest))
'
)}
`
esac
+
`
${
addLine
(
1
,
'
return 0
'
)}
`
COMPREPLY=($(compgen -W "$words" -- $latest))
+
`
${
addLine
(
0
,
'
}
'
)}
`
return 0
+
`
${
addLine
(
0
,
'
complete -F _dojo_completions dojo
'
)}
`
}
complete -F _dojo_completions dojo
`
writeFileSync
(
filename
,
data
);
writeFileSync
(
filename
,
data
);
}
}
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