Module: Space::Src::ShellIntegration

Defined in:
lib/space_src/shell_integration.rb

Overview

Generates the standalone ‘src` fish shell integration and completions. All paths resolved through Space::Src::Paths (no external core deps).

Constant Summary collapse

FISH_TEMPLATE =
<<~'FISH'
  # Generated by space-src. Do not edit by hand.

  function src --wraps src --description "Navigate source checkouts"
      if not set -q __src_compat_checked
          set -g __src_compat_checked 1
          set -l __src_installed_version __SRC_VERSION__
          set -l __src_binary_version (command src --version 2>/dev/null)
          if test "$__src_binary_version" != "$__src_installed_version"
              echo "space-src: shell integration version $__src_installed_version does not match binary version $__src_binary_version; re-run 'src shell fish install'" >&2
          end
      end

      switch "$argv[1]"
          case repo org sync status config daemon clone shell
              command src $argv
              return $status
          case '*'
              set -l __src_output (command src $argv)
              set -l __src_status $status

              if test (count $__src_output) -gt 0
                  printf "%s\n" $__src_output
              end

              if test $__src_status -eq 0
                  set -l __src_target $__src_output[-1]
                  set __src_target (string replace -r "^~(?=/|$)" $HOME -- $__src_target)
                  if test -d "$__src_target"
                      cd "$__src_target"
                  end
              end

              return $__src_status
      end
  end
FISH
FISH_COMPLETIONS =
<<~'FISH'
  # Generated by space-src. Do not edit by hand.

  function __src_complete_command
      set -l tokens (commandline -opc)
      set -l index 2

      while test $index -le (count $tokens)
          set -l token $tokens[$index]

          switch "$token"
              case "-*"
                  set index (math $index + 1)
                  continue
              case "*"
                  echo $token
                  return 0
          end
      end
  end

  function __src_complete_needs_command
      test -z "(__src_complete_command)"
  end

  function __src_complete_using_command
      contains -- (__src_complete_command) $argv
  end

  function __src_complete_first_argument_after
      set -l commands $argv
      set -l tokens (commandline -opc)
      set -l index 2
      set -l matched_command 0

      while test $index -le (count $tokens)
          set -l token $tokens[$index]

          switch "$token"
              case "-*"
                  set index (math $index + 1)
                  continue
          end

          if test $matched_command -eq 0
              if contains -- "$token" $commands
                  set matched_command 1
              else
                  return 1
              end
          else
              echo $token
              return 0
          end

          set index (math $index + 1)
      end

      return 1
  end

  function __src_complete_has_first_argument_after
      set -q argv[1]; or return 1
      set -l first_argument (__src_complete_first_argument_after $argv)
      test -n "$first_argument"
  end

  function __src_complete_second_argument_after
      set -q argv[1]; or return 1
      set -l command $argv[1]
      set -l tokens (commandline -opc)
      set -l index 2
      set -l matched_command 0
      set -l matched_first_argument 0

      while test $index -le (count $tokens)
          set -l token $tokens[$index]

          switch "$token"
              case "-*"
                  set index (math $index + 1)
                  continue
          end

          if test $matched_command -eq 0
              test "$token" = "$command"; or return 1
              set matched_command 1
          else if test $matched_first_argument -eq 0
              set matched_first_argument 1
          else
              echo $token
              return 0
          end

          set index (math $index + 1)
      end

      return 1
  end

  function __src_complete_has_second_argument_after
      set -q argv[1]; or return 1
      set -l second_argument (__src_complete_second_argument_after $argv)
      test -n "$second_argument"
  end

  function __src_complete_first_argument_is
      set -q argv[1]; or return 1
      set -l expected $argv[1]
      set -e argv[1]
      test "$(__src_complete_first_argument_after $argv)" = "$expected"
  end

  function __src_complete_checkouts
      command src shell complete checkouts 2>/dev/null
  end

  function __src_complete_shells
      command src shell complete shells 2>/dev/null
  end

  complete -c src -f -n "__src_complete_needs_command" -a clone   -d "Clone a repo into the source tree"
  complete -c src -f -n "__src_complete_needs_command" -a config  -d "Show or update config"
  complete -c src -f -n "__src_complete_needs_command" -a daemon  -d "Manage the background sync daemon"
  complete -c src -f -n "__src_complete_needs_command" -a org     -d "Manage tracked GitHub orgs"
  complete -c src -f -n "__src_complete_needs_command" -a repo    -d "Manage tracked repos"
  complete -c src -f -n "__src_complete_needs_command" -a shell   -d "Manage shell integration"
  complete -c src -f -n "__src_complete_needs_command" -a status  -d "Show sync status"
  complete -c src -f -n "__src_complete_needs_command" -a sync    -d "Sync all tracked repos"
  complete -c src -f -n "__src_complete_needs_command" -a "(__src_complete_checkouts)" -d "Checkout"

  complete -c src -f -n "__src_complete_using_command config; and not __src_complete_has_first_argument_after config" -a path -d "Print config path"
  complete -c src -f -n "__src_complete_using_command config; and not __src_complete_has_first_argument_after config" -a show -d "Show config"

  complete -c src -f -n "__src_complete_using_command org; and not __src_complete_has_first_argument_after org" -a add    -d "Add a GitHub org"
  complete -c src -f -n "__src_complete_using_command org; and not __src_complete_has_first_argument_after org" -a remove -d "Remove a GitHub org"
  complete -c src -f -n "__src_complete_using_command org; and not __src_complete_has_first_argument_after org" -a list   -d "List tracked orgs"

  complete -c src -f -n "__src_complete_using_command repo; and not __src_complete_has_first_argument_after repo" -a add    -d "Add a repo"
  complete -c src -f -n "__src_complete_using_command repo; and not __src_complete_has_first_argument_after repo" -a remove -d "Remove a repo"
  complete -c src -f -n "__src_complete_using_command repo; and not __src_complete_has_first_argument_after repo" -a list   -d "List repos"

  complete -c src -f -n "__src_complete_using_command daemon; and not __src_complete_has_first_argument_after daemon" -a install   -d "Install launchd agent"
  complete -c src -f -n "__src_complete_using_command daemon; and not __src_complete_has_first_argument_after daemon" -a uninstall -d "Uninstall launchd agent"
  complete -c src -f -n "__src_complete_using_command daemon; and not __src_complete_has_first_argument_after daemon" -a start     -d "Start the agent"
  complete -c src -f -n "__src_complete_using_command daemon; and not __src_complete_has_first_argument_after daemon" -a stop      -d "Stop the agent"
  complete -c src -f -n "__src_complete_using_command daemon; and not __src_complete_has_first_argument_after daemon" -a restart   -d "Restart the agent"
  complete -c src -f -n "__src_complete_using_command daemon; and not __src_complete_has_first_argument_after daemon" -a status    -d "Show agent status"

  complete -c src -f -n "__src_complete_using_command shell; and not __src_complete_has_first_argument_after shell" -a init     -d "Print shell integration"
  complete -c src -f -n "__src_complete_using_command shell; and not __src_complete_has_first_argument_after shell" -a fish     -d "Manage fish integration and completions"
  complete -c src -f -n "__src_complete_using_command shell; and not __src_complete_has_first_argument_after shell" -a complete -d "Print completion candidates"

  complete -c src -f -n "__src_complete_first_argument_is fish shell; and not __src_complete_has_second_argument_after shell" -a install   -d "Install fish integration and completions"
  complete -c src -f -n "__src_complete_first_argument_is fish shell; and not __src_complete_has_second_argument_after shell" -a uninstall -d "Remove fish integration and completions"
  complete -c src -f -n "__src_complete_first_argument_is fish shell; and not __src_complete_has_second_argument_after shell" -a path      -d "Print fish integration paths"

  complete -c src -f -n "__src_complete_first_argument_is init shell; and not __src_complete_has_second_argument_after shell" -a "(__src_complete_shells)" -d "Shell"
  complete -c src -f -n "__src_complete_first_argument_is complete shell; and not __src_complete_has_second_argument_after shell" -a "checkouts shells" -d "Completion kind"
FISH

Class Method Summary collapse

Class Method Details

.completions_for(shell) ⇒ Object



218
219
220
221
222
223
224
225
# File 'lib/space_src/shell_integration.rb', line 218

def self.completions_for(shell)
  case shell.to_s
  when "fish"
    FISH_COMPLETIONS
  else
    raise "Unsupported shell '#{shell}'. Expected: fish"
  end
end

.completions_path_for(shell, env: ENV) ⇒ Object



236
237
238
239
240
241
242
243
# File 'lib/space_src/shell_integration.rb', line 236

def self.completions_path_for(shell, env: ENV)
  case shell.to_s
  when "fish"
    Pathname(Paths.new(environment: env).config_home).join("fish", "completions", "src.fish")
  else
    raise "Unsupported shell '#{shell}'. Expected: fish"
  end
end

.for(shell) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/space_src/shell_integration.rb', line 209

def self.for(shell)
  case shell.to_s
  when "fish"
    FISH_TEMPLATE.gsub("__SRC_VERSION__", Space::Src::VERSION)
  else
    raise "Unsupported shell '#{shell}'. Expected: fish"
  end
end

.install(shell, env: ENV, force: false) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/space_src/shell_integration.rb', line 245

def self.install(shell, env: ENV, force: false)
  fn_result = write_managed_file(
    path: path_for(shell, env:),
    content: self.for(shell),
    force: force,
    description: "fish function"
  )
  co_result = write_managed_file(
    path: completions_path_for(shell, env:),
    content: completions_for(shell),
    force: force,
    description: "fish completions"
  )
  fn_result.merge(
    completions_action: co_result.fetch(:action),
    completions_path: co_result.fetch(:path)
  )
end

.managed_src?(content) ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/space_src/shell_integration.rb', line 283

def self.managed_src?(content)
  content.include?("Generated by space-src")
end

.path_for(shell, env: ENV) ⇒ Object



227
228
229
230
231
232
233
234
# File 'lib/space_src/shell_integration.rb', line 227

def self.path_for(shell, env: ENV)
  case shell.to_s
  when "fish"
    Pathname(Paths.new(environment: env).config_home).join("fish", "functions", "src.fish")
  else
    raise "Unsupported shell '#{shell}'. Expected: fish"
  end
end

.uninstall(shell, env: ENV, force: false) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/space_src/shell_integration.rb', line 264

def self.uninstall(shell, env: ENV, force: false)
  fn_result = remove_managed_file(
    path: path_for(shell, env:),
    content: self.for(shell),
    force: force,
    description: "fish function"
  )
  co_result = remove_managed_file(
    path: completions_path_for(shell, env:),
    content: completions_for(shell),
    force: force,
    description: "fish completions"
  )
  fn_result.merge(
    completions_action: co_result.fetch(:action),
    completions_path: co_result.fetch(:path)
  )
end