Skip to content

Commit ff0405a

Browse files
committed
CrateSidebar: Show "cargo install" snippet for crates with binaries
1 parent 213ed75 commit ff0405a

File tree

4 files changed

+91
-28
lines changed

4 files changed

+91
-28
lines changed

app/components/crate-sidebar.hbs

Lines changed: 2 additions & 0 deletions
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}}
Lines changed: 77 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,82 @@
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+
{{#if (eq @binNames.length 1)}}
19+
Running the above command will globally install the
20+
<span local-class="bin-name">{{get @binNames 0}}</span>
21+
binary.
22+
{{else if (eq @binNames.length 2)}}
23+
Running the above command will globally install the
24+
<span local-class="bin-name">{{get @binNames 0}}</span>
25+
and
26+
<span local-class="bin-name">{{get @binNames 1}}</span>
27+
binaries.
28+
{{else}}
29+
Running the above command will globally install these binaries:
30+
{{#each @binNames as |binName index|~}}
31+
{{~#if (eq index 0)~}}
32+
<span local-class="bin-name">{{binName}}</span>
33+
{{~else if (eq index (sum @binNames.length -1))}}
34+
and <span local-class="bin-name">{{binName}}</span>
35+
{{~else~}}
36+
, <span local-class="bin-name">{{binName}}</span>
37+
{{~/if}}
38+
{{~/each}}
39+
{{/if}}
40+
</p>
41+
42+
{{/if}}
43+
44+
{{#if (and @hasLib @binNames)}}
45+
<h3>Install as library</h3>
1646
{{/if}}
1747

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

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>
68+
{{#if (is-clipboard-supported)}}
69+
<CopyButton
70+
@copyText={{this.tomlSnippet}}
71+
title="Copy Cargo.toml snippet to clipboard"
72+
local-class="copy-button"
73+
>
74+
<span local-class="selectable">{{this.tomlSnippet}}</span>
75+
{{svg-jar "copy" aria-hidden="true" local-class="copy-icon"}}
76+
</CopyButton>
77+
{{else}}
78+
<code local-class="copy-fallback">
79+
{{this.tomlSnippet}}
80+
</code>
81+
{{/if}}
3382
{{/if}}

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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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)