Skip to content

Instantly share code, notes, and snippets.

@Rudde
Rudde / HumanReadableBytes.cs
Created October 8, 2020 10:58
Will convert bytes into readable data units.
using System.Collections.Generic;
using System.Globalization;
namespace HumanReadable
{
public class HumanReadableBytes
{
private readonly Dictionary<Base, Unit[]> _baseUnits = new Dictionary<Base, Unit[]>();
public int DecimalPoints = 2;
public int DecimalsAfterPower = 2;
@Rudde
Rudde / ProgressBar.cs
Created October 7, 2020 09:55
Let you have a moving progressbar in a C# console app.
public class ProgressBar : IDisposable, IProgress<double>
{
private const int blockCount = 10;
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
private const string animation = @"|/-\";
private readonly Timer timer;
private double currentProgress = 0;
private string currentText = string.Empty;
@Rudde
Rudde / ConsoleSpinner.cs
Created October 7, 2020 09:51
Will display a pleasant spinner in your C# console apps.
public class ConsoleSpinner : IDisposable
{
private const string Sequence = @"/-\|";
private int counter = 0;
private readonly int? left;
private readonly int? top;
private readonly int delay;
private bool active;
private readonly Thread thread;
private readonly ConsoleColor? color;
Function Format-Time() {
Param ([int]$ms)
If ($ms -gt 3600000) {[string]::Format("{0:0.00} hours", $ms / 3600000)}
ElseIf ($ms -gt 60000) {[string]::Format("{0:0.00} minutes", $ms / 60000)}
ElseIf ($ms -gt 1000) {[string]::Format("{0:0.00} seconds", $ms / 1000)}
ElseIf ($ms -gt 0) {[string]::Format("{0:0.00} milliseconds", $ms)}
Else {"0 milliseconds"}
}
Function Format-FileSize() {
Param ([long]$size)
If ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
ElseIf ($size -gt 0) {[string]::Format("{0:0.00} B", $size)}
Else {"0 byte"}
}
@Rudde
Rudde / IncrementLetters.cs
Last active August 29, 2017 06:13
Increment letters
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
while (true)
{
@Rudde
Rudde / HTTP Cookie header parser.php
Last active January 20, 2019 19:51 — forked from pokeb/HTTP Cookie header parser
Quick and dirty HTTP cookie header parser in PHP
@Rudde
Rudde / array_filter_key.php
Created May 14, 2016 00:27 — forked from h4cc/array_filter_key.php
Filtering a PHP array by key instead of value.
<?php
/**
* Filtering a array by its keys using a callback.
*
* @param $array array The array to filter
* @param $callback Callback The filter callback, that will get the key as first argument.
*
* @return array The remaining key => value combinations from $array.
*/
@Rudde
Rudde / numSuffix.lib.php
Created May 12, 2016 18:58
Take a number input output it with proper suffix like 1st, 2nd, 3rd, and so on
<?php
/* Written by Barand from phpfreaks.com */
function numSuffix($n) {
$str = "$n";
$t = $n > 9 ? substr($str,-2,1) : 0;
$u = substr($str,-1);
if ($t==1) return $str . 'th';
else switch ($u) {
case 1: return $str . 'st';
@Rudde
Rudde / sec2ts.sh
Created May 10, 2016 00:36
Takes int or float seconds as input will output in format hh:mm:ss.ms where ms is rounded to 3 decimals
#!/bin/bash
function padnum {
NUM=$1
if [ $(echo "$NUM < 10" | bc) -ne 0 ]
then
echo 0$NUM
else
echo $NUM
fi