Skip to content

Instantly share code, notes, and snippets.

@mjohnsullivan
mjohnsullivan / http_server.rs
Last active March 12, 2024 16:08
Simple HTTP server example for Rust
// Updated example from http://rosettacode.org/wiki/Hello_world/Web_server#Rust
// to work with Rust 1.0 beta
use std::net::{TcpStream, TcpListener};
use std::io::{Read, Write};
use std::thread;
fn handle_read(mut stream: &TcpStream) {
let mut buf = [0u8 ;4096];
@mjohnsullivan
mjohnsullivan / MainActivity.java
Last active March 1, 2024 01:28
Android Wear activity that reads and displays sensor data from the device
package com.example.wear;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
@mjohnsullivan
mjohnsullivan / streams_with_async.dart
Created December 3, 2019 17:55
Example of how to transform streams of data in Dart/Flutter when the transform involves asynchronous calls.
import 'dart:async';
import 'dart:math' show Random;
final random = Random();
// Mock data for a list of messages
const messageList = [
{
'message': 'Message 1',
'timestamp': 1,
@mjohnsullivan
mjohnsullivan / book_list.dart
Last active December 1, 2023 21:53
A simple book list Flutter example using the Google Books API
/*
Copyright 2018 The Chromium Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
@mjohnsullivan
mjohnsullivan / parse_json.go
Created December 14, 2015 23:17
Parse JSON objects with arbitrary key names in Go using interfaces and type assertions
// Parsing arbitrary JSON using interfaces in Go
// Demonstrates how to parse JSON with abritrary key names
// See https://blog.golang.org/json-and-go for more info on generic JSON parsing
package main
import (
"encoding/json"
"fmt"
)
@mjohnsullivan
mjohnsullivan / particles.dart
Last active September 13, 2023 04:52
Demonstrates drawing and animating simple shapes in Flutter
// Adapted from https://github.com/nhancv/nc_flutter_util
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
main() {
runApp(new MaterialApp(
@mjohnsullivan
mjohnsullivan / rows_columns.dart
Created July 13, 2018 17:17
Using rows and columns to create an asymmetric grid-like layout
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
@mjohnsullivan
mjohnsullivan / icon_gradient.dart
Created November 25, 2019 22:09
Apply a color gradient to an icon in Flutter
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
@mjohnsullivan
mjohnsullivan / download.py
Last active December 17, 2022 10:05
Python HTTP download with resume and optional MD5 hash checking
import os.path
import shutil
import hashlib
import logging
# Support both Python 2 and 3 urllib2 importing
try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, Request
@mjohnsullivan
mjohnsullivan / scroll_detection.dart
Created July 11, 2018 17:13
Detect when a scrollable widget starts or stops scrolling
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override