Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Hashbrown777 / prompt.bashrc
Created February 9, 2020 07:09
neat coloured prompt for bash that shows `user@pcname:pwd\n$?>` where $? changes between red and green if it was an error or no
prompt-color() { printf '\\[\e[38;5;%sm\\]%s\\[\e[m\\]' "$1" "$2"; }
PS1="$(printf '%s' \
'\n' \
`prompt-color 226 '\u'` \
'@' \
`prompt-color 208 '\h'` \
':' \
`prompt-color 207 '\w'` \
'\n' \
`prompt-color '$(( $?==0 ? 82 : 196 ))' '$?'` \
@Hashbrown777
Hashbrown777 / hairOfTheDiff.sh
Created February 9, 2020 07:21
compare files line-by-line, showing differences within each line by colour-highlighting (or visual symbols)
#!/bin/bash
same='-' #unchanged
up='△' #exists in first line, but not in second
down='▽' #exists in second line, but not in first
reset=''
reset=$'\e[0m'
same=$reset
up=$reset$'\e[1m\e[7m'
@Hashbrown777
Hashbrown777 / blameDiff.sh
Created February 9, 2020 07:28
performs an `svn diff` but includes `svn annotate` metadata
function blameDiff() {
file="$1"
rev1="$2"
rev2="$3"
#default to HEAD if omitted
if [ -n "$rev1" ]
then
title1="(revision $rev1)"
else
@Hashbrown777
Hashbrown777 / lockMonitor.sh
Created February 9, 2020 07:50
outputs "Locked" & "Unlocked" + `date` every time the [fedora+plasma] machine's screen is locked/unlocked. Add to startup script like `nohup lockMonitor.sh >>locklog.txt &`
#!/bin/bash
#prints out, among other things;
# string "org.kde.screensaver"
#transform it to 'org.kde.screensaver'
service=$(\
dbus-send \
--session \
--dest=org.freedesktop.DBus \
--type=method_call \
@Hashbrown777
Hashbrown777 / rezip.sh
Created February 9, 2020 07:53
rezip an archive at a higher compression rate with no temp files. reading zip metadata, demo of unzipping/zipping a stream, and deleting/renaming files inside a zip
zipinfo -1 A.zip | while read filename
do
unzip -p A.zip "$filename" | zip -9 A.zip -
zip --delete A.zip "$filename"
printf "@ -\n@=$filename\n" | zipnote -w A.zip
done
@Hashbrown777
Hashbrown777 / change_reddit_wallpaper.py
Created February 9, 2020 08:03
downloads and sets the desktop background to today's top hot image from the specified subreddits
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import argparse
import ctypes
import os
#requires version 3.5 of praw
import praw
import platform
import re
@Hashbrown777
Hashbrown777 / patcher.sh
Last active February 17, 2020 08:21
performs releases from zips meant to overwrite live files, backs up & allows you to preview changes beforehand
#!/bin/bash
method="$1"
name="$2"
archives="$(dirname "${BASH_SOURCE[0]}")"
live=`pwd`
release="$archives/$name.zip"
backup="$archives/${name}_BAK.zip"
log="$live/released.txt"
@Hashbrown777
Hashbrown777 / dedupe.ps1
Last active March 9, 2021 23:05
Deduplicates identical files with preferences for which filenames to choose between the options.
$dirs='.'
Get-ChildItem -Path 'dedupe' | Remove-Item -Recurse
$dirs = $dirs | %{ ($_ | Get-Item).FullName }
$matchDirs = '^(' + (($dirs | %{ [Regex]::Escape($_) }) -join '|') + ')'
$preferences = (
('Act \d{4}( No \d+| \(\d{4} No[. ]\d+\))? - \d{1,2}\.\d{1,2}\.\d{4}\.pdf$', 'Act,( \d{4},?)?( No \d+| \(\d{4} No[. ]\d+\))? - \d{1,2}\.\d{1,2}\.\d{4}\.pdf$' ),
('Act \d{4} No \d+ - \d{1,2}\.\d{1,2}\.\d{4}\.pdf$' , 'Act( No \d+)? - \d{1,2}\.\d{1,2}\.\d{4}\.pdf$' ),
('Act \d{4} No \d+ - \d{1,2}\.\d{1,2}\.\d{4}\.pdf$' , 'Act,? \d{4},? No\.\d+ - \d{1,2}\.\d{1,2}\.\d{4}\.pdf$' ),
@Hashbrown777
Hashbrown777 / resetEnv.ps1
Created February 9, 2020 08:14
resets an environment variable to what it would be if you opened a new prompt
function resetEnv {
Set-Item `
-Path (('Env:', $args[0]) -join '') `
-Value ((
[System.Environment]::GetEnvironmentVariable($args[0], "Machine"),
[System.Environment]::GetEnvironmentVariable($args[0], "User")
) -match '.' -join ';')
}
@Hashbrown777
Hashbrown777 / selfElevate.ps1
Created March 26, 2021 11:39
when put at the top of a script ensures it is [re]ran as admin
#at top of script
if (!
#current role
(New-Object Security.Principal.WindowsPrincipal(
[Security.Principal.WindowsIdentity]::GetCurrent()
#is admin?
)).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator
)
) {