Compare commits

...

7 commits

17 changed files with 395 additions and 4 deletions

View file

@ -3,5 +3,6 @@ _: {
./shared.nix
./desktop.nix
./wayland.nix
./scripts
];
}

View file

@ -0,0 +1,14 @@
{
pkgs,
...
}: let
scriptPkgs = (import ./packages {inherit pkgs;});
in {
home.packages = with scriptPkgs; [
bitcoin
cheatsh
colors256
unix
];
}

View file

@ -0,0 +1,18 @@
{pkgs, ...}:
(pkgs.writeShellApplication {
name = "bitcoin";
runtimeInputs = with pkgs; [coreutils curl jq];
text = ''
#!/bin/sh
BTC_DATA=$(curl https://api.coindesk.com/v1/bpi/currentprice.json 2>/dev/null || echo 'ERR')
if [ "$BTC_DATA" != "ERR" ]; then
BTC_PRICE=$(echo "$BTC_DATA" | jq -r ".bpi.USD.rate_float")
BTC_PRICE=$(printf "%.0f" "$BTC_PRICE")
echo \$"$BTC_PRICE"
else
echo "N/A"
fi
'';
})

View file

@ -0,0 +1,112 @@
#!/bin/sh
# rm_trailing_slashes(string)
#
# Prints a string without any trailing slashes.
# This is used because cheat.sh doesn't play nicely with multiple slashes in
# URLs.
rm_trailing_slashes() {
string="${1-}"
last_char="$(printf "%s" "$string" | tail -c 1)"
if [ "$last_char" = "/" ]; then
rm_trailing_slashes "${string%?}"
else
echo "$string"
fi
}
# pick_category(must_match, query, argument, recurse)
#
# Pick cheat.sh category.
# if must_match is 1, only allow listed options to be picked.
# if query is specified, pick sub-category of it, else pick global categories.
# if argument is specified, optionally perform must_match check and print it.
# if recurse is 1, if the selected option ends with /, run the function again.
#
# Prints the chosen category
pick_category() {
must_match="$1"
query="$(rm_trailing_slashes "$2")"
argument="$3"
recurse="$4"
# Query all possible options
if [ -n "$query" ]; then
url="cheat.sh/$query/:list"
else
url="cheat.sh/:list"
fi
selectable="$(curl -s "$url")"
# If argument is specified, print it, optionally perform must_match check.
if [ -n "$argument" ]; then
if [ "$must_match" -ne 1 ] || echo "$selectable" | grep -qe "\b$1\b"; then
selected="$argument"
else
echo "Invalid selection: '$argument'"
echo "For all selections, query $url"
exit 1
fi
# Select the option with fzf, optionally allow other matches if must_match isn't set.
else
if [ "$must_match" -ne 1 ]; then
if [ -z "$selectable" ]; then
header="No selections found, you can use empty query to show category help, or type a custom query."
else
header="Use alt-enter to enter non-listed query. You can use empty queries to show category help."
fi
selected="$(
printf "\n%s" "$selectable" |
fzf --bind=alt-enter:print-query \
--print-query \
--prompt="cheat.sh/$query query>" \
--header="$header"
)"
else
selected=$(printf "%s" "$selectable" | fzf --prompt="cheat.sh/$query category>")
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "Invalid selection: '$selected'"
echo "For all selections, query $url"
exit 1
fi
fi
selected=$(printf "%s" "$selected" | tail -1)
fi
# Replace spaces with '+' (cheat.sh resolves those as spaces)
selected="$(echo "$selected" | tr ' ' '+')"
# Prepend the original query, if we have one
# Print the selected category, or subcategory with the category
if [ -n "$query" ]; then
result="$query/$selected"
else
result="$selected"
fi
# Recurse, if specified and the result ended with /
if [ "$recurse" -eq 1 ]; then
if [ "$(printf "%s" "$selected" | tail -c 1)" = "/" ]; then
result="$(pick_category "$must_match" "$result" "$argument" 1)"
fi
fi
# Print the result
printf "%s" "$result"
}
# Select the cheatsheat category (language/core-util/...)
query=$(pick_category 1 "" "${1-}" 0)
# If the query isn't already complete, select a sub-category
if ! echo "$query" | grep -qe ":"; then
query="$(pick_category 0 "$query" "${2-}" 1)"
fi
# Construct the URL from given query and print it
url="cheat.sh/$query"
echo "$url"
# Show the output of cheat.sh request
curl -s "$url"

View file

@ -0,0 +1,8 @@
{pkgs, ...}:
(pkgs.writeShellApplication {
name = "cheat.sh";
runtimeInputs = with pkgs; [coreutils curl jq gnugrep fzf];
text = ''
${builtins.readFile ./cheat.sh}
'';
})

View file

