Skip to content

Instantly share code, notes, and snippets.

View bew's full-sized avatar
😎
https://nohello.net

Benoit de Chezelles bew

😎
https://nohello.net
View GitHub Profile
@bew
bew / keymap.c
Created February 10, 2017 12:00 — forked from pocketkk/keymap.c
Chord and Pick Keymap
// This is the canonical layout file for the Quantum project. If you want to add another keyboard,
// this is the style you want to emulate.
#include "planck.h"
#ifdef BACKLIGHT_ENABLE
#include "backlight.h"
#endif
// Each layer gets a name for readability, which is then used in the keymap matrix below.
// The underscores don't mean anything - you can have a layer called STUFF or any other name.
@bew
bew / json_mapper.cr
Created March 13, 2017 04:52
A JSON.mapper for crystal that allow additional fields
module JSON
# JSON.mapping documentation removed, as it's taking to much spaces :p
# prop: the properties, like JSON.mapping
# decl: whatever or not to generate the variables declarations, getters & setters
# more_init: if not nil, must be a TupleLiteral that is the list of additional fields to the initialize method
macro mapper(props properties, strict = false, decl gen_declarations = true, more_init = nil)
{% for key, value in properties %}
{% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
@bew
bew / material_colors_hexs.json
Created March 18, 2017 18:35
All material colors in hex
[
{
"name": "red",
"key": "red",
"android": "red",
"default": {
"strength": 500,
"hex": "#f44336",
"contrast": "white"
},
@bew
bew / OOM error handling.cr
Last active May 5, 2017 01:22
Test for OutOfMemory exception
Kilobyte = 1024u32
Megabyte = 1024u32 * Kilobyte
Gigabyte = 1024u32 * Megabyte
def write_debug(str)
LibC.write 1, str, str.size
end
class FatalOOMError < Exception
def initialize
@bew
bew / rpc_call_concept.cr
Last active August 12, 2017 01:24
Cool micro RPC fake calls with object serialization, deserialization, method call
module SharedNode
ADDRESS_PROXY = {} of Int32 => {UInt64, Node.class}
@@last_id = 0
def self.add_node(node)
@@last_id += 1
ADDRESS_PROXY[@@last_id] = {node.as(Void*).address, node.class}
@@last_id
end
module Contract
# We put everything in `included` macro so the constants don't clashes
# when used in multiple classes
macro included
CONTRACTS_FOR_DEF = {} of _ => _
macro __add_contract(contract)
\{% if CONTRACTS_FOR_DEF[:next_def] == nil %}
\{% CONTRACTS_FOR_DEF[:next_def] = [contract] %}
\{% else %}
\{% CONTRACTS_FOR_DEF[:next_def] << contract %}
@bew
bew / noprelude_exception.cr
Last active March 9, 2024 17:59
Minimal noprelude exception example in Crystal, with the generated LLVM IR code
# Compile it with: crystal build --prelude=empty --emit llvm-ir noprelude_exception.cr
lib LibC
alias SizeT = UInt64
fun printf(format : UInt8*, ...) : Int32
fun exit(exit_code : Int32) : NoReturn
end
require "callstack/lib_unwind"
// Super Fast Blur v1.1
// by Mario Klingemann <http://incubator.quasimondo.com>
//
// Tip: Multiple invovations of this filter with a small
// radius will approximate a gaussian blur quite well.
//
BImage a;
BImage b;
void setup()
@bew
bew / 5_combinators.cr
Created February 15, 2018 01:32
Functional programming in Crystal to implement parser combinators
# Following: https://fsharpforfunandprofit.com/posts/understanding-parser-combinators/
require "spec"
# Result types
record Success(T), value : T, remaining : String do
def_equals_and_hash value, remaining
end
@bew
bew / concurrent_rpc.cr
Last active March 9, 2024 17:57
Simple implementation of a concurrent server handling requests on STDIN
# How to use / how it works:
#
# NOTE: The server reads on STDIN, and writes on STDOUT
#
# Start the program. Now you can write something then press enter.
# The server will do something 'useful' with the input, and gives you the result.
#
# The requests/responses are handled asynchronously, and can handle multiple requests at the same time.
#
# So you can spam the input (write a lot of lines) then close the input (Ctrl-d on a terminal), and