Skip to content

Commit

Permalink
feat: autocompletion with omelette
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu4k committed May 24, 2020
1 parent 2b8baa0 commit 9ab4fb4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"helloworld",
"importmap",
"oneliner",
"stdlib"
"pcopy",
"stdlib",
"velociraptor"
]
}
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ to see what else you can do with deno CLI use the help flag:
$ denon --help
```

## Autocompletion

In **zsh**, you can install autocompletion with:

```bash
echo '. <(denon --completion)' >> ~/.zshrc
```

In **bash**:

```bash
denon --completion >> ~/.config/denon.completion.sh
echo 'source ~/.config/denon.completion.sh' >> ~/.bash_profile
```

In **fish**:

```bash
echo 'denon --completion-fish | source' >> ~/.config/fish/config.fish
```

## Configuration

denon is designed to be simple but also extremely configurable to fit your project needs. It supports both json and yaml for the configuration file. The configuration options in yaml is the same as json making it compatible.
Expand Down
3 changes: 3 additions & 0 deletions denon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
initializeConfig,
grantPermissions,
upgrade,
autocomplete,
} from "./src/cli.ts";
import { readConfig, DenonConfig } from "./src/config.ts";
import { parseArgs } from "./src/args.ts";
Expand Down Expand Up @@ -100,6 +101,8 @@ if (import.meta.main) {
const config = await readConfig(args.config);
await setupLog(config);

autocomplete(config);

config.args = args;

// show help message.
Expand Down
3 changes: 3 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ export { deferred, delay } from "https://deno.land/std@0.52.0/async/mod.ts";

// permission management
export { grant } from "https://deno.land/std@0.52.0/permissions/mod.ts";

// autocomplete
export * as omelette from "https://deno.land/x/omelette/omelette.ts";
20 changes: 20 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
setColorEnabled,
grant,
exists,
omelette,
} from "../deps.ts";

import { DenonConfig, writeConfig, getConfigFilename } from "./config.ts";
Expand Down Expand Up @@ -96,6 +97,25 @@ export async function upgrade() {
Deno.exit(0);
}

/**
* Generate autocomplete suggestions
*/
export function autocomplete(config: DenonConfig) {
// Write your CLI template.
const completion = omelette.default(`denon <script>`);

// Bind events for every template part.
completion.on('script', function ({ reply }: { reply: Function}) {
const auto = Object.keys(config.scripts);
for (const file of Deno.readDirSync(Deno.cwd())) {
auto.push(file.name);
}
reply(auto);
});

completion.init();
}

/**
* List all available scripts declared in the config file.
* // TODO: make it interactive
Expand Down

0 comments on commit 9ab4fb4

Please sign in to comment.