From 8f862e91e0e9f56bd3672c01897cd7105658efea Mon Sep 17 00:00:00 2001 From: pips Date: Tue, 4 Oct 2016 23:52:17 +0200 Subject: [PATCH] Inital commit for simple tools repo --- color.sh | 27 ++++++++++++++ color.simple.sh | 11 ++++++ justify | 22 ++++++++++++ removenl | 22 ++++++++++++ spacesbreak | 41 +++++++++++++++++++++ stpaster | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ trim | 67 ++++++++++++++++++++++++++++++++++ 7 files changed, 285 insertions(+) create mode 100755 color.sh create mode 100755 color.simple.sh create mode 100755 justify create mode 100755 removenl create mode 100755 spacesbreak create mode 100755 stpaster create mode 100755 trim diff --git a/color.sh b/color.sh new file mode 100755 index 0000000..8a78840 --- /dev/null +++ b/color.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# +# This file echoes a bunch of color codes to the +# terminal to demonstrate what's available. Each +# line is the color code of one forground color, +# out of 17 (default + 16 escapes), followed by a +# test use of that color on all nine background +# colors (default + 8 escapes). +# + +T='gYw' # The test text + +echo -e "\n 40m 41m 42m 43m\ + 44m 45m 46m 47m"; + +for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \ + '1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \ + ' 36m' '1;36m' ' 37m' '1;37m'; + do FG=${FGs// /} + echo -en " $FGs \033[$FG $T " + for BG in 40m 41m 42m 43m 44m 45m 46m 47m; + do echo -en "$EINS \033[$FG\033[$BG $T \033[0m"; + done + echo; +done +echo + diff --git a/color.simple.sh b/color.simple.sh new file mode 100755 index 0000000..7cd9207 --- /dev/null +++ b/color.simple.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# Print four lines showing blocks of colors: 0-7 | 0-7bold | 8-15 | 8-15bold +perl -CADS -lwe ' +my $block = shift || (chr(0x2588) x 3); +for (["", 0], ["1;", 0], ["", 8], ["1;", 8]) { + my ($bold, $offset) = @$_; + my @range = map $offset + $_, 0..7; + printf "%s %-6s ", $bold ? "bold" : "norm", "$range[0]-$range[-1]"; + print map("\e[${bold}38;5;${_}m$block", @range), "\e[0m" +} +' "$@" diff --git a/justify b/justify new file mode 100755 index 0000000..4239ab3 --- /dev/null +++ b/justify @@ -0,0 +1,22 @@ +#!/bin/sh + +aligncols="72" +if [ $# -gt 0 ]; +then + aligncols="$1" +fi + +TMPFILE="/tmp/justify.$USER.$$" + +cat > $TMPFILE + +length=`wc -l $TMPFILE` + +echo ".ll $aligncols +.pl $length +.nh +.fi +`cat $TMPFILE`" | sed 's,\\,\\\\,g' | groff -kT utf8 | cat -s + +rm $TMPFILE + diff --git a/removenl b/removenl new file mode 100755 index 0000000..910501c --- /dev/null +++ b/removenl @@ -0,0 +1,22 @@ +#!/usr/bin/tcc -run +/* vim: set syn=c: */ + +#include +#include + +/* + * 0x0a - linefeed + * 0x0d - carriage return + **/ + +int +main() { + char buffer; + size_t nread; + + while ((nread = fread(&buffer, 1, sizeof buffer, stdin)) > 0) + if (buffer != 0x0a && buffer != 0x0d) + fwrite(&buffer, 1, nread, stdout); + + return 0; +} diff --git a/spacesbreak b/spacesbreak new file mode 100755 index 0000000..2bc513c --- /dev/null +++ b/spacesbreak @@ -0,0 +1,41 @@ +#!/usr/bin/tcc -run +/* vim: set syn=c: */ + +#include +#include + +#define WORD_BREAK " ()<>[]\"" + +static unsigned int is_word_break(char c); + +int +main() { + char buffer, prevbuffer; + size_t nread; + + while ((nread = fread(&buffer, 1, sizeof buffer, stdin)) > 0) { + if (is_word_break(buffer)) + buffer = '\n'; + + if (buffer != '\n' || prevbuffer != '\n') + fwrite(&buffer, 1, nread, stdout); + + prevbuffer = buffer; + } + + return 0; +} + +unsigned int +is_word_break(char c) { + static char *word_break = WORD_BREAK; + char *s = word_break; + + while (*s) { + if (*s == c) + return 1; + s++; + } + + return 0; +} diff --git a/stpaster b/stpaster new file mode 100755 index 0000000..b616661 --- /dev/null +++ b/stpaster @@ -0,0 +1,95 @@ +#!/usr/bin/tcc -run +/* vim: set syn=c: */ + +/* + * stpaster intends to pass st(1) terminal content to dmenu(1) for rapid + * selection and finaly in xclip(1) for pasting easily. + * + * Remove empty lines from stdin and output full‐lines, WORDS splited by + * space/tab, words splited by work breakers. + * + * see st & externalpipe patch. + * http://st.suckless.org/ + * http://st.suckless.org/patches/externalpipe + * + * is_word_break() comes from : + * http://st.suckless.org/patches/configwordbreak + * + * Add to config.h shortcuts : + * { MODKEY, 'u', externalpipe, { .s = "stpaster | sort | uniq | dmenu | removenl | xclip -in -selection primary" } }, + * + * memo : + * 0x09 - horizontal tab + * 0x0a - linefeed + * 0x0b - vertical tab + * 0x0c - form feed + * 0x0d - carriage return + * 0x20 - space + **/ + +#include +#include + +#define WORD_BREAK " ()<>[]\",/:'." + +static unsigned int is_word_break(char c); + +int +main() { + char *line = NULL; + size_t len = 0; + ssize_t nread = 0; + unsigned int i = 0; + + unsigned int wasblank = 1; + unsigned int emptyline = 1; + + while ((nread = getline(&line, &len, stdin)) != -1) { + /* remove empty lines */ + emptyline = 1; + for (i = 0; i < nread; ++i) + if(line[i] != 0x20 && line[i] != 0x09 && line[i] != 0x0a) + emptyline = 0; + if (emptyline) + continue; + + /* print each lines as is*/ + printf("%s", line); + + /* split each lines on space/tab */ + for (i = 0; i < nread; ++i) + if (line[i] != 0x20 && line[i] != 0x09) { + wasblank = 0; + printf("%c", line[i]); + } else if (!wasblank) { + wasblank = 1; + printf("\n"); + } + + wasblank = 1; + /* split each lines on word breaker */ + for (i = 0; i < nread; ++i) + if (!is_word_break(line[i])) { + wasblank = 0; + printf("%c", line[i]); + } else if (!wasblank) { + wasblank = 1; + printf("\n"); + } + } + + return 0; +} + +unsigned int +is_word_break(char c) { + static char *word_break = WORD_BREAK; + char *s = word_break; + while (*s) { + if (*s == c) + return 1; + s++; + } + + return 0; +} diff --git a/trim b/trim new file mode 100755 index 0000000..a07c90f --- /dev/null +++ b/trim @@ -0,0 +1,67 @@ +#!/usr/bin/tcc -run +/* vim: set syn=c: */ + +#include +#include + +/* + * 0x09 - horizontal tab + * 0x0a - linefeed + * 0x0b - vertical tab + * 0x0c - form feed + * 0x0d - carriage return + * 0x20 - space + **/ + +int +main() { + char *linein = NULL, *lineout = NULL; + size_t len = 0, lastblankchar = 0; + ssize_t nread; + + unsigned int trimmingleft = 1; + + unsigned int i, j; + + while ((nread = getline(&linein, &len, stdin)) > 0) { + trimmingleft = 1; + lastblankchar = j = 0; + + lineout = malloc(nread+1); + + for (i = 0; i < nread; ++i) { + if (trimmingleft) { + if(linein[i] != 0x09 && + linein[i] != 0x0a && + linein[i] != 0x0b && + linein[i] != 0x0c && + linein[i] != 0x0d && + linein[i] != 0x20) { + trimmingleft = 0; + } + } + if (!trimmingleft){ + if(linein[i] != 0x09 && + linein[i] != 0x0a && + linein[i] != 0x0b && + linein[i] != 0x0c && + linein[i] != 0x0d && + linein[i] != 0x20) { + lastblankchar = j + 1; + } + } + if (!trimmingleft) { + lineout[j++] = linein[i]; + } + } + + lineout[lastblankchar] = '\n'; + lineout[lastblankchar+1] = '\0'; + + printf("%s", lineout); + + free(lineout); + } + + return 0; +}