diff --git a/research/XPlat/run.cmd b/research/XPlat/run.cmd index 712d81c..8d7b118 100755 --- a/research/XPlat/run.cmd +++ b/research/XPlat/run.cmd @@ -1,20 +1,28 @@ #!/bin/csh -echo off > /dev/null +echo off > nul +echo ================================================= echo Welcome to a cross-platform script +echo ================================================= goto do_windows do_windows: +rm nul [ `uname` = "Interix" ] && goto do_win32unix [ `uname` = "Darwin" ] && goto do_mac [ `uname` = "Linux" ] && goto do_linux do_win32unix: echo Environment is SFU Win32 Unix - exit + goto end do_mac: echo Environment is MacOSX - exit + goto end do_linux: echo Environment is Linux - exit + goto end :do_windows echo Environment is Windows + goto end +end: + alias ':end' "" +:end + echo Done diff --git a/research/XPlat/run.cmd b/research/XPlat/run.cmd index 712d81c..8d7b118 100755 --- a/research/XPlat/run.cmd +++ b/research/XPlat/run.cmd @@ -1,20 +1,28 @@ #!/bin/csh -echo off > /dev/null +echo off > nul +echo ================================================= echo Welcome to a cross-platform script +echo ================================================= goto do_windows do_windows: +rm nul [ `uname` = "Interix" ] && goto do_win32unix [ `uname` = "Darwin" ] && goto do_mac [ `uname` = "Linux" ] && goto do_linux do_win32unix: echo Environment is SFU Win32 Unix - exit + goto end do_mac: echo Environment is MacOSX - exit + goto end do_linux: echo Environment is Linux - exit + goto end :do_windows echo Environment is Windows + goto end +end: + alias ':end' "" +:end + echo Done diff --git a/research/mini-shell/completions.cpp b/research/mini-shell/completions.cpp index b3b45de..989ab63 100644 --- a/research/mini-shell/completions.cpp +++ b/research/mini-shell/completions.cpp @@ -57,6 +57,40 @@ } +CommandTabCompletions::CommandTabCompletions(const std::vector& cmdList) + : commandList(cmdList) +{ +} + + +std::vector + CommandTabCompletions::getCompletions(const std::string& contextBeforeText, const std::string& partialText) +{ + std::vector matches; + for (int i = 0; i < commandList.size(); i++) + { + std::string cmd = commandList[i]; + if (strncmp(partialText.c_str(), cmd.c_str(), partialText.size()) == 0) + { + Completion completion; + completion.shortDisplayText = cmd; + completion.completionText = cmd; + matches.push_back(completion); + } + } + return matches; +} + + +void CommandTabCompletions::showPossibleCompletions(const std::vector& completions) +{ + printf("\n"); + for (int i = 0; i < completions.size(); i++) + printf("%s ", completions[i].shortDisplayText.c_str()); + printf("\n"); +} + + FileTabCompletions::FileTabCompletions(FileSystem& fileSys) : fs(fileSys) { @@ -101,8 +135,9 @@ } -ReadLineWithCompletions::ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp) +ReadLineWithCompletions::ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp_arg0, CompletionsProviderInterface& cp) : Parent(text) + , commandCompletions(cp_arg0) , completions(cp) { } @@ -125,7 +160,12 @@ lstArg = args.back(); //printf("lst arg: -%s-\n", lstArg.c_str()); - std::vector matches = completions.getCompletions(curText, lstArg); + std::vector matches; + + if (args.size() == 1) + matches = commandCompletions.getCompletions(curText, lstArg); + else + matches = completions.getCompletions(curText, lstArg); int commonLength = 0; if (matches.size() >= 1) { diff --git a/research/XPlat/run.cmd b/research/XPlat/run.cmd index 712d81c..8d7b118 100755 --- a/research/XPlat/run.cmd +++ b/research/XPlat/run.cmd @@ -1,20 +1,28 @@ #!/bin/csh -echo off > /dev/null +echo off > nul +echo ================================================= echo Welcome to a cross-platform script +echo ================================================= goto do_windows do_windows: +rm nul [ `uname` = "Interix" ] && goto do_win32unix [ `uname` = "Darwin" ] && goto do_mac [ `uname` = "Linux" ] && goto do_linux do_win32unix: echo Environment is SFU Win32 Unix - exit + goto end do_mac: echo Environment is MacOSX - exit + goto end do_linux: echo Environment is Linux - exit + goto end :do_windows echo Environment is Windows + goto end +end: + alias ':end' "" +:end + echo Done diff --git a/research/mini-shell/completions.cpp b/research/mini-shell/completions.cpp index b3b45de..989ab63 100644 --- a/research/mini-shell/completions.cpp +++ b/research/mini-shell/completions.cpp @@ -57,6 +57,40 @@ } +CommandTabCompletions::CommandTabCompletions(const std::vector& cmdList) + : commandList(cmdList) +{ +} + + +std::vector + CommandTabCompletions::getCompletions(const std::string& contextBeforeText, const std::string& partialText) +{ + std::vector matches; + for (int i = 0; i < commandList.size(); i++) + { + std::string cmd = commandList[i]; + if (strncmp(partialText.c_str(), cmd.c_str(), partialText.size()) == 0) + { + Completion completion; + completion.shortDisplayText = cmd; + completion.completionText = cmd; + matches.push_back(completion); + } + } + return matches; +} + + +void CommandTabCompletions::showPossibleCompletions(const std::vector& completions) +{ + printf("\n"); + for (int i = 0; i < completions.size(); i++) + printf("%s ", completions[i].shortDisplayText.c_str()); + printf("\n"); +} + + FileTabCompletions::FileTabCompletions(FileSystem& fileSys) : fs(fileSys) { @@ -101,8 +135,9 @@ } -ReadLineWithCompletions::ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp) +ReadLineWithCompletions::ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp_arg0, CompletionsProviderInterface& cp) : Parent(text) + , commandCompletions(cp_arg0) , completions(cp) { } @@ -125,7 +160,12 @@ lstArg = args.back(); //printf("lst arg: -%s-\n", lstArg.c_str()); - std::vector matches = completions.getCompletions(curText, lstArg); + std::vector matches; + + if (args.size() == 1) + matches = commandCompletions.getCompletions(curText, lstArg); + else + matches = completions.getCompletions(curText, lstArg); int commonLength = 0; if (matches.size() >= 1) { diff --git a/research/mini-shell/completions.h b/research/mini-shell/completions.h index d53c10a..d1dba48 100755 --- a/research/mini-shell/completions.h +++ b/research/mini-shell/completions.h @@ -38,6 +38,17 @@ }; +class CommandTabCompletions : public CompletionsProviderInterface +{ +public: + CommandTabCompletions(const std::vector& commandList); + std::vector getCompletions(const std::string& contextBeforeText, const std::string& partialText); + void showPossibleCompletions(const std::vector& completions); +private: + const std::vector& commandList; +}; + + class FileTabCompletions : public CompletionsProviderInterface { public: @@ -53,12 +64,13 @@ { typedef ReadLineWithHistory Parent; public: - ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp); + ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp_arg0, CompletionsProviderInterface& cp); virtual void prepare(); virtual bool processKeyPress(unsigned int ch); private: int lastCh; + CompletionsProviderInterface& commandCompletions; CompletionsProviderInterface& completions; }; diff --git a/research/XPlat/run.cmd b/research/XPlat/run.cmd index 712d81c..8d7b118 100755 --- a/research/XPlat/run.cmd +++ b/research/XPlat/run.cmd @@ -1,20 +1,28 @@ #!/bin/csh -echo off > /dev/null +echo off > nul +echo ================================================= echo Welcome to a cross-platform script +echo ================================================= goto do_windows do_windows: +rm nul [ `uname` = "Interix" ] && goto do_win32unix [ `uname` = "Darwin" ] && goto do_mac [ `uname` = "Linux" ] && goto do_linux do_win32unix: echo Environment is SFU Win32 Unix - exit + goto end do_mac: echo Environment is MacOSX - exit + goto end do_linux: echo Environment is Linux - exit + goto end :do_windows echo Environment is Windows + goto end +end: + alias ':end' "" +:end + echo Done diff --git a/research/mini-shell/completions.cpp b/research/mini-shell/completions.cpp index b3b45de..989ab63 100644 --- a/research/mini-shell/completions.cpp +++ b/research/mini-shell/completions.cpp @@ -57,6 +57,40 @@ } +CommandTabCompletions::CommandTabCompletions(const std::vector& cmdList) + : commandList(cmdList) +{ +} + + +std::vector + CommandTabCompletions::getCompletions(const std::string& contextBeforeText, const std::string& partialText) +{ + std::vector matches; + for (int i = 0; i < commandList.size(); i++) + { + std::string cmd = commandList[i]; + if (strncmp(partialText.c_str(), cmd.c_str(), partialText.size()) == 0) + { + Completion completion; + completion.shortDisplayText = cmd; + completion.completionText = cmd; + matches.push_back(completion); + } + } + return matches; +} + + +void CommandTabCompletions::showPossibleCompletions(const std::vector& completions) +{ + printf("\n"); + for (int i = 0; i < completions.size(); i++) + printf("%s ", completions[i].shortDisplayText.c_str()); + printf("\n"); +} + + FileTabCompletions::FileTabCompletions(FileSystem& fileSys) : fs(fileSys) { @@ -101,8 +135,9 @@ } -ReadLineWithCompletions::ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp) +ReadLineWithCompletions::ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp_arg0, CompletionsProviderInterface& cp) : Parent(text) + , commandCompletions(cp_arg0) , completions(cp) { } @@ -125,7 +160,12 @@ lstArg = args.back(); //printf("lst arg: -%s-\n", lstArg.c_str()); - std::vector matches = completions.getCompletions(curText, lstArg); + std::vector matches; + + if (args.size() == 1) + matches = commandCompletions.getCompletions(curText, lstArg); + else + matches = completions.getCompletions(curText, lstArg); int commonLength = 0; if (matches.size() >= 1) { diff --git a/research/mini-shell/completions.h b/research/mini-shell/completions.h index d53c10a..d1dba48 100755 --- a/research/mini-shell/completions.h +++ b/research/mini-shell/completions.h @@ -38,6 +38,17 @@ }; +class CommandTabCompletions : public CompletionsProviderInterface +{ +public: + CommandTabCompletions(const std::vector& commandList); + std::vector getCompletions(const std::string& contextBeforeText, const std::string& partialText); + void showPossibleCompletions(const std::vector& completions); +private: + const std::vector& commandList; +}; + + class FileTabCompletions : public CompletionsProviderInterface { public: @@ -53,12 +64,13 @@ { typedef ReadLineWithHistory Parent; public: - ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp); + ReadLineWithCompletions(EditTextInterface& text, CompletionsProviderInterface& cp_arg0, CompletionsProviderInterface& cp); virtual void prepare(); virtual bool processKeyPress(unsigned int ch); private: int lastCh; + CompletionsProviderInterface& commandCompletions; CompletionsProviderInterface& completions; }; diff --git a/research/mini-shell/main.cpp b/research/mini-shell/main.cpp index b0c9ddd..5da4a80 100644 --- a/research/mini-shell/main.cpp +++ b/research/mini-shell/main.cpp @@ -39,10 +39,11 @@ class CommandLineWithCompletions : public FileTabCompletions { public: - CommandLineWithCompletions(FileSystem& fs) + CommandLineWithCompletions(const std::vector& commandList, FileSystem& fs) : fileSystem(fs) , FileTabCompletions(fs) - , inputProcessor(text, *this) + , cmdCompletions(commandList) + , inputProcessor(text, cmdCompletions, *this) , input(kb, inputProcessor) { } @@ -69,6 +70,7 @@ fflush(stdout); } FileSystem& fileSystem; + CommandTabCompletions cmdCompletions; CommandLineText text; RawKeyboard kb; ReadLineWithCompletions inputProcessor; @@ -865,8 +867,12 @@ int main(int argc, char *argv[]) { +#define DO_COMMAND(str, cmd) str, + std::vector cmdList = { COMMAND_LIST }; +#undef DO_COMMAND + FileSystem fs; - CommandLineWithCompletions commandLine(fs); + CommandLineWithCompletions commandLine(cmdList, fs); Context ctx(argc, argv, fs, commandLine); char IFS[64];