Skip to content

Commit d2fdf3a

Browse files
committed
CrateSidebar: Show "cargo install" snippet for crates with binaries
1 parent 6100022 commit d2fdf3a

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed

app/components/crate-sidebar.hbs

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
@crate={{@crate.name}}
5353
@version={{@version.num}}
5454
@exactVersion={{@requestedVersion}}
55+
@hasLib={{not (@version.has_lib false)}}
56+
@binNames={{@version.bin_names}}
5557
/>
5658
</div>
5759
{{/unless}}
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,76 @@
1-
<p local-class="copy-help">Run the following Cargo command in your project directory:</p>
1+
{{#if @binNames}}
2+
{{#if (is-clipboard-supported)}}
3+
<CopyButton
4+
@copyText={{this.cargoInstallCommand}}
5+
title="Copy command to clipboard"
6+
local-class="copy-button"
7+
>
8+
<span local-class="selectable">{{this.cargoInstallCommand}}</span>
9+
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
10+
</CopyButton>
11+
{{else}}
12+
<code local-class="copy-fallback">
13+
{{this.cargoInstallCommand}}
14+
</code>
15+
{{/if}}
216

3-
{{#if (is-clipboard-supported)}}
4-
<CopyButton
5-
@copyText={{this.cargoAddCommand}}
6-
title="Copy command to clipboard"
7-
local-class="copy-button"
8-
>
9-
<span local-class="selectable">{{this.cargoAddCommand}}</span>
10-
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
11-
</CopyButton>
12-
{{else}}
13-
<code local-class="copy-fallback">
14-
{{this.cargoAddCommand}}
15-
</code>
17+
<p local-class="copy-help">
18+
Running the above command will globally install the following binaries:
19+
{{#if (eq @binNames.length 1)}}
20+
<span local-class="bin-name">{{get @binNames 0}}</span>
21+
{{else if (eq @binNames.length 2)}}
22+
<span local-class="bin-name">{{get @binNames 0}}</span> and <span local-class="bin-name">{{get @binNames 1}}</span>
23+
{{else}}
24+
{{#each @binNames as |binName index|~}}
25+
{{~#if (eq index 0)~}}
26+
<span local-class="bin-name">{{binName}}</span>
27+
{{~else if (eq index (sum @binNames.length -1))~}}
28+
, and <span local-class="bin-name">{{binName}}</span>
29+
{{~else~}}
30+
, <span local-class="bin-name">{{binName}}</span>
31+
{{~/if}}
32+
{{~/each}}
33+
{{/if}}
34+
</p>
35+
36+
{{/if}}
37+
38+
{{#if (and @hasLib @binNames)}}
39+
<h3>Install as library</h3>
1640
{{/if}}
1741

18-
<p local-class="copy-help">Or add the following line to your Cargo.toml:</p>
42+
{{#if @hasLib}}
43+
<p local-class="copy-help">Run the following Cargo command in your project directory:</p>
44+
45+
{{#if (is-clipboard-supported)}}
46+
<CopyButton
47+
@copyText={{this.cargoAddCommand}}
48+
title="Copy command to clipboard"
49+
local-class="copy-button"
50+
>
51+
<span local-class="selectable">{{this.cargoAddCommand}}</span>
52+
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
53+
</CopyButton>
54+
{{else}}
55+
<code local-class="copy-fallback">
56+
{{this.cargoAddCommand}}
57+
</code>
58+
{{/if}}
59+
60+
<p local-class="copy-help">Or add the following line to your Cargo.toml:</p>
1961

20-
{{#if (is-clipboard-supported)}}
21-
<CopyButton
22-
@copyText={{this.tomlSnippet}}
23-
title="Copy Cargo.toml snippet to clipboard"
24-
local-class="copy-button"
25-
>
26-
<span local-class="selectable">{{this.tomlSnippet}}</span>
27-
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
28-
</CopyButton>
29-
{{else}}
30-
<code local-class="copy-fallback">
31-
{{this.tomlSnippet}}
32-
</code>
62+
{{#if (is-clipboard-supported)}}
63+
<CopyButton
64+
@copyText={{this.tomlSnippet}}
65+
title="Copy Cargo.toml snippet to clipboard"
66+
local-class="copy-button"
67+
>
68+
<span local-class="selectable">{{this.tomlSnippet}}</span>
69+
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
70+
</CopyButton>
71+
{{else}}
72+
<code local-class="copy-fallback">
73+
{{this.tomlSnippet}}
74+
</code>
75+
{{/if}}
3376
{{/if}}

app/components/crate-sidebar/install-instructions.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import Component from '@glimmer/component';
22

33
export default class InstallInstructions extends Component {
4+
get cargoInstallCommand() {
5+
return this.args.exactVersion
6+
? `cargo install ${this.args.crate}@${this.args.version}`
7+
: `cargo install ${this.args.crate}`;
8+
}
9+
410
get cargoAddCommand() {
511
return this.args.exactVersion
612
? `cargo add ${this.args.crate}@=${this.args.version}`

app/components/crate-sidebar/install-instructions.module.css

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.copy-help {
22
font-size: 12px;
3+
overflow-wrap: break-word;
34
}
45

56
.copy-button,
@@ -51,3 +52,8 @@
5152
.selectable {
5253
user-select: text;
5354
}
55+
56+
.bin-name {
57+
font-family: var(--font-monospace);
58+
font-weight: bold;
59+
}

0 commit comments

Comments
 (0)