Skip to content

Instantly share code, notes, and snippets.

@crevier
crevier / swingTimer.groovy
Created July 3, 2018 14:12
Simple swing timer in groovy
import javax.swing.JDialog
import javax.swing.JPanel
import java.awt.*
import java.awt.image.BufferedImage
import static java.lang.System.exit
import static javax.swing.JOptionPane.*
class TimerPanel extends JPanel {
@crevier
crevier / checkListViewer.groovy
Created June 21, 2018 17:08
CheckList viewer in groovy
import javax.swing.*
import java.awt.*
import java.nio.file.Path
import java.nio.file.Paths
import java.util.List
import static javax.swing.JOptionPane.*
/*
Display a checklist selected from a folder and write the user selection in a log file
@crevier
crevier / cliQuestion.groovy
Created June 15, 2018 11:24
simple stdin option selection in groovy
void displayOptions(String name, List<String> options) {
print("Select $name ::")
options.eachWithIndex { String entry, int i ->
print(" $entry [${i + 1}]${i + 1 == options.size() ? ": " : ","}")
}
}
int readUserInput(List<String> options) {
displayOptions(options)
def input = ++new Scanner(System.in)
@crevier
crevier / consoleProgressBar.groovy
Created June 14, 2018 14:05
quick and dirty progress bar in groovy for cli
def progressColor(int percent) {
def red = "${(char) 27}[31;49m"
def yellow = "${(char) 27}[33;49m"
def green = "${(char) 27}[32;49m"
if (percent <= 25)
return red
if (percent <= 75)
return yellow
return green
}
@crevier
crevier / colorInConsole.groovy
Created June 13, 2018 14:21
How to print color in console in groovy
def fg = 30
def bg = 46
def style = "${(char)27}[$fg;$bg"+"m"
println(style+"HELLO")
/*
+~~~~~~+~~~~~~+~~~~~~~~~~~+
| fg | bg | color |
+~~~~~~+~~~~~~+~~~~~~~~~~~+
| 30 | 40 | black |
| 31 | 41 | red |
@crevier
crevier / Bowling.java
Last active March 15, 2018 15:51
Bowling kata - constrains : fake outside in and golf.
import org.junit.Test;
import static java.util.Arrays.stream;
import static java.util.stream.IntStream.range;
import static org.junit.Assert.assertEquals;
public class Bowling {
private int score(int[][]f) {
return range(0,9).boxed().mapToInt(i->stream(f[i]).sum()+((f[i][0]+f[i][1]==10)?f[i+1][0]:0)+((f[i][0]==10)?(f[i+1][0]!=10?f[i+1][1]:(i!=8?f[i+2][0]:f[i+1][2])):0)).sum()+stream(f[9]).sum();
}
@crevier
crevier / InMemoryAppender.java
Created March 5, 2018 16:34
Log4j2 InMemoryAppender for tests
package log.appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import java.util.Arrays;
public class StairCaseSolution {
public static String staircase(int n) {
if (n <= 0)
return "";
StringBuilder resultBuilder = new StringBuilder();
for (int spaceNumber = n - 1; spaceNumber >= 0; spaceNumber--) {
resultBuilder.append(createLine(n, spaceNumber)).append('\n');
}
@crevier
crevier / codeGolf.groovy
Last active December 18, 2017 11:38
script template used for code golf on codingame
/*
This is a script template used for code golf on codingame
use scriptSize to compute the size of the code between two tags
use runCode to execute the golfCode with custom STDIN/OUT for your unit tests
*/
/**
* Read this file lines and return the number of char between two line marked with the given tags
* @param startTag the tag of the starting line ex : //START
* @param endTag the tag of the end line ex : //END
@crevier
crevier / BowlingGame.groovy
Created December 15, 2017 22:25
Bowling Kata : This snipet is the result of a pair programing session with thomas in Starbuck (avenue de l'opera Paris)
class Bowling {
static int score(def rolls) {
def score = scoreWithoutBonus(rolls)
def frames = 0..9
frames.each {
def firstRollInFrame = it * 2
if (isStrike(rolls, firstRollInFrame)) {
score += strikeBonus(rolls, firstRollInFrame)
} else if (isSpare(rolls, firstRollInFrame)) {
score += spareBonus(rolls, firstRollInFrame)