Skip to content

Commit

Permalink
👍 Add ++color option on Gin command
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Mar 20, 2022
1 parent 78295b3 commit 5b8a073
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
32 changes: 20 additions & 12 deletions denops/gin/core/bare/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import {
option,
unknownutil,
} from "../../deps.ts";
import {
builtinOpts,
formatOpts,
parseOpts,
validateOpts,
} from "../../util/args.ts";
import { builtinOpts, parseOpts, validateOpts } from "../../util/args.ts";
import { normCmdArgs } from "../../util/cmd.ts";
import * as buffer from "../../util/buffer.ts";
import * as decoration from "../../util/decoration.ts";
import { getWorktreeFromOpts } from "../../util/worktree.ts";
import { decodeUtf8 } from "../../util/text.ts";
import { run } from "../../git/process.ts";
Expand All @@ -38,10 +34,16 @@ export async function command(
validateOpts(opts, [
"worktree",
"buffer",
"color",
...builtinOpts,
]);
const hasColor = "color" in opts;
const cmd = [
...(hasColor ? ["-c", "color.ui=always"] : []),
...residue,
];
const worktree = await getWorktreeFromOpts(denops, opts);
const proc = run(residue, {
const proc = run(cmd, {
printCommand: !!verbose,
stdin: "null",
stdout: "piped",
Expand All @@ -57,19 +59,25 @@ export async function command(
]);
proc.close();
if ("buffer" in opts) {
const cmdarg = formatOpts(opts, builtinOpts).join(" ");
const decoder = new TextDecoder(opts["enc"] ?? opts["encoding"] ?? "utf8");
const content = decoder.decode(new Uint8Array([...stdout, ...stderr]))
.split("\n");
const [trimmed, decorations] = hasColor
? decoration.buildDecorationsFromAnsiEscapeCode(content)
: [content, []];
await denops.cmd("noswapfile enew");
const bufnr = await fn.bufnr(denops);
await buffer.ensure(denops, bufnr, async () => {
await batch.batch(denops, async (denops) => {
await option.modifiable.setLocal(denops, false);
});
await buffer.editData(denops, new Uint8Array([...stdout, ...stderr]), {
mods: "silent keepalt keepjumps",
cmdarg,
});
await buffer.replace(denops, bufnr, trimmed);
await decoration.decorate(denops, bufnr, decorations);
});
await buffer.concrete(denops, bufnr);
if (denops.meta.host === "vim") {
await denops.cmd("redraw");
}
} else {
if (status.success) {
await helper.echo(denops, decodeUtf8(stdout) + decodeUtf8(stderr));
Expand Down
4 changes: 3 additions & 1 deletion doc/gin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ COMMANDS *gin-commands*

*:Gin*
:Gin[!] [++worktree={worktree}] {command} [{args}]
:Gin[!] [++opt] [++worktree={worktree}] ++buffer {command} [{args}]
:Gin[!] [++worktree={worktree}] ++buffer [++color] {command} [{args}]
Call a git raw {command} with {args} asynchronously and echo result on
a {worktree}.
For example, "Gin add %" on "hello.txt" invokes "git add hello.txt".
Expand All @@ -103,6 +103,8 @@ COMMANDS *gin-commands*
working directory.
If ++buffer option is specified, it opens a |scratch-buffer| to show
the result of the command.
If ++color option is specified, the result buffer is highlighted by
ANSI escape code of the result of the command.

Note that users need to install guise.vim (|gin-guise|) to open a
|tabpage| to edit a commit message on "Gin commit" or so on.
Expand Down

0 comments on commit 5b8a073

Please sign in to comment.