Skip to content

Instantly share code, notes, and snippets.

View alexfu's full-sized avatar

Alex Fu alexfu

View GitHub Profile
@alexfu
alexfu / EqualSpacingItemDecoration.java
Last active August 22, 2023 07:53
Add equal spacing to RecyclerView items automatically. Can handle horizontal, vertical, and grid display modes
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {
private final int spacing;
private int displayMode;
public static final int HORIZONTAL = 0;
@alexfu
alexfu / docker-compose.yml
Last active April 27, 2023 18:58
Sourcegraph Docker Compose
version: "3.8"
services:
sourcegraph:
image: sourcegraph/server:4.4.2 # 4.4.2 is the last version that doesn't have a repo limit
container_name: leafly-sourcegraph
ports:
- 7080:7080
- 3370:3370
volumes:
- "~/.sourcegraph/config:/etc/sourcegraph"
@alexfu
alexfu / DateTimeExtensions.kt
Created July 22, 2022 17:29
Do you wish there was an easier way to write dates in Kotlin?
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Month
import java.time.ZonedDateTime
import java.util.TimeZone
fun January(day: Int, year: Int) = LocalDate.of(year, Month.JANUARY, day)
fun February(day: Int, year: Int) = LocalDate.of(year, Month.FEBRUARY, day)
fun March(day: Int, year: Int) = LocalDate.of(year, Month.MARCH, day)
fun April(day: Int, year: Int) = LocalDate.of(year, Month.APRIL, day)
@alexfu
alexfu / DividerItemDecoration.java
Last active February 9, 2023 05:09
An ItemDecoration that draws dividers between items. Pulled from Android support demos.
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@alexfu
alexfu / ColorUtils.java
Last active January 30, 2023 00:04
Automatic text color selection using relative luminance.
public class ColorUtils {
private static final double LM_RED_COEFFICIENT = 0.2126;
private static final double LM_GREEN_COEFFICIENT = 0.7152;
private static final double LM_BLUE_COEFFICIENT = 0.0722;
public static int calculateRelativeLuminance(int color) {
int red = (int) (Color.red(color) * LM_RED_COEFFICIENT);
int green = (int) (Color.green(color) * LM_GREEN_COEFFICIENT);
int blue = (int) (Color.blue(color) * LM_BLUE_COEFFICIENT);
return red + green + blue;
@alexfu
alexfu / ghof
Last active August 22, 2022 17:53
GitHub Open File (GHOF)
#!/usr/bin/env bash
# Prints the GitHub URL of the chosen file. Requires fzf.
FILE=$1
if [ -z "${FILE}" ]
then
FILE=$(fzf)
fi
CURRENT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
@alexfu
alexfu / PrefixedEditText.java
Last active July 11, 2022 10:41
EditText with support for prefixes.
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;
/**
@alexfu
alexfu / geocode
Created November 1, 2018 13:55
Bash script that geocodes a location.
#!/bin/bash
BASE_URL="https://maps.googleapis.com/maps/api/geocode/json"
API_KEY="" # Obtain an API key from https://developers.google.com/maps/documentation/geocoding/get-api-key
ADDRESS=$1
curl -s -G $BASE_URL --data-urlencode "address=$1" --data-urlencode "key=$API_KEY" | jq '.results[] | { name: .formatted_address, latLng: "\(.geometry.location.lat),\(.geometry.location.lng)" }'

git addi

Add git files interactively from the command line.

asciicast

Requirements

@alexfu
alexfu / git-eject
Last active May 13, 2021 17:59
git eject
#!/bin/bash
# Bails on current working branch and switches to main
# Install: Place this script anywhere that is in your $PATH and make it executable (chmod +x)
BRANCH_TO_EJECT=$(git rev-parse --abbrev-ref HEAD)
git reset HEAD --hard
git checkout main
git br -D $BRANCH_TO_EJECT