This commit is contained in:
parent
88556fbff3
commit
758bcde8a7
@ -24,8 +24,8 @@ namespace dropshelltool
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr const char *BLOCK_START = "#---DROPSHELL-TOOL-START---";
|
||||
static constexpr const char *BLOCK_END = "#---DROPSHELL-TOOL-END---";
|
||||
static constexpr const char *BLOCK_START = "#---GETPKG-TOOL-START---";
|
||||
static constexpr const char *BLOCK_END = "#--GETPKG-TOOL-END---";
|
||||
|
||||
BashrcEditor::BashrcEditor()
|
||||
{
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
namespace dropshelltool {
|
||||
extern const std::filesystem::path DROPSHELL_RC_PATH;
|
||||
@ -72,19 +73,23 @@ void DropshellScriptManager::addAutocomplete(const std::string& toolName) {
|
||||
std::ifstream infile(dropshelltool::DROPSHELL_RC_PATH);
|
||||
std::vector<std::string> lines;
|
||||
std::string line;
|
||||
|
||||
std::string funcName = "_" + toolName + "_autocomplete";
|
||||
std::string blockStart = "# DROPSHELL-AUTOCOMPLETE-START: " + toolName;
|
||||
std::string blockEnd = "# DROPSHELL-AUTOCOMPLETE-END: " + toolName;
|
||||
std::string funcDef;
|
||||
funcDef += blockStart + "\n";
|
||||
funcDef += funcName + "() {\n";
|
||||
funcDef += " local cur=\"${COMP_WORDS[COMP_CWORD]}\"\n";
|
||||
funcDef += " mapfile -t completions < <(" + toolName + " autocomplete \"${COMP_WORDS[@]:1:${COMP_CWORD}-1}\")\n";
|
||||
funcDef += " mapfile -t COMPREPLY < <(compgen -W \"${completions[*]}\" -- \"$cur\")\n";
|
||||
funcDef += "}\n";
|
||||
funcDef += blockEnd + "\n";
|
||||
std::string completeLine = "complete -F " + funcName + " " + toolName + " # dropshell-autocomplete:" + toolName;
|
||||
bool blockFound = false, completeFound = false;
|
||||
|
||||
std::string blockStart = "# GETPKG-AUTOCOMPLETE-START: " + toolName;
|
||||
std::string blockEnd = "# GETPKG-AUTOCOMPLETE-END: " + toolName;
|
||||
|
||||
std::vector<std::string> funcLines;
|
||||
funcLines.push_back(blockStart);
|
||||
funcLines.push_back(funcName + "() {");
|
||||
funcLines.push_back(" local cur=\"${COMP_WORDS[COMP_CWORD]}\"");
|
||||
funcLines.push_back(" mapfile -t completions < <(" + toolName + " autocomplete \"${COMP_WORDS[@]:1:${COMP_CWORD}-1}\")");
|
||||
funcLines.push_back(" mapfile -t COMPREPLY < <(compgen -W \"${completions[*]}\" -- \"$cur\")");
|
||||
funcLines.push_back("}");
|
||||
funcLines.push_back(blockEnd);
|
||||
|
||||
std::string completeLine = "complete -F " + funcName + " " + toolName + " # autocomplete: " + toolName;
|
||||
bool blockFound = false;
|
||||
bool inBlock = false;
|
||||
while (std::getline(infile, line)) {
|
||||
if (!inBlock && trim(line) == blockStart) {
|
||||
@ -95,20 +100,23 @@ void DropshellScriptManager::addAutocomplete(const std::string& toolName) {
|
||||
if (inBlock) {
|
||||
if (trim(line) == blockEnd) {
|
||||
inBlock = false;
|
||||
lines.push_back(funcDef); // Add new function block
|
||||
// Add all function lines
|
||||
for (const auto& funcLine : funcLines) {
|
||||
lines.push_back(funcLine);
|
||||
}
|
||||
}
|
||||
continue; // Skip all lines in the old block
|
||||
}
|
||||
if (line.find("# dropshell-autocomplete:" + toolName) != std::string::npos) {
|
||||
completeFound = true;
|
||||
lines.push_back(completeLine);
|
||||
} else {
|
||||
else
|
||||
lines.push_back(line);
|
||||
}
|
||||
}
|
||||
infile.close();
|
||||
if (!blockFound) lines.push_back(funcDef);
|
||||
if (!completeFound) lines.push_back(completeLine);
|
||||
if (!blockFound) {
|
||||
// Add all function lines
|
||||
for (const auto& funcLine : funcLines) {
|
||||
lines.push_back(funcLine);
|
||||
}
|
||||
}
|
||||
std::ofstream outfile(dropshelltool::DROPSHELL_RC_PATH, std::ios::trunc);
|
||||
for (const auto& l : lines) outfile << l << "\n";
|
||||
outfile.close();
|
||||
|
Loading…
x
Reference in New Issue
Block a user