@ -0,0 +1,101 @@
#!/usr/bin/env bash
# # Print out 256 colors, with each number printed in its corresponding colour
#
# This file is uploaded on <https://gist.githubusercontent.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263/raw/>
# It was originally intended to be used as an alias that curled this URL and piped it into bash, however
# this is very unsafe as the owner can change the content of this gist at his convenience, meaning it was
# a potential security vulnerability. This is a script version of this alias to avoid this issue.
#
# The copyright for this file belongs to the original author: Tom Hale, 2016
# This file was released under MIT license
set -eu # Fail on errors or undeclared variables
printable_colours=256
# Return a colour that contrasts with the given colour
# Bash only does integer division, so keep it integral
function contrast_colour {
colour="$1"
if ((colour < 16)); then # Initial 16 ANSI colours
((colour == 0)) && printf "15" || printf "0"
return
fi
# Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
if ((colour > 231)); then # Greyscale ramp
((colour < 244)) && printf "15" || printf "0"
return
fi
# All other colours:
# 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
# See http://stackoverflow.com/a/27165165/5353461
# r=$(( (colour-16) / 36 ))
g=$((((colour - 16) % 36) / 6))
# b=$(( (colour-16) % 6 ))
# If luminance is bright, print number in black, white otherwise.
# Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601
((g > 2)) && printf "0" || printf "15"
return
# Uncomment the below for more precise luminance calculations
# # Calculate percieved brightness
# # See https://www.w3.org/TR/AERT#color-contrast
# # and http://www.itu.int/rec/R-REC-BT.601
# # Luminance is in range 0..5000 as each value is 0..5
# luminance=$(( (r * 299) + (g * 587) + (b * 114) ))
# (( $luminance > 2500 )) && printf "0" || printf "15"
}
# Print a coloured block with the number of that colour
function print_colour {
local colour="$1" contrast
contrast=$(contrast_colour "$1")
printf "\e[48;5;%sm" "$colour" # Start block of colour
printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number
printf "\e[0m " # Reset colour
}
# Starting at $1, print a run of $2 colours
function print_run {
local i
for ((i = "$1"; i < "$1" + "$2" && i < printable_colours; i++)); do
print_colour "$i"
done
printf " "
}
# Print blocks of colours
function print_blocks {
local start="$1" i
local end="$2" # inclusive
local block_cols="$3"
local block_rows="$4"
local blocks_per_line="$5"
local block_length=$((block_cols * block_rows))
# Print sets of blocks
for ((i = start; i <= end; i += (blocks_per_line - 1) * block_length)); do
printf "\n" # Space before each set of blocks
# For each block row
for ((row = 0; row < block_rows; row++)); do
# Print block columns for all blocks on the line
for ((block = 0; block < blocks_per_line; block++)); do
print_run $((i + (block * block_length))) "$block_cols"
done
((i += block_cols)) # Prepare to print the next row
printf "\n"
done
done
}
print_run 0 16 # The first 16 colours are spread over the whole spectrum
printf "\n"
print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive
print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey

View file

@ -0,0 +1,9 @@
{pkgs, ...}:
(pkgs.writeShellApplication {
name = "colors-256";
runtimeInputs = with pkgs; [coreutils];
text = ''
${builtins.readFile ./colors-256.sh}
'';
})

View file

@ -0,0 +1,13 @@
{
pkgs,
...
}: let
packages = {
bitcoin = pkgs.callPackage ./bitcoin.nix {};
cheatsh = pkgs.callPackage ./cheatsh {};
colors256 = pkgs.callPackage ./colors256 {};
unix = pkgs.callPackage ./unix {};
};
in
packages

View file

@ -0,0 +1,10 @@
{pkgs, ...}:
(pkgs.writeShellApplication {
name = "unix";
runtimeInputs = with pkgs; [coreutils];
text = ''
${builtins.readFile ./unix.sh}
'';
})

View file

@ -0,0 +1,26 @@
#!/bin/sh
#original artwork by http://www.sanderfocus.nl/#/portfolio/tech-heroes
#converted to shell by #nixers @ irc.unix.chat
cat <<'eof'
,_ ,_==▄▂
, ▂▃▄▄▅▅▅▂▅¾. / /
▄▆<´ "»▓▓▓%\ / / / /
,▅7" ´>▓▓▓% / / > / >/%
▐¶▓ ,»▓▓¾´ /> %/%// / /
▓▃▅▅▅▃,,▄▅▅▅Æ\// ///>// />/ /
V║«¼.;→ ║<«.,`=// />//%/% / /
//╠<´ -²,)(~"-╝/¾/ %/>/ />
/ / / ▐% -./▄▃▄▅▐, /7//;//% / /
/ ////`▌▐ %zWv xX▓▇▌//&;% / /
/ / / %//%/¾½´▌▃▄▄▄▄▃▃▐¶\/& /
</ /</%//`▓!%▓%╣[38;5;255;WY<Y)y&/`\
/ / %/%//</%//\i7; ╠N>)VY>7; \_ UNIX IS VERY SIMPLE IT JUST NEEDS A
/ /</ //<///<_/%\▓ V%W%£)XY _/%‾\_, GENIUS TO UNDERSTAND ITS SIMPLICITY
/ / //%/_,=--^/%/%%%¶%%} /%%%%%%;\,
%/< /_/ %%%%%;X%%\%%;, _/%%%;, \
/ / %%%%%%;, \%%l%%;// _/%;, dmr
/ %%%;, <;\-=-/ /
;, l
eof

View file

@ -1,6 +1,6 @@
(defwidget bitcoin_module []
(eventbox
:onclick "~/.local/bin/scripts/cli/bitcoin | xargs -I_ ${EWW_CMD} update bitcoin=_"
:onclick "bitcoin | xargs -I{} ${EWW_CMD} update bitcoin={}"
:class "module bitcoin"
(box

View file

@ -37,7 +37,7 @@
(defpoll bitcoin
:interval "5m"
:initial "$N/A"
`~/.local/bin/scripts/cli/bitcoin`)
`bitcoin`)
; TODO: Figure out how to store this one-time
(defpoll kernel

View file

@ -6,6 +6,8 @@
}: let
inherit (lib) mkIf;
scriptPkgs = (import ../../../../packages/cli/scripts/packages {inherit pkgs;});
cfg = osConfig.myOptions.home-manager.programs.bars.eww;
in {
config = mkIf cfg.enable {
@ -21,6 +23,7 @@ in {
python3
bash
coreutils
findutils
gnugrep
gawk
netcat-openbsd
@ -36,6 +39,7 @@ in {
wireplumber
pulseaudio
hyprland
scriptPkgs.bitcoin
];
in {
Unit = {
@ -66,7 +70,7 @@ in {
Unit = {
Description = "Open bar0 eww (ElKowar's Wacky Widgets) window";
After = [ "eww.service" ];
PartOf = [ "graphical-session.target" ];
PartOf = [ "graphical-session.target" "eww.service" ];
};
Service = {

View file

@ -1,4 +1,10 @@
{
pkgs,
...
}: let
scriptPkgs = (import ./bin {inherit pkgs;});
in {
programs.git = {
aliases = {
quickclone = "clone --single-branch --depth=1";
@ -48,7 +54,7 @@
bD = "branch --delete --force";
bm = "branch --move";
bM = "branch --move --force";
bb = "!~/.local/bin/scripts/cli/better-git-branch";
bb = "!${scriptPkgs.better-git-branch}/bin/better-git-branch";
r = "rebase";
ri = "rebase -i";

View file

@ -0,0 +1,56 @@
#!/usr/bin/env bash
# Source: https://gist.github.com/schacon/e9e743dee2e92db9a464619b99e94eff
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COLOR='\033[0m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
NO_COLOR='\033[0m'
width1=5
width2=6
width3=30
width4=20
width5=20
# Function to count commits
count_commits() {
local branch="$1"
local base_branch="$2"
local ahead_behind
ahead_behind=$(git rev-list --left-right --count "$base_branch"..."$branch")
echo "$ahead_behind"
}
# Main script
main_branch=$(git rev-parse HEAD)
printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" "Ahead" "Behind" "Branch" "Last Commit" " "
# Separator line for clarity
printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" "-----" "------" "------------------------------" "-------------------" " "
format_string="%(objectname:short)@%(refname:short)@%(committerdate:relative)"
IFS=$'\n'
for branchdata in $(git for-each-ref --sort=-authordate --format="$format_string" refs/heads/ --no-merged); do
sha=$(echo "$branchdata" | cut -d '@' -f1)
branch=$(echo "$branchdata" | cut -d '@' -f2)
time=$(echo "$branchdata" | cut -d '@' -f3)
if [ "$branch" != "$main_branch" ]; then
# Get branch description
description=$(git config branch."$branch".description)
# Count commits ahead and behind
ahead_behind=$(count_commits "$sha" "$main_branch")
ahead=$(echo "$ahead_behind" | cut -f2)
behind=$(echo "$ahead_behind" | cut -f1)
# Display branch info
# shellcheck disable=SC2086
printf "${GREEN}%-${width1}s ${RED}%-${width2}s ${BLUE}%-${width3}s ${YELLOW}%-${width4}s ${NO_COLOR}%-${width5}s\n" $ahead $behind $branch "$time" "$description"
fi
done

View file

@ -0,0 +1,4 @@
{pkgs, ...}:
pkgs.writeShellScriptBin "better-git-branch" ''
${builtins.readFile ./better-git-branch.sh}
''

View file

@ -0,0 +1,9 @@
{
pkgs,
...
}: let
packages = {
better-git-branch = pkgs.callPackage ./better-git-branch {};
};
in
packages