diff --git a/bleep-cli/src/scala/bleep/Main.scala b/bleep-cli/src/scala/bleep/Main.scala index bd288de2..b51489aa 100644 --- a/bleep-cli/src/scala/bleep/Main.scala +++ b/bleep-cli/src/scala/bleep/Main.scala @@ -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 diff --git a/bleep-cli/src/scala/bleep/commands/InstallBashTabCompletions.scala b/bleep-cli/src/scala/bleep/commands/InstallBashTabCompletions.scala index fd47352a..111de540 100644 --- a/bleep-cli/src/scala/bleep/commands/InstallBashTabCompletions.scala +++ b/bleep-cli/src/scala/bleep/commands/InstallBashTabCompletions.scala @@ -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 @@ -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(()) } diff --git a/bleep-cli/src/scala/bleep/commands/InstallZshTabCompletions.scala b/bleep-cli/src/scala/bleep/commands/InstallZshTabCompletions.scala index 341938ab..237964b3 100644 --- a/bleep-cli/src/scala/bleep/commands/InstallZshTabCompletions.scala +++ b/bleep-cli/src/scala/bleep/commands/InstallZshTabCompletions.scala @@ -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 @@ -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(()) }