forked from Tyrrrz/CliFx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tyrrrz#13 - Implement command suggestions
- Loading branch information
Showing
4 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using CliFx.Input; | ||
using CliFx.Schema; | ||
using CliFx.Utils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CliFx | ||
{ | ||
internal class SuggestionService | ||
{ | ||
private ApplicationSchema _applicationSchema; | ||
|
||
public SuggestionService(ApplicationSchema applicationSchema) | ||
{ | ||
_applicationSchema = applicationSchema; | ||
} | ||
|
||
public IEnumerable<string> GetSuggestions(CommandInput commandInput) | ||
{ | ||
var text = ExtractCommandText(commandInput); | ||
var suggestArgs = CommandLineSplitter.Split(text).Skip(1); // ignore the application name | ||
|
||
var suggestInput = CommandInput.Parse( | ||
suggestArgs.ToArray(), | ||
commandInput.EnvironmentVariables.ToDictionary(p => p.Name, p => p.Value), | ||
_applicationSchema.GetCommandNames()); | ||
|
||
var commandMatch = _applicationSchema.Commands | ||
.FirstOrDefault(p => string.Equals(p.Name, suggestInput.CommandName, StringComparison.OrdinalIgnoreCase)); | ||
|
||
// suggest a command name if we don't have an exact match | ||
if (commandMatch == null) | ||
{ | ||
return _applicationSchema.GetCommandNames() | ||
.Where(p => p.Contains(suggestInput.CommandName, StringComparison.OrdinalIgnoreCase)) | ||
.OrderBy(p => p) | ||
.ToList(); | ||
} | ||
|
||
return NoSuggestions(); | ||
} | ||
|
||
private string ExtractCommandText(CommandInput input) | ||
{ | ||
// Accept command line arguments via environment variable as a workaround to powershell escape sequence shennidgans | ||
var commandCacheVariable = input.Options.FirstOrDefault(p => p.Identifier == "envvar")?.Values[0]; | ||
|
||
if (commandCacheVariable == null) | ||
{ | ||
// ignore cursor position as we don't know what the original user input string is | ||
return string.Join(" ", input.OriginalCommandLine.Where(arg => !IsDirective(arg))); | ||
} | ||
|
||
var command = input.EnvironmentVariables.FirstOrDefault(p => string.Equals(p.Name, commandCacheVariable))?.Value ?? ""; | ||
var cursorPositionText = input.Options.FirstOrDefault(p => p.Identifier == "cursor")?.Values[0]; | ||
var cursorPosition = command.Length; | ||
|
||
if (int.TryParse(cursorPositionText, out cursorPosition) && cursorPosition < command.Length) | ||
{ | ||
return command.Remove(cursorPosition); | ||
} | ||
return command; | ||
} | ||
|
||
private static bool IsDirective(string arg) | ||
{ | ||
return arg.StartsWith('[') && arg.EndsWith(']'); | ||
} | ||
|
||
private static List<string> NoSuggestions() | ||
{ | ||
return new List<string>(); | ||
} | ||
} | ||
} |