diff --git a/content/alarm-clock/Clock.cpp b/content/alarm-clock/Clock.cpp index fdef219..ab2a12e 100644 --- a/content/alarm-clock/Clock.cpp +++ b/content/alarm-clock/Clock.cpp @@ -20,7 +20,7 @@ since this number is out of the range for `int` (-32,768 ... 32,767), we'll get an unexpected result: -5536 instead of 60000. - */ +*/ #define MINUTE 60 * 1000L /* ms */ #define TIMESPAN_DAY TimeSpan(1, 0, 0, 0) /* gac:end */ diff --git a/content/dice-roller/dice-roller.ino b/content/dice-roller/dice-roller.ino index 0e31ab8..6b52892 100644 --- a/content/dice-roller/dice-roller.ino +++ b/content/dice-roller/dice-roller.ino @@ -49,8 +49,8 @@ void loop() { ✅ Give `digitalRead(...)` a meaningful name by storing the result it into a variable */ bool buttonPressed = digitalRead(BUTTON_PIN) == LOW; - /* gac: - Check if this is the first time the button is pressed. */ + /* gac: + Check if this is the first time the button is pressed. */ if (!randomReady && buttonPressed) { /* Initialize the random number generator with the number of microseconds between program start and the first button press */ diff --git a/content/simon/pitches.h b/content/simon/pitches.h index d12cfbd..c597c5a 100644 --- a/content/simon/pitches.h +++ b/content/simon/pitches.h @@ -1,6 +1,6 @@ /************************************************** - * This file defines constants with the frequency - * of different musical notes. + This file defines constants with the frequency + of different musical notes. *************************************************/ #define NOTE_B0 31 diff --git a/package-lock.json b/package-lock.json index c177e6a..e321faa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4372,6 +4372,12 @@ "integrity": "sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==", "dev": true }, + "astyle": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/astyle/-/astyle-2.0.0.tgz", + "integrity": "sha512-qmtKy9S4k2xjHIQei6DjLtHy1I7Nu3dZFACFSMMHBypxn2UuH6M9VHwJ4/3WWtUmexs7j1cwfC2QhsGLmLyu8A==", + "dev": true + }, "async-each": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/async-each/-/async-each-1.0.3.tgz", diff --git a/package.json b/package.json index fba34f0..6a41eaa 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "@types/react-test-renderer": "^16.9.2", "@typescript-eslint/eslint-plugin": "^3.7.0", "@typescript-eslint/parser": "^3.7.0", + "astyle": "^2.0.0", "eslint": "^7.5.0", "eslint-config-prettier": "^6.11.0", "eslint-config-standard": "^14.1.1", @@ -63,6 +64,9 @@ ], "*.md": [ "prettier --write" + ], + "*.{h,ino,cpp}": [ + "node utils/format-arduino-code" ] } } diff --git a/utils/format-arduino-code.js b/utils/format-arduino-code.js new file mode 100644 index 0000000..ce7ecc9 --- /dev/null +++ b/utils/format-arduino-code.js @@ -0,0 +1,36 @@ +/** + * A small command-line utility used by the git pre-commit hook + * to automatically format the code. + */ + +const { format } = require('astyle'); +const { promises: fs } = require('fs'); + +// Source: https://github.com/arduino/Arduino/blob/ide-1.5.x/build/shared/lib/formatter.conf +const ARDUINO_FORMAT_RULES = [ + 'mode=c', + 'indent=spaces=2', + 'indent-preprocessor', + 'indent-classes', + 'indent-switches', + 'indent-cases', + 'indent-col1-comments', + 'pad-oper', + 'pad-header', + 'keep-one-line-statements', + 'remove-comment-prefix', +].join(' '); + +async function main() { + for (const file of process.argv.slice(2)) { + const source = await fs.readFile(file, 'utf-8'); + console.log(file); + const formatted = await format(source, ARDUINO_FORMAT_RULES); + if (source !== formatted) { + console.log(`* Formating ${file}`); + await fs.writeFile(file, formatted); + } + } +} + +main();