Created
January 27, 2014 12:51
-
-
Save timyates/8648011 to your computer and use it in GitHub Desktop.
Spinner whilst parsing files in Groovy on the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab( 'com.bloidonia:groovy-common-extensions:0.5.5' ) | |
@Grab('com.xlson.groovycsv:groovycsv:1.0') | |
import static com.xlson.groovycsv.CsvParser.parseCsv | |
import groovy.transform.* | |
// A bufferedReader with some output | |
@InheritConstructors | |
class SpinnerReader extends BufferedReader { | |
private String output = '/-\\|' | |
private int offset = 0 | |
private void update() { | |
print "\r${output[ offset ]}" | |
System.out.flush() | |
offset += 1 | |
offset %= output.length() | |
} | |
int read() { update() ; super.read() } | |
int read(char[] cbuf, int off, int len) { update() ; super.read( cbuf, off, len ) } | |
String readLine() { update() ; super.readLine() } | |
void close() { | |
print "\r" | |
super.close() | |
} | |
} | |
// Add a method to the metaClass of File | |
File.metaClass.withSpinnerReader = { Closure cl -> | |
// withClosable is from common-extensions | |
new SpinnerReader( delegate.newReader() ).withClosable cl | |
} | |
// Then, to use it: | |
new File( 'massiveFile.csv' ).withSpinnerReader { r -> | |
parseCsv( r, separator:'\t' ).each { row -> | |
// do something | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment