Skip to content

Instantly share code, notes, and snippets.

@gosoccerboy5
Last active August 27, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gosoccerboy5/aec0c9059054c13e64ca4cdc2a587be8 to your computer and use it in GitHub Desktop.
Save gosoccerboy5/aec0c9059054c13e64ca4cdc2a587be8 to your computer and use it in GitHub Desktop.
A recreation of tabs-to-spaces.html on my formatting-help repository.
import "dart:html";
final input = querySelector("#input") as TextAreaElement;
final output = querySelector("#output") as TextAreaElement;
final radio = querySelector("#tabs2spaces") as RadioButtonInputElement;
final button = querySelector("#button") as ButtonElement;
final spacesPerTabs = querySelector("#spacesPerTabs") as InputElement;
// Setup elements
void main() {
spacesPerTabs.value = "4";
input.focus();
// Init
void Function(Event) update = (event) {
if (radio.checked as bool) {
// We want tabs to spaces
output.value = input.value?.replaceAllMapped(
RegExp(r"^\t*", multiLine: true),
// Regex matches string-start/newline, then multiple tabs
(n) => " " * (n[0]!.length * int.tryParse(spacesPerTabs.value ?? "4") ?? 4)
// Check number of tabs, and multiply that by wanted num of spaces
);
} else {
// We want spaces to tabs
output.value = input.value?.replaceAllMapped(
RegExp("^ *", multiLine: true), // Newline/string-start, then spaces
(n) => "\t"*(n[0]!.length/int.parse(spacesPerTabs.value ?? "4")).floor()
// Divide that by spaces per tab, and use that many tabs
);
}
};
input.onInput.listen(update);
button.onClick.listen(update);
spacesPerTabs.onInput.listen(update);
radio.onChange.listen(update);
(querySelector("#spaces2tabs") as RadioButtonInputElement)
.onChange.listen(update);
// Add event listeners to all the elements that receive input
}
<h1>Replacing tabs with spaces, or vice versa</h1>
<h2>Do you ever get frustrated when someone uses tabs and you don't like tabs,
<br>or someone insists on using spaces while you prefer tabs?</h2>
<p>Then this is for you!</p>
<textarea id="input" placeholder="Input here..."></textarea>
<br>
<button id="button">Switch!</button>
<div>
<input type="radio" name="tabsorspaces" value="tabs2spaces" id="tabs2spaces" checked>
<label for="tabs2spaces">Tabs to spaces</label>
</div>
<div>
<input type="radio" name="tabsorspaces" value="spaces2tabs" id="spaces2tabs">
<label for="spaces2tabs">Spaces to tabs</label>
</div>
Spaces per tab:
<input type="number" id="spacesPerTabs">
<br>
<textarea id="output" placeholder="Output here."></textarea>
<style>
html {
font-family: sans-serif;
tab-size: 4;
-moz-tab-size: 4;
-webkit-tab-size: 4;
margin: 10px;
color: white;
}
textarea {
width: 500px;
height: 200px;
}
h1, h2 {
font-weight: 400;
}
</style>
@gosoccerboy5
Copy link
Author

https://dartpad.dev/aec0c9059054c13e64ca4cdc2a587be8?null_safety=true

Made with ❤️ by Dartpad, the official online IDE/code runner for Dart, which allows even HTML to be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment