gpt4 book ai didi

bash - 更改 bash 中内置读取的制表符补全

转载 作者:行者123 更新时间:2023-11-29 09:27:43 25 4
gpt4 key购买 nike

当“read -e”在 bash 中处于事件状态时,当前的制表符补全似乎只匹配文件名:

read -e
[[TabTab]]
abc.txt bcd.txt cde.txt

我希望补全是我定义的一组字符串,而文件/目录/主机名补全等应该在“read -e”期间停用。

在脚本之外

complete -W 'string1 string2 string3' -E

效果很好,但我无法在使用“read -e”时在脚本中完成这种完成工作。

最佳答案

虽然这似乎是一个合理的要求,但我认为这是不可能的。

在调用 readline 处理 -e 输入之前,read 内置函数的现有实现将 readline 完成环境设置为相当基本的配置。

您可以在builtins/read.def中查看代码, 在 edit_line function :它设置了rl_attempted_completion_function在调用 readline 期间为 NULLreadline 有几个完成覆盖,所以这不是 100% 明显重置整个完成环境,但据我所知,这是用于根据 实现可编程完成的函数完成命令。

通过一些工作,您可能可以修改 read 命令的定义,以允许使用特定的完成函数来代替 readline 标准文件名完成函数或作为其补充。这需要对 bash 内部结构有一定的了解,但如果您想熟悉这些内部结构,这将是一个合理的项目。

作为一种更简单但效率较低的替代方法,您可以编写自己的小实用程序,它只接受一行带有 readline 的键盘输入并将其回显到标准输出。然后调用 read 将其标准输入重定向到您的实用程序:

read -r < <(my_reader string1 string2 string3)

(假定 my_reader 使用其命令行参数为 readline 库构造潜在的完成列表。您可能希望该选项显示提示还有。)

readline 文档包括 an example of an application做简单的自定义完成;一旦您从 K&R 函数原型(prototype)语法翻译它,它可能很容易适应您的需求。


编辑: 再次查看那个例子后,我认为它有很多不必要的细节,所以我写了下面的例子,减少了不必要的细节。我可能会把它上传到 github,但现在它就在这里,尽管它有将近 100 行:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <readline/readline.h>

static void version(const char* progname) {
fprintf(stderr, "%s 0.1\n", progname);
}
static void usage(const char* progname) {
fprintf(stderr, "Usage: %s [-fhv] [-p PROMPT] [-n PROGNAME] [COMPLETION...]\n", progname);
fprintf(stderr,
"Reads one line using readline, and prints it to stdout.\n"
"Returns success if a line was read.\n"
" -p PROMPT Output PROMPT before requesting input.\n"
" -n PROGNAME Set application name to PROGNAME for readline config file\n"
" (Default: %s).\n"
" -f Use filename completion as well as specified completions.\n"
" -h Print this help text and exit.\n"
" -v Print version number and exit.\n"
" COMPLETION word to add to the list of possible completions.\n",
progname);
}

/* Readline really likes globals, so none of its hooks take a context parameter. */
static char** completions = NULL;
static char* generate_next_completion(const char* text, int state) {
static int index = 0;
if (state == 0) index = 0; /* reset index if we're starting */
size_t textlen = strlen(text);
while (completions[index++])
if (strncmp(completions[index - 1], text, textlen) == 0)
return strdup(completions[index - 1]);
return NULL;
}

/* We use this if we will fall back to filename completion */
static char** generate_completions(const char* text, int start, int end) {
return rl_completion_matches(text, generate_next_completion);
}

int main (int argc, char **argv) {
const char* prompt = "";
const char* progname = strrchr(argv[0], '/');
progname = progname ? progname + 1 : argv[0];
rl_readline_name = progname;

bool use_file_completion = false;

for (;;) {
int opt = getopt(argc, argv, "+fp:n:hv");
switch (opt) {
case -1: break;
case 'f': use_file_completion = true; continue;
case 'p': prompt = optarg; continue;
case 'n': rl_readline_name = optarg; continue;
case 'h': usage(progname); return 0;
case 'v': version(progname); return 0;
default: usage(progname); return 2;
}
break;
}

/* The default is stdout, which would interfere with capturing output. */
rl_outstream = stderr;

completions = argv + optind;
rl_completion_entry_function = rl_filename_completion_function;
if (*completions) {
if (use_file_completion)
rl_attempted_completion_function = generate_completions;
else
rl_completion_entry_function = generate_next_completion;
} else {
/* No specified strings */
if (!use_file_completion)
rl_inhibit_completion = true;
}

char* line = readline(prompt);

if (line) {
puts(line);
free(line);
return 0;
} else {
fputc('\n', rl_outstream);
return 1;
}
}

关于bash - 更改 bash 中内置读取的制表符补全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34660459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com