Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag to send shell completions to stdout. #385

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions bleep-cli/src/scala/bleep/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,20 @@ object Main {
def installTabCompletions(userPaths: UserPaths, logger: Logger): Opts[BleepCommand] =
List(
Opts.subcommand("install-tab-completions-bash", "Install tab completions for bash")(
Opts(commands.InstallBashTabCompletions(logger))
Opts
.flag("stdout", "send completion configuration to stdout")
.orFalse
.map { stdout =>
commands.InstallBashTabCompletions(logger, stdout)
}
),
Opts.subcommand("install-tab-completions-zsh", "Install tab completions for zsh")(
Opts(commands.InstallZshTabCompletions(userPaths, logger))
Opts
.flag("stdout", "send completion configuration to stdout")
.orFalse
.map { stdout =>
commands.InstallZshTabCompletions(userPaths, logger, stdout)
}
)
).foldK

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package commands
import bleep.internal.FileUtils
import bleep.logging.Logger

case class InstallBashTabCompletions(logger: Logger) extends BleepCommand {
case class InstallBashTabCompletions(logger: Logger, stdout: Boolean) extends BleepCommand {
override def run(): Either[BleepException, Unit] = {
val programName = BleepExecutable.findCurrentBleep(logger) match {
case Some(CoursierInstallation(scriptPath, _)) => scriptPath.getFileName.toString
Expand All @@ -15,13 +15,18 @@ case class InstallBashTabCompletions(logger: Logger) extends BleepCommand {
}
val customProgramName = if (programName == "bleep") None else Some(programName)

PatchRcFile(customProgramName, logger, FileUtils.Home / ".profile")(
val completionScript =
s"""_${programName}_completions() {
| COMPREPLY=($$(bleep _complete "$${COMP_LINE}" "$${COMP_CWORD}" "$${COMP_POINT}"))
|}
|
|complete -F _${programName}_completions $programName""".stripMargin
)

if (stdout) {
println(completionScript)
} else {
PatchRcFile(customProgramName, logger, FileUtils.Home / ".profile")(completionScript)
}

Right(())
}
Expand Down
19 changes: 11 additions & 8 deletions bleep-cli/src/scala/bleep/commands/InstallZshTabCompletions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import bleep.logging.Logger
import java.nio.charset.StandardCharsets
import java.nio.file.Path

case class InstallZshTabCompletions(userPaths: UserPaths, logger: Logger) extends BleepCommand {
case class InstallZshTabCompletions(userPaths: UserPaths, logger: Logger, stdout: Boolean) extends BleepCommand {
override def run(): Either[BleepException, Unit] = {
val programName = BleepExecutable.findCurrentBleep(logger) match {
case Some(CoursierInstallation(scriptPath, _)) => scriptPath.getFileName.toString
Expand All @@ -28,16 +28,19 @@ case class InstallZshTabCompletions(userPaths: UserPaths, logger: Logger) extend
val completionScriptDir = userPaths.configDir / "zsh"
val completionScriptDest = completionScriptDir / s"_$programName"

logger.info(s"Writing $completionScriptDest")
FileSync.softWriteBytes(completionScriptDest, completionScript.getBytes(StandardCharsets.UTF_8))
if (stdout) {
println(completionScript)
} else {
logger.info(s"Writing $completionScriptDest")
FileSync.softWriteBytes(completionScriptDest, completionScript.getBytes(StandardCharsets.UTF_8)): Unit
val zshRc = Option(System.getenv("ZDOTDIR")).map(Path.of(_)).getOrElse(FileUtils.Home) / ".zshrc"

val zshRc = Option(System.getenv("ZDOTDIR")).map(Path.of(_)).getOrElse(FileUtils.Home) / ".zshrc"

PatchRcFile(None, logger, zshRc)(
s"""fpath=("$completionScriptDir" $$fpath)
PatchRcFile(None, logger, zshRc)(
s"""fpath=("$completionScriptDir" $$fpath)
|compinit
|""".stripMargin
)
)
}

Right(())
}
Expand Down
Loading