You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
5.8KB

  1. using namespace System.Management.Automation
  2. using namespace System.Management.Automation.Language
  3. Register-ArgumentCompleter -Native -CommandName 'zola' -ScriptBlock {
  4. param($wordToComplete, $commandAst, $cursorPosition)
  5. $commandElements = $commandAst.CommandElements
  6. $command = @(
  7. 'zola'
  8. for ($i = 1; $i -lt $commandElements.Count; $i++) {
  9. $element = $commandElements[$i]
  10. if ($element -isnot [StringConstantExpressionAst] -or
  11. $element.StringConstantType -ne [StringConstantType]::BareWord -or
  12. $element.Value.StartsWith('-')) {
  13. break
  14. }
  15. $element.Value
  16. }) -join ';'
  17. $completions = @(switch ($command) {
  18. 'zola' {
  19. [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml')
  20. [CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'Path to a config file other than config.toml')
  21. [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
  22. [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
  23. [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
  24. [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
  25. [CompletionResult]::new('init', 'init', [CompletionResultType]::ParameterValue, 'Create a new Zola project')
  26. [CompletionResult]::new('build', 'build', [CompletionResultType]::ParameterValue, 'Builds the site')
  27. [CompletionResult]::new('serve', 'serve', [CompletionResultType]::ParameterValue, 'Serve the site. Rebuild and reload on change automatically')
  28. [CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Prints this message or the help of the given subcommand(s)')
  29. break
  30. }
  31. 'zola;init' {
  32. [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
  33. [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
  34. [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
  35. [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
  36. break
  37. }
  38. 'zola;build' {
  39. [CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'Force the base URL to be that value (default to the one in config.toml)')
  40. [CompletionResult]::new('--base-url', 'base-url', [CompletionResultType]::ParameterName, 'Force the base URL to be that value (default to the one in config.toml)')
  41. [CompletionResult]::new('-o', 'o', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path')
  42. [CompletionResult]::new('--output-dir', 'output-dir', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path')
  43. [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
  44. [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
  45. [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
  46. [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
  47. break
  48. }
  49. 'zola;serve' {
  50. [CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'Interface to bind on')
  51. [CompletionResult]::new('--interface', 'interface', [CompletionResultType]::ParameterName, 'Interface to bind on')
  52. [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'Which port to use')
  53. [CompletionResult]::new('--port', 'port', [CompletionResultType]::ParameterName, 'Which port to use')
  54. [CompletionResult]::new('-o', 'o', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path')
  55. [CompletionResult]::new('--output-dir', 'output-dir', [CompletionResultType]::ParameterName, 'Outputs the generated site in the given path')
  56. [CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'Changes the base_url')
  57. [CompletionResult]::new('--base-url', 'base-url', [CompletionResultType]::ParameterName, 'Changes the base_url')
  58. [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
  59. [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
  60. [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
  61. [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
  62. break
  63. }
  64. 'zola;help' {
  65. [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
  66. [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
  67. [CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
  68. [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
  69. break
  70. }
  71. })
  72. $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
  73. Sort-Object -Property ListItemText
  74. }