The initial idea comes from a discussion with friends over dinner a couple of
nights ago, when we realized
ar(1) can read input from
STDIN. While we were
wondering why that would be useful and what it could be used for, one of use
gcc(1) can also read input from
STDIN. From there, the
conversation derailed into how to “misuse” that feature in order to do something
completely over the top and useless.
Inspired by Kelsey Hightower’s
nocode idea, we wanted to
gcc(1) to write our code directly on
STDIN, compile it, and, if successful,
add and commit it to a git repo. Obviously doing all of this by hand wouldn’t cut
it, so all of it had to be “automated” or neatly wrapped into a useable script.
Which then gave birth to
Nocode: A simple way to write clean single-file code without taking up disk space.
Nocode is essentialy a bash script which wraps all of the useful steps.
It starts of by creating and filling a
.gitignore file if none exist, as to
not create git clutter with the following steps.
if [ ! -f ".gitignore" ]; then $(echo "a.out" >> ".gitignore") $(echo -n "*.c" >> ".gitignore") fi
The script then goes on to read the code from
STDIN, in order to compiling it.
Before passing the input to
gcc(1) it is first given to
order to apply the coding style the user wants. This is then input to
order to save it until it is committed to our git repository, and finally into
gcc(1) for compilation.
# "$ARG" is equal to the first argument given to Nocode $(clang-format | tee "$ARG" | gcc -x c -)
The next step is to check if the script was called in a git repository, if not the user is prompted to initialize one. If the user refuses, the script stops and the generated C file is saved.
# Check if we're it a git repository $(git status &>/dev/null) if [ "$?" -ne 0 ]; then read -p "Not a git repository, do you want to create one? [Y/n] " USERCREATEGITINPUT if [ "$USERCREATEGITINPUT" = "n" ]; then echo "nocode.sh: leaving file as is" exit 0 fi $(git init &>/dev/null) fi
To finish everything off, the file is added and committed to the repository before finally being deleted.
# Perform all git actions except push, left up to the user $(git add "$ARG" &>/dev/null) $(git commit -m "$ARG: update" &>/dev/null) # Remove file because we're #NoCodeCompliant $(rm "$ARG")
Of course, not all of the boilerplate is presented here because it is not very interesting. The scource code is available on GitHub. Issues and PRs are welcome!
All you have to do to use
Nocode is call it with the filename you want to
save, as follows:
Once you are done writing your code on
STDIN, you can use
CTRL+D to send
EOF in order to tell
clang-format that you are done typing.
Pushing the code to the repository’s remote is left up to the user in order to avoid dealing with git credentials.