Skip to content

Instantly share code, notes, and snippets.

@timyates
Created January 27, 2014 12:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timyates/8648011 to your computer and use it in GitHub Desktop.
Save timyates/8648011 to your computer and use it in GitHub Desktop.
Spinner whilst parsing files in Groovy on the command line
@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