Skip to content

Instantly share code, notes, and snippets.

View turboBasic's full-sized avatar
🔮
Focusing

andriy melnyk turboBasic

🔮
Focusing
View GitHub Profile
@turboBasic
turboBasic / wrapperDemo.groovy
Last active May 25, 2022 12:41
Groovy: wrap variable number of nested Closure calls #groovy
/**
* Class Wrapper allows to wrap and execute variable number of nested Closures.
* Immediately-wrapped closure is accessible inside the wrapper as variable `_`.
*/
final class Wrapper {
private final List stack = []
Wrapper leftShift(Closure c) {
c.resolveStrategy = Closure.DELEGATE_FIRST
stack << c
@turboBasic
turboBasic / getHostFromUrl.groovy
Last active January 2, 2022 16:07
getHostFromUrl() #groovy
String getHostFromUrl(String url) {
url
.replaceAll('^[a-z]+://', '')
.replaceAll('(/.*)?$', '')
.replaceAll('(:[0-9]+)?$', '')
}
def getHostFromUrlSpec() {
expect:
getHostFromUrl('https://example.com:9999/path.ext') == 'example.com'
@turboBasic
turboBasic / argumentsProcessing.groovy
Last active May 25, 2022 11:24
Method arguments processing in Groovy #groovy
String f(Object... varArgs) {
List args = varArgs == null ? [null] : varArgs
Map named = args
? (args[0] instanceof Map ? args[0] : [:])
: [:]
List positional = named ? args.drop(1) : args
"named: $named; positional: $positional; all: $args"
}
@turboBasic
turboBasic / test-firefox-places-queries.md
Last active November 9, 2021 10:24
List of test queries and bookmark items for Firefox places db #firefox

List of test queries and bookmarks for Firefox Places database

Bookmark items for testing queries

@turboBasic
turboBasic / generate.sh
Last active October 24, 2021 18:46
LS_COLORS file for file type coloring in ls, grep etc.
function __generate() {
typeset -A themes=(
[molokai]="no=0:di=0;38;2;102;217;239:ln=0;38;2;249;38;114:ow=0:so=0;38;2;0;0;0;48;2;249;38;114:fi=0:tw=0:ex=1;38;2;249;38;114:mi=0;38;2;0;0;0;48;2;255;74;68:*~=0;38;2;122;112;112:pi=0;38;2;0;0;0;48;2;102;217;239:or=0;38;2;0;0;0;48;2;255;74;68:cd=0;38;2;249;38;114;48;2;51;51;51:bd=0;38;2;102;217;239;48;2;51;51;51:st=0:*.r=0;38;2;0;255;135:*.t=0;38;2;0;255;135:*.o=0;38;2;122;112;112:*.a=1;38;2;249;38;114:*.d=0;38;2;0;255;135:*.h=0;38;2;0;255;135:*.m=0;38;2;0;255;135:*.p=0;38;2;0;255;135:*.z=4;38;2;249;38;114:*.c=0;38;2;0;255;135:*.td=0;38;2;0;255;135:*.hs=0;38;2;0;255;135:*.mn=0;38;2;0;255;135:*.jl=0;38;2;0;255;135:*.ui=0;38;2;166;226;46:*.pl=0;38;2;0;255;135:*.ml=0;38;2;0;255;135:*.py=0;38;2;0;255;135:*.kt=0;38;2;0;255;135:*.el=0;38;2;0;255;135:*.ps=0;38;2;230;219;116:*.cc=0;38;2;0;255;135:*.gv=0;38;2;0;255;135:*.ts=0;38;2;0;255;135:*.vb=0;38;2;0;255;135:*.rb=0;38;2;0;255;135:*.di=0;38;2;0;255;135:*.rs=0;38;2;0;255;135:*.pp=0;38;2;0;255;135:*.xz=4;38;2;2
@turboBasic
turboBasic / changeCallerOfClosure.groovy
Last active May 25, 2022 12:00
Dynamically change caller of Groovy closure #groovy
void withOptionalDocker(String dockerImageName, Closure body) {
(dockerImageName
? { c -> withDockerContainer(image: dockerImageName) {c()} }
: { c -> c() }
)(body)
}
withOptionalDocker(shouldUseDocke() ? 'my-image' : null) {
sh 'doSomething'
}
@turboBasic
turboBasic / rename-program-in-ubuntu.md
Last active September 15, 2021 18:37
Rename program provided by Ubuntu/Debian package manager #ubuntu

Rename program in Ubuntu

Example 1. Rename ack-grep utility to ack

sudo dpkg-divert --local --divert /usr/bin/ack --rename --add /usr/bin/ack-grep
@turboBasic
turboBasic / class-as-container-for-constants.md
Last active September 14, 2021 15:33
How to manage constants in Java/Groovy #java #groovy

Class as constants container

final class Constants {
    final class File {
        public static final int MIN_ROWS = 1
        public static final int MAX_ROWS = 1000
        private File() {}
 }
@turboBasic
turboBasic / Inspect.groovy
Last active August 16, 2021 22:21
class Inspect for inspecting big objects in Groovy #groovy
import groovy.json.JsonGenerator
import groovy.json.JsonOutput
/**
* Inspects big objects
* Usage:
* println Inspect.unwrapAsJsonWithProperties(sourceSets, 3)
* println Inspect.unwrapAsJsonWithProperties(sourceSets.main.allSource, 2, 150)
* println Inspect.unwrapAsJson(sourceSets.main, 2, 120)
*/
@turboBasic
turboBasic / build.gradle
Last active August 15, 2021 10:59
Gradle task `printSourceSets` #gradle
plugins {
id 'groovy'
}
sourceSets {
// ...
}