Skip to content

Instantly share code, notes, and snippets.

Avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / KeepWindowsSessionAlive.ps1
Created Oct 2, 2020
PowerShell script to keep Windows session alive by repeatedly toggling numlock.
View KeepWindowsSessionAlive.ps1
$wshell = New-Object -ComObject WScript.Shell
while ($true) {
Write-Output "$(Get-Date) Toggling numlock"
$whell.SendKeys('{NUMLOCK}')
Start-Sleep -Seconds 10
}
@mminer
mminer / convert_to_image_sequence.sh
Created Aug 10, 2020
Converts .mov files to image sequences using FFmpeg.
View convert_to_image_sequence.sh
#!/usr/bin/env bash
for filename in "$@"; do
base=$(basename "$filename" .mov)
mkdir "$base"
ffmpeg -i "$filename" "$base/$base-%03d.png"
done
@mminer
mminer / storageArray.ts
Created Mar 9, 2020
Utility class to read and write arrays in localStorage / sessionStorage.
View storageArray.ts
class StorageArray {
private storage: Storage;
constructor(storage: Storage) {
this.storage = storage;
}
get(key: string): string[] | null {
const jsonString = this.storage.getItem(key);
@mminer
mminer / VerticalCollapse.tsx
Created Jan 2, 2020
React component for expanding and collapsing container with dynamic height.
View VerticalCollapse.tsx
import React, {forwardRef, useImperativeHandle, useRef} from 'react';
interface Props {
children: React.ReactNode;
className?: string;
collapsedHeight: number;
isCollapsed: boolean;
transitionDuration?: string;
}
@mminer
mminer / testequality.sh
Created Dec 10, 2019
Basic Bash script to test equality of two inputs with coloured output.
View testequality.sh
#!/usr/bin/env bash
actual=$1
expected=$2
# https://stackoverflow.com/a/5947802
GREEN='\033[0;32m'
RED='\033[0;31m'
RESET='\033[0m' # No Color
@mminer
mminer / MultiReadHttpServletRequest.java
Created Aug 21, 2019
Servlet request wrapper that allows reading request body multiple times.
View MultiReadHttpServletRequest.java
package com.matthewminer;
import org.apache.commons.io.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@mminer
mminer / lruCache.ts
Created Jul 4, 2019
Simple least-recently inserted cache.
View lruCache.ts
export default class LeastRecentlyInsertedCache<T> implements Iterable<T> {
private maxSize: number;
// Set objects maintain insertion order, making them appropriate for keeping track of when values
// are added relative to each other.
private set = new Set<T>();
constructor(maxSize: number) {
this.maxSize = maxSize;
}
@mminer
mminer / basicAuthMiddleware.js
Created Dec 12, 2018
Express middleware for HTTP Basic Authentication.
View basicAuthMiddleware.js
function basicAuthMiddleware(request, response, next) {
const { authorization = '' } = request.headers;
const [, credentials] = authorization.split(' ');
const decoded = new Buffer(credentials || '', 'base64').toString();
const splitIndex = decoded.indexOf(':');
const username = decoded.substring(0, splitIndex);
const password = decoded.substring(splitIndex + 1);
// TODO: supply valid values for username and password
if (username !== USERNAME || password !== PASSWORD) {
@mminer
mminer / InactivityReset.js
Created Dec 11, 2018
React component that runs an action after no user activity is detected.
View InactivityReset.js
import PropTypes from 'prop-types';
import { Component } from 'react';
const eventListenerOptions = { capture: true, passive: true };
// Events that signify that the user is still active.
const eventNames = ['keydown', 'mousedown', 'touchstart'];
class InactivityReset extends Component {
constructor(props) {
@mminer
mminer / RelativeTime.js
Created Dec 11, 2018
React component that displays an auto-updating relative time.
View RelativeTime.js
import moment from 'moment';
import PropTypes from 'prop-types';
import { Component, createElement as h } from 'react';
class RelativeTime extends Component {
constructor(props) {
super(props);
this.intervalId = null;
this.state = { output: moment(props.time).fromNow() };
this.updateOutput = this.updateOutput.bind(this);
You can’t perform that action at this time.