Skip to content

Commit

Permalink
feat: Added support for fish CLI (#3104)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristianLentino99 authored Aug 19, 2024
1 parent 4119040 commit 98e98b6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scala.cli.commands.installcompletions

import caseapp.*
import caseapp.core.complete.{Bash, Zsh}
import caseapp.core.complete.{Bash, Fish, Zsh}
import caseapp.core.help.HelpFormat

import java.io.File
Expand Down Expand Up @@ -47,6 +47,7 @@ object InstallCompletions extends ScalaCommand[InstallCompletionsOptions] {
)
System.err.println(s"$name install completions --shell zsh")
System.err.println(s"$name install completions --shell bash")
System.err.println(s"$name install completions --shell fish")
sys.exit(1)
}
}
Expand Down Expand Up @@ -76,6 +77,10 @@ object InstallCompletions extends ScalaCommand[InstallCompletionsOptions] {
"compinit"
).map(_ + System.lineSeparator()).mkString
(script, defaultRcFile)
case Fish.id | "fish" =>
val script = Fish.script(name)
val defaultRcFile = os.home / ".config" / "fish" / "config.fish"
(script, defaultRcFile)
case _ =>
System.err.println(s"Unrecognized or unsupported shell: $format")
sys.exit(1)
Expand Down Expand Up @@ -118,6 +123,7 @@ object InstallCompletions extends ScalaCommand[InstallCompletionsOptions] {
EnvVar.Misc.shell.valueOpt.map(_.split("[\\/]+").last).map {
case "bash" => Bash.id
case "zsh" => Zsh.id
case "fish" => Fish.id
case other => other
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ object UninstallCompletions extends ScalaCommand[UninstallCompletionsOptions] {
.getOrElse(os.home)
val rcFiles = options.shared.rcFile.map(file => Seq(os.Path(file, os.pwd))).getOrElse(Seq(
zDotDir / ".zshrc",
os.home / ".bashrc"
os.home / ".bashrc",
os.home / ".config" / "fish" / "config.fish"
)).filter(os.exists(_))

rcFiles.foreach { rcFile =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,41 @@ import scala.util.Properties
class InstallAndUninstallCompletionsTests extends ScalaCliSuite {
val zshRcFile: String = ".zshrc"
val bashRcFile: String = ".bashrc"
val fishRcFile: String = "config.fish"
val rcContent: String = s"""
|dummy line
|dummy line""".stripMargin
val testInputs: TestInputs = TestInputs(
os.rel / zshRcFile -> rcContent,
os.rel / bashRcFile -> rcContent
os.rel / zshRcFile -> rcContent,
os.rel / bashRcFile -> rcContent,
os.rel / ".config" / "fish" / fishRcFile -> rcContent
)

def runInstallAndUninstallCompletions(): Unit = {
testInputs.fromRoot { root =>
val zshRcPath = root / zshRcFile
val bashRcPath = root / bashRcFile
val fishRcPath = root / ".config" / "fish" / fishRcFile
// install completions to the dummy rc files
os.proc(TestUtil.cli, "install-completions", "--rc-file", zshRcPath, "--shell", "zsh").call(
cwd = root
)
os.proc(TestUtil.cli, "install-completions", "--rc-file", bashRcPath, "--shell", "bash").call(
cwd = root
)
os.proc(TestUtil.cli, "install-completions", "--rc-file", fishRcPath, "--shell", "fish").call(
cwd = root
)
expect(os.read(bashRcPath).contains(bashRcScript))
expect(os.read(zshRcPath).contains(zshRcScript))
expect(os.read(fishRcPath).contains(fishRcScript))
// uninstall completions from the dummy rc files
os.proc(TestUtil.cli, "uninstall-completions", "--rc-file", zshRcPath).call(cwd = root)
os.proc(TestUtil.cli, "uninstall-completions", "--rc-file", bashRcPath).call(cwd = root)
os.proc(TestUtil.cli, "uninstall-completions", "--rc-file", fishRcPath).call(cwd = root)
expect(os.read(zshRcPath) == rcContent)
expect(os.read(bashRcPath) == rcContent)
expect(os.read(fishRcPath) == rcContent)
}
}

Expand All @@ -57,6 +66,15 @@ class InstallAndUninstallCompletionsTests extends ScalaCliSuite {
addTags(script)
}

lazy val fishRcScript: String = {
val progName = "scala-cli"
val script =
s"""
complete $progName -a '($progName complete fish-v1 (math 1 + (count (__fish_print_cmd_args))) (__fish_print_cmd_args))'
|""".stripMargin
addTags(script)
}

lazy val zshRcScript: String = {
val projDirs = ProjectDirectories.from(null, null, "ScalaCli")
val dir = os.Path(projDirs.dataLocalDir, TestUtil.pwd) / "completions" / "zsh"
Expand Down
20 changes: 20 additions & 0 deletions website/docs/_advanced_install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,11 @@ Alternatively, you can directly download it from the Maven repository [here](htt
values={[
{label: 'Bash', value: 'bash'},
{label: 'zsh', value: 'zsh'},
{label: 'fish', value: 'fish'},
]}>
<TabItem value="bash"></TabItem>
<TabItem value="zsh"></TabItem>
<TabItem value="fish"></TabItem>
</Tabs>
</SectionAbout>

Expand All @@ -463,6 +465,7 @@ Try the completions with
values={[
{label: 'Bash', value: 'bash'},
{label: 'zsh', value: 'zsh'},
{label: 'fish', value: 'fish'},
]}>
<TabItem value="bash">

Expand All @@ -479,6 +482,15 @@ eval "$(scala-cli install completions --env --shell zsh)"
scala-cli --<TAB>
```

</TabItem>
<TabItem value="fish">

```
eval "$(scala-cli install completions --env --shell fish)"
fish
scala-cli --<TAB>
```

</TabItem>
</Tabs>

Expand All @@ -493,6 +505,7 @@ with `--shell`
values={[
{label: 'Bash', value: 'bash'},
{label: 'zsh', value: 'zsh'},
{label: 'fish', value: 'fish'},
]}>
<TabItem value="bash">

Expand All @@ -507,6 +520,13 @@ scala-cli install completions --shell bash
scala-cli install completions --shell zsh
```

</TabItem>
<TabItem value="fish">

```bash
scala-cli install completions --shell fish
```

</TabItem>
</Tabs>
</div></div>
Expand Down

0 comments on commit 98e98b6

Please sign in to comment.