Skip to content

Instantly share code, notes, and snippets.

@PaulChana
PaulChana / UniformDistribution.cpp
Last active March 12, 2018 11:49
C++ uniform distribution random numbers
#include <random>
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 6);
const int rnd = dis(gen);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(-1.f, 1.f);
@PaulChana
PaulChana / ShowExtended.sh
Created October 6, 2016 09:55
Show extended attributes in terminal
ls -al@
@PaulChana
PaulChana / NSTaskAsyncOutput.mm
Created October 17, 2016 09:12
NSTask Async output to NSTaskView
NSMutableArray *arguments = [[NSMutableArray alloc] init];
[arguments addObject:[[NSBundle mainBundle] pathForResource:@"MyScript" ofType:@"py"]];
[arguments addObject:@"--verbose"]; // Any arguments you want here...
NSTask* task = [[NSTask alloc] init];
task.launchPath = @"/usr/bin/python";
task.arguments = arguments;
NSMutableDictionary *defaultEnv = [[NSMutableDictionary alloc] initWithDictionary:[[NSProcessInfo processInfo] environment]];
[defaultEnv setObject:@"YES" forKey:@"NSUnbufferedIO"] ;
@PaulChana
PaulChana / DragAndDropTextField.h
Created October 17, 2016 11:07
NSTextField enabled for drag and drop from finder
#import <Cocoa/Cocoa.h>
@interface DragAndDropTextField : NSTextField
@end
@PaulChana
PaulChana / LLDB String Length
Created December 1, 2016 17:20
Set string length in LLDB
set set target.max-string-summary-length 10000
@PaulChana
PaulChana / ShowHideCursor.c
Created June 6, 2017 10:56
Show / Hide the cursor on a console
void setConsoleCursorVisibility (const bool visible)
{
if (! visible)
std::cout << "\033[?25l" << std::flush;
else
std::cout << "\033[?25h" << std::flush;
}
@PaulChana
PaulChana / sizeofcarray.c
Created June 6, 2017 11:23
Size of c array
(sizeof(a) / sizeof(*a))
@PaulChana
PaulChana / appendVector.cpp
Last active August 8, 2017 08:28
Append vector
a.insert (std::end (a), std::begin (b), std::end (b));
// Less efficient but can be used for const members:
std::copy (std::begin (b), std::end (b), std::back_inserter (a));
@PaulChana
PaulChana / StringLiteral.cpp
Created August 8, 2017 09:09
String literal in C++
const auto myString = R"identifier(
This string wont be "processed" like a normal string
Line breaks will be preserved
And you wont need to do escapes...
)identifier";
@PaulChana
PaulChana / PackUnpackARGB.cpp
Created August 8, 2017 09:27
Pack and unpack ARGB
int pack (int a, int r, int g, int b)
{
return a << 24 + r << 16 + g<< 8 + b;
}
void unpack (int& a, int& r, int& g, int& b, const int argb)
{
a = (argb >> 24) & 0xFF;
r = (argb >> 16) & 0xFF;
g = (argb >> 8) & 0xFF;