Skip to content

Commit

Permalink
Added flag to send shell completions to stdout. (#385)
Browse files Browse the repository at this point in the history
This will allow users that have bespoke systems to pipe and modify the
completions as they see fit, increasing the number of systems shell
completions can be installed too.
  • Loading branch information
KristianAN committed Jun 9, 2024
1 parent 8ca5c55 commit 37bb98e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
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

0 comments on commit 37bb98e

Please sign in to comment.