Skip to content

Commit

Permalink
Fixes #119.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfbiedert committed Jan 22, 2021
1 parent a63ce7a commit 04442df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
14 changes: 12 additions & 2 deletions content/_index.md
Expand Up @@ -8,7 +8,7 @@ insert_anchor_links = "right"

<img id="logo" class="hide_on_small" src="logo.png" alt="Ferris holding a cheat sheet."></img>
<pagetitle>Rust Language Cheat Sheet</pagetitle>
<subtitle><span id="subtitle" onclick="toggle_subtitle()">{{ date() }}</span></subtitle>
<subtitle><span id="subtitle" onclick="advance_subtitle()">{{ date() }}</span></subtitle>


> Contains clickable links to
Expand Down Expand Up @@ -624,13 +624,23 @@ Debuggers hate him. Avoid bugs with this one weird trick.

<fixed-2-column class="color-header special_example">


| Within Doc Comments | Explanation |
|--------|-------------|
| ` ```rust ... ``` ` | Include a [doc test](https://doc.rust-lang.org/rustdoc/documentation-tests.html) (doc code running on `cargo test`). |
| ` ```...``` ` | Include a [**doc test**](https://doc.rust-lang.org/rustdoc/documentation-tests.html) (doc code running on `cargo test`). |
| ` ```X,Y ...``` ` | Same, and include optional configurations; with `X`, `Y` being ... |
| {{ tab() }} <code style="color: gray;">rust</code> | Make it explicit test is written in Rust; implied by Rust tooling. |
| {{ tab() }} <code style="color: gray; opacity: 0.3;">-</code> | Compile test. Run test. Fail if panic. **Default behavior**. |
| {{ tab() }} <code style="color: gray;">should_panic</code> | Compile test. Run test. Execution should panic. If not, fail test. |
| {{ tab() }} <code style="color: gray;">no_run</code> | Compile test. Fail test if code can't be compiled, Don't run test. |
| {{ tab() }} <code style="color: gray;">compile_fail</code> | Compile test but fail test if code _can_ be compiled. |
| {{ tab() }} <code style="color: gray;">ignore</code> | Do not compile. Do not run. Prefer option above instead. |
| {{ tab() }} <code style="color: gray;">edition2018</code> | Execute code as Rust '18; default is '15. |
| `#` | Hide line from documentation (` ``` # use x::hidden; ``` `). |
| <code>[&#96;S&#96;]</code> | Create a link to struct, enum, trait, function, &hellip; `S`. |
| <code>[&#96;S&#96;]&#40;crate::S&#41;</code> | Paths can also be used, in the form of markdown links. |


</fixed-2-column>


Expand Down
42 changes: 31 additions & 11 deletions static/index.js
Expand Up @@ -27,7 +27,8 @@ const subtiles = [
"Prints best on Dunder Mifflin premium copy paper.",
"May contain R-rated content.",
"What if the Mayas meant 2021?",
"Your mission, should you choose to accept it: Put Rust on a Mars rover. <br> This message will self-destruct in 5 seconds.",
"Your mission, should you choose to accept it: Put Rust on a Mars rover.",
["This message will self-destruct in 5 seconds.", false],
["The self-destruct mechanism was written in JavaScript ...", false],
"Turned out the Señor Developer job wasn't much of a pay bump.",
"Testing Bekenstein's limit one entry a time.",
Expand Down Expand Up @@ -231,21 +232,39 @@ function set_survey(state) {


// Called when the user clicks the subtitle (usually the date)
function toggle_subtitle(to_index) {
function advance_subtitle(to_index) {
let subtitle = document.getElementById("subtitle");
let subtitle_entry = "UNDEFINED";

if (!!to_index) {
// If called with specific index use that and stop thinking
// about it.
subtitle_entry = subtiles[to_index];
subtitle_index = to_index;
} else {
subtitle_index = (subtitle_index + 1) % subtiles.length;
}
// If not called with specific index (normal onclick behavior),
// increase number.
let next_possible_index = (subtitle_index + 1) % subtiles.length;

// Is this now a follow-up entry?
//
// Yes: Show it.
// No: Cycle back between the initial SKIP_FIRST_N_SUBTITLES.
//
// To figure out if follow-up entry, check if ("xxx", false) pair.

// Can either be "xxx", or a ("xxx", false) pair, in which case we have
// to get the first element.
let subtitle_entry = subtiles[subtitle_index];
console.log(subtitle_entry);
if (subtitle_entry[1] === false) {
subtitle_entry = subtitle_entry[0];
subtitle_entry = subtiles[next_possible_index];

if (subtitle_entry[1] === false) {
// If that was a ("xxx", false) follow-up pair, get actual content and show.
subtitle_index = next_possible_index;
subtitle_entry = subtitle_entry[0];
} else {
// If was not a follow-up, rotate between first keys only.
next_possible_index = next_possible_index % SKIP_FIRST_N_SUBTITLES;
subtitle_index = next_possible_index;
subtitle_entry = subtiles[next_possible_index];
}
}

subtitle.innerHTML = subtitle_entry;
Expand All @@ -255,6 +274,7 @@ function toggle_subtitle(to_index) {
function random_quote() {
let index = false;

// Keep picking random numbers until we find some not a follow-up.
while (index === false) {
let rand = Math.random();

Expand All @@ -266,7 +286,7 @@ function random_quote() {
}
}

toggle_subtitle(index);
advance_subtitle(index);
}


Expand Down

0 comments on commit 04442df

Please sign in to comment.