Skip to content

Commit

Permalink
refactor: restart implementing completer using shargs lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yord committed Jun 13, 2020
1 parent 05eb304 commit 795b8c1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 71 deletions.
3 changes: 0 additions & 3 deletions src/repl/completer/getMatches.d.ts

This file was deleted.

29 changes: 0 additions & 29 deletions src/repl/completer/getMatches.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/repl/completer/getMatches.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/repl/completer/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Err, Opt} from '../../index'

export const completer: <A, B>(parser: (opt?: Opt) => (any?: A) => {errs: Err[], any: B}, commands: Opt) =>
export const completer: <A, B>(lexer: (opt?: Opt) => (any?: A) => {errs: Err[], opts: B}, commands: Opt, opts?: {only?: boolean}) =>
(line: string) =>
string[]
42 changes: 24 additions & 18 deletions src/repl/completer/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import {getMatches} from './getMatches'
import {flatMap} from './flatMap'
const {flatMap} = require('./flatMap')

const completer = (parser, commands) => {
const parse = parser(commands)
const completer = (lexer, cmd, {only = false} = {only: false}) => {
const lex = lexer(cmd)

return line => {
if (line === '') {
const args = flatMap(commands.opts, cmd => cmd.args)
return [args, '']
}

const {args} = parse(line)

const {_, ...subcommands} = args
const {opts} = lex(line)

let matches = getMatches(commands.opts, subcommands, _)

if (line[line.length - 1] !== ' ' && matches.length === 1) {
matches = matches.map(m => ' ' + m)
}
const values = justValues(opts)

return [matches, '']
return [[], line]
}
}

module.exports = {
completer
}

function justValues (opts) {
return flatMap(opts, opt => {
if (Array.isArray(opt.values)) {
if (isSubcommand(opt)) {
return [{...opt, values: justValues(opt.values)}]
} else {
return [opt]
}
} else {
return []
}
})
}

function isSubcommand ({opts} = {opts: undefined}) {
return Array.isArray(opts)
}
Empty file removed src/repl/completer/index.test.js
Empty file.
2 changes: 1 addition & 1 deletion src/repl/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export interface Opt {
[key: string]: any
}

export const repl: (parser: (opt?: Opt) => (any?: A) => {errs: Err[], any: B}, commands: Opt) => void
export const repl: (parser: (opt?: Opt) => (any?: A) => {errs: Err[], args: B}, commands: Opt) => void
4 changes: 2 additions & 2 deletions src/repl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const nodeRepl = require('repl')
const {replEval} = require('./replEval')
const {completer} = require('./completer')

const repl = (parser, commands) => {
const repl = (lexer, parser, commands, {only = false} = {only: false}) => {
console.log(commands.desc ? commands.desc + '\n' : '')

nodeRepl.start({
prompt: `${commands.key}~$ `,
ignoreUndefined: true,
completer: completer(parser, commands)
eval: replEval(parser, commands),
completer: completer(lexer, commands, {only})
})
}

Expand Down

0 comments on commit 795b8c1

Please sign in to comment.