diff --git a/README.md b/README.md index 4f8bd3c..fa8e2c6 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,6 @@ SETUP: logins, login Log in to a Gitea server logout Log out from a Gitea server - shellcompletion, autocomplete Install shell completion for tea whoami Show current logged in user OPTIONS @@ -101,6 +100,24 @@ There are different ways to get `tea`: 5. asdf (thirdparty): [mvaldes14/asdf-tea](https://github.com/mvaldes14/asdf-tea) +### Shell completion + +If you installed from source or the package does not provide the completions with it you can add them yourself with `tea completion ` command which is not visible in help. To generate the completions run one of the following commands depending on your shell. + +```shell +# .bashrc +source <(tea completion bash) + +# .zshrc +source <(tea completion zsh) + +# fish +tea completion fish > ~/.config/fish/completions/tea.fish + +# Powershell +Output the script to path/to/autocomplete/tea.ps1 an run it. +``` + ## Compilation Make sure you have a current go version installed (1.13 or newer). diff --git a/cmd/autocomplete.go b/cmd/autocomplete.go deleted file mode 100644 index 7e6fa9b..0000000 --- a/cmd/autocomplete.go +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package cmd - -import ( - "context" - "fmt" - "io" - "net/http" - "os" - "os/exec" - - "github.com/adrg/xdg" - "github.com/urfave/cli/v3" -) - -// CmdAutocomplete manages autocompletion -var CmdAutocomplete = cli.Command{ - Name: "shellcompletion", - Aliases: []string{"autocomplete"}, - Category: catSetup, - Usage: "Install shell completion for tea", - Description: "Install shell completion for tea", - ArgsUsage: " (bash, zsh, powershell, fish)", - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "install", - Usage: "Persist in shell config instead of printing commands", - }, - }, - Action: runAutocompleteAdd, -} - -func runAutocompleteAdd(_ context.Context, cmd *cli.Command) error { - var remoteFile, localFile, cmds string - shell := cmd.Args().First() - - switch shell { - case "zsh": - remoteFile = "contrib/autocomplete.zsh" - localFile = "autocomplete.zsh" - cmds = "echo 'PROG=tea _CLI_ZSH_AUTOCOMPLETE_HACK=1 source \"%s\"' >> ~/.zshrc && source ~/.zshrc" - - case "bash": - remoteFile = "contrib/autocomplete.sh" - localFile = "autocomplete.sh" - cmds = "echo 'PROG=tea source \"%s\"' >> ~/.bashrc && source ~/.bashrc" - - case "powershell": - remoteFile = "contrib/autocomplete.ps1" - localFile = "tea.ps1" - cmds = "\"& %s\" >> $profile" - - case "fish": - // fish is different, in that urfave/cli provides a generator for the shell script needed. - // this also means that the fish completion can become out of sync with the tea binary! - // writing to this directory suffices, as fish reads files there on startup, no cmds needed. - return writeFishAutoCompleteFile(cmd) - - default: - return fmt.Errorf("Must specify valid %s", cmd.ArgsUsage) - } - - localPath, err := xdg.ConfigFile("tea/" + localFile) - if err != nil { - return err - } - - cmds = fmt.Sprintf(cmds, localPath) - if err = writeRemoteAutoCompleteFile(remoteFile, localPath); err != nil { - return err - } - - if cmd.Bool("install") { - fmt.Println("Installing in your shellrc") - installer := exec.Command(shell, "-c", cmds) - if shell == "powershell" { - installer = exec.Command("powershell.exe", "-Command", cmds) - } - out, err := installer.CombinedOutput() - if err != nil { - return fmt.Errorf("Couldn't run the commands: %s %s", err, out) - } - } else { - fmt.Println("\n# Run the following commands to install autocompletion (or use --install)") - fmt.Println(cmds) - } - - return nil -} - -func writeRemoteAutoCompleteFile(file, destPath string) error { - url := fmt.Sprintf("https://gitea.com/gitea/tea/raw/branch/master/%s", file) - fmt.Println("Fetching " + url) - - res, err := http.Get(url) - if err != nil { - return err - } - defer res.Body.Close() - - writer, err := os.Create(destPath) - if err != nil { - return err - } - defer writer.Close() - - _, err = io.Copy(writer, res.Body) - return err -} - -func writeFishAutoCompleteFile(cmd *cli.Command) error { - // NOTE: to make sure this file is in sync with tea commands, we'd need to - // - check if the file exists - // - if it does, check if the tea version that wrote it is the currently running version - // - if not, rewrite the file - // on each application run - // NOTE: this generates a completion that also suggests file names, which looks kinda messy.. - script, err := cmd.ToFishCompletion() - if err != nil { - return err - } - - localPath, err := xdg.ConfigFile("fish/conf.d/tea_completion.fish") - if err != nil { - return err - } - writer, err := os.Create(localPath) - if err != nil { - return err - } - if _, err = io.WriteString(writer, script); err != nil { - return err - } - fmt.Printf("Installed tab completion to %s\n", localPath) - return nil -} diff --git a/cmd/cmd.go b/cmd/cmd.go index 5c3d3dd..a74a6d2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -36,7 +36,6 @@ func App() *cli.Command { Commands: []*cli.Command{ &CmdLogin, &CmdLogout, - &CmdAutocomplete, &CmdWhoami, &CmdIssues, diff --git a/contrib/autocomplete.ps1 b/contrib/autocomplete.ps1 deleted file mode 100644 index 81812a6..0000000 --- a/contrib/autocomplete.ps1 +++ /dev/null @@ -1,9 +0,0 @@ -$fn = $($MyInvocation.MyCommand.Name) -$name = $fn -replace "(.*)\.ps1$", '$1' -Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock { - param($commandName, $wordToComplete, $cursorPosition) - $other = "$wordToComplete --generate-bash-completion" - Invoke-Expression $other | ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) - } - } \ No newline at end of file diff --git a/contrib/autocomplete.sh b/contrib/autocomplete.sh deleted file mode 100644 index f0f6241..0000000 --- a/contrib/autocomplete.sh +++ /dev/null @@ -1,21 +0,0 @@ -#! /bin/bash - -: ${PROG:=$(basename ${BASH_SOURCE})} - -_cli_bash_autocomplete() { - if [[ "${COMP_WORDS[0]}" != "source" ]]; then - local cur opts base - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - if [[ "$cur" == "-"* ]]; then - opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion ) - else - opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ) - fi - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - fi -} - -complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG -unset PROG diff --git a/contrib/autocomplete.zsh b/contrib/autocomplete.zsh deleted file mode 100644 index cf39c88..0000000 --- a/contrib/autocomplete.zsh +++ /dev/null @@ -1,23 +0,0 @@ -#compdef $PROG - -_cli_zsh_autocomplete() { - - local -a opts - local cur - cur=${words[-1]} - if [[ "$cur" == "-"* ]]; then - opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}") - else - opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}") - fi - - if [[ "${opts[1]}" != "" ]]; then - _describe 'values' opts - else - _files - fi - - return -} - -compdef _cli_zsh_autocomplete $PROG diff --git a/docs/CLI.md b/docs/CLI.md index ec137f8..7a844c5 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -95,12 +95,6 @@ Refresh an OAuth token Log out from a Gitea server -## shellcompletion, autocomplete - -Install shell completion for tea - -**--install**: Persist in shell config instead of printing commands - ## whoami Show current logged in user