Finished pretty_format script
This commit is contained in:
parent
04c4a2b6ac
commit
59a81a4f77
@ -1,95 +0,0 @@
|
||||
#!/usr/bin/env groovy
|
||||
|
||||
import java.io.BufferedWriter
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileWriter
|
||||
import java.io.OutputStreamWriter
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.text.SimpleDateFormat
|
||||
|
||||
//import jdbernard.timestamper.Timeline
|
||||
|
||||
SimpleDateFormat paramDateFormat = new SimpleDateFormat("yyyy-MM-dd")
|
||||
|
||||
def cl = new CliBuilder(usage: 'pretty_format.groovy [-i input_file] [-o output_file] [OPTIONS]')
|
||||
cl.h(argName: 'help', 'Show usage information.')
|
||||
cl.i(argName: 'input_file', longOpt: 'input-file', args: 1, 'Input timeline file (if not present, read from STDIN)')
|
||||
cl.o(argName: 'output_file', longOpt: 'output-file', args: 1, 'File to write formatted output to (if not present, output is written to STOUT).')
|
||||
cl.s(argName: 'start_date', longOpt: 'start-date', args: 1, 'Exclude all entries before this date (YYYY-MM-DD).')
|
||||
cl.e(argName: 'end_date', longOpt: 'end-date', args: 1, 'Exclude all entries after this date (YYYY-MM-DD).')
|
||||
cl.p(argName: 'entry_period', longOpt: 'period', args: 1, 'Shorthand notation for the period of entries to include in the output file. The format '
|
||||
+ 'takes the following format: MUN where M = number of units between current time and start time, N = number of units between start time and '
|
||||
+ 'end time, and U = time unit. Valid values for U are: D - day, W - week, M - month, and Y - year. For example: 0W1 means this week '
|
||||
+ 'and the next. 2D5 means a five day period, starting two days from today. -1Y1 means last year, this year, and the next year.')
|
||||
|
||||
def opt = cl.parse(args)
|
||||
|
||||
if (!opt) System.exit(1);
|
||||
|
||||
is = System.in;
|
||||
os = new BufferedWriter(new OutputStreamWriter(System.out));
|
||||
start = false;
|
||||
end = false;
|
||||
|
||||
if (opt.i) is = new FileInputStream(opt.i);
|
||||
if (opt.o) os = new BufferedWriter(new FileWriter(opt.o));
|
||||
|
||||
if (opt.s) start = paramDateFormat.parse(opt.s);
|
||||
if (opt.e) end = paramDateFormat.parse(opt.e);
|
||||
|
||||
if (opt.p) {
|
||||
int offset
|
||||
def unit
|
||||
def lower_unit
|
||||
int length
|
||||
|
||||
(opt.p =~ /([\-n]?)(\d+)([dwmyDWMY])(\d+)/).each { all, nx, x, y, z ->
|
||||
offset = x.toInteger(); unit = y; length = z.toInteger() - 1;
|
||||
if (nx) offset = -offset;
|
||||
}
|
||||
|
||||
start = Calendar.getInstance()
|
||||
start.set(Calendar.HOUR_OF_DAY, start.getMinimum(Calendar.HOUR_OF_DAY));
|
||||
start.set(Calendar.MINUTE, start.getMinimum(Calendar.MINUTE));
|
||||
start.set(Calendar.SECOND, start.getMinimum(Calendar.SECOND));
|
||||
|
||||
end = Calendar.getInstance()
|
||||
end.set(Calendar.HOUR_OF_DAY, end.getMaximum(Calendar.HOUR_OF_DAY));
|
||||
end.set(Calendar.MINUTE, end.getMaximum(Calendar.MINUTE));
|
||||
end.set(Calendar.SECOND, end.getMaximum(Calendar.SECOND));
|
||||
|
||||
switch (unit) {
|
||||
case 'd': case 'D': default:
|
||||
unit = Calendar.DAY_OF_YEAR
|
||||
lower_unit = Calendar.HOUR_OF_DAY
|
||||
break
|
||||
case 'w': case 'W':
|
||||
unit = Calendar.WEEK_OF_YEAR
|
||||
lower_unit = Calendar.DAY_OF_WEEK
|
||||
break
|
||||
case 'm': case 'M':
|
||||
unit = Calendar.MONTH
|
||||
lower_unit = Calendar.DAY_OF_MONTH
|
||||
break
|
||||
case 'y': case 'Y':
|
||||
unit = Calendar.YEAR
|
||||
lower_unit = Calendar.DAY_OF_YEAR
|
||||
break
|
||||
}
|
||||
|
||||
start.add(unit, offset)
|
||||
start.set(lower_unit, start.getActualMinimum(lower_unit))
|
||||
|
||||
end.setTime(start.getTime())
|
||||
end.add(unit, length)
|
||||
end.set(lower_unit, end.getActualMaximum(lower_unit))
|
||||
|
||||
start = start.getTime()
|
||||
end = end.getTime()
|
||||
}
|
||||
|
||||
//Timeline timeline = Timeline.readFromStream(is);
|
||||
|
||||
print "Start: ${paramDateFormat.format(start)}\n"
|
||||
print "End: ${paramDateFormat.format(end)}\n"
|
@ -67,6 +67,13 @@ public class Timeline implements Iterable<Timeline.TimelineMarker> {
|
||||
return this.timestamp.equals(((TimelineMarker)that).timestamp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 5;
|
||||
hash = 53 * hash + (this.timestamp != null ? this.timestamp.hashCode() : 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static enum ReadingState {
|
||||
|
224
src/pretty_format.groovy
Normal file
224
src/pretty_format.groovy
Normal file
@ -0,0 +1,224 @@
|
||||
#!groovy
|
||||
|
||||
package jdbernard.timestamper
|
||||
|
||||
import java.util.Date
|
||||
|
||||
EOL = System.getProperty("line.separator")
|
||||
|
||||
boolean sameDay(Date d1, Date d2) {
|
||||
Calendar c1 = Calendar.getInstance()
|
||||
Calendar c2 = Calendar.getInstance()
|
||||
|
||||
c1.setTime(d1)
|
||||
c2.setTime(d2)
|
||||
|
||||
return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) &&
|
||||
c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR))
|
||||
}
|
||||
|
||||
String wrapToWidth(String text, int width, String prefix, String suffix) {
|
||||
int lastSpaceIdx = 0;
|
||||
int curLineLength = 0;
|
||||
int lineStartIdx = 0;
|
||||
int i = 0;
|
||||
int actualWidth = width - prefix.length() - suffix.length()
|
||||
String wrapped = ""
|
||||
|
||||
text = text.replaceAll("[\n\r]", " ")
|
||||
|
||||
for (i = 0; i < text.length(); i++) {
|
||||
|
||||
curLineLength++
|
||||
if (curLineLength > actualWidth) {
|
||||
wrapped += prefix + text[lineStartIdx..<lastSpaceIdx] + suffix + EOL
|
||||
curLineLength = 0
|
||||
lineStartIdx = lastSpaceIdx + 1
|
||||
i = lastSpaceIdx
|
||||
}
|
||||
|
||||
if (text.charAt(i).isWhitespace())
|
||||
lastSpaceIdx = i
|
||||
|
||||
}
|
||||
|
||||
if (i - lineStartIdx > 1)
|
||||
wrapped += prefix + text[lineStartIdx..<text.length()] + EOL
|
||||
|
||||
return wrapped
|
||||
}
|
||||
|
||||
paramDateFormat = "yyyy-MM-dd"
|
||||
dateFormat = "EEE dd, MMM yyyy"
|
||||
timeFormat = "HH:mm"
|
||||
|
||||
def cl = new CliBuilder(usage: 'pretty_format.groovy [-i input_file] [-o output_file] [OPTIONS]')
|
||||
cl.h(argName: 'help', 'Show usage information.')
|
||||
cl.i(argName: 'input_file', longOpt: 'input-file', args: 1, 'Input timeline file (if not present, read from STDIN)')
|
||||
cl.o(argName: 'output_file', longOpt: 'output-file', args: 1, 'File to write formatted output to (if not present, output is written to STOUT).')
|
||||
cl.s(argName: 'start_date', longOpt: 'start-date', args: 1, 'Exclude all entries before this date (YYYY-MM-DD).')
|
||||
cl.e(argName: 'end_date', longOpt: 'end-date', args: 1, 'Exclude all entries after this date (YYYY-MM-DD).')
|
||||
cl.p(argName: 'entry_period', longOpt: 'period', args: 1, 'Shorthand notation for the period of entries to include in the output file. The format '
|
||||
+ 'takes the following format: MUN where M = number of units between current time and start time, N = number of units between start time and '
|
||||
+ 'end time, and U = time unit. Valid values for U are: D - day, W - week, M - month, and Y - year. For example: 0W1 means this week '
|
||||
+ 'and the next. 2D5 means a five day period, starting two days from today. -1Y1 means last year, this year, and the next year.')
|
||||
cl.v(argName: 'verbose', longOpt: 'verbose', 'Print detailed information.')
|
||||
cl.x(argName: 'exclude', longOpt: 'exclude', args:1, 'Regex of tasks to exclude.')
|
||||
|
||||
def opt = cl.parse(args)
|
||||
|
||||
if (!opt) System.exit(1);
|
||||
|
||||
boolean verbose = false;
|
||||
is = System.in;
|
||||
os = new PrintWriter(System.out);
|
||||
start = new Date(0);
|
||||
end = new Date(Long.MAX_VALUE);
|
||||
excluded = false
|
||||
toStdout = true
|
||||
|
||||
if (opt.v) verbose = true;
|
||||
|
||||
if (opt.x) {
|
||||
excluded = opt.x
|
||||
if (verbose) println "Excluding /${excluded}/";
|
||||
}
|
||||
|
||||
if (!excluded) {
|
||||
excludeFile = new File("pretty_format_excludes");
|
||||
if (excludeFile.exists()) {
|
||||
excluded = new StringBuilder("(")
|
||||
excludeFile.eachLine { line -> excluded.append(line + "|") }
|
||||
excluded[-1..-1] = ")"
|
||||
excluded = excluded.toString()
|
||||
if (verbose) println "Excluding /${excluded}/";
|
||||
}
|
||||
}
|
||||
|
||||
if (opt.h) cl.usage()
|
||||
|
||||
if (opt.i) {
|
||||
is = new FileInputStream(opt.i);
|
||||
if (verbose) println "Reading from file '${opt.i}'."
|
||||
} else {
|
||||
if (verbose) println "Reading from STDIN."
|
||||
}
|
||||
|
||||
if (opt.o) {
|
||||
os = new PrintWriter(new FileOutputStream(opt.o));
|
||||
if (verbose) println "Writing output to file '${opt.o}'."
|
||||
toStdout = false
|
||||
} else if (verbose) {
|
||||
println "Writing output to STOUT."
|
||||
}
|
||||
|
||||
if (opt.s) start = paramDateFormat.parse(opt.s);
|
||||
if (opt.e) end = paramDateFormat.parse(opt.e);
|
||||
|
||||
if (opt.p) {
|
||||
int offset
|
||||
def unit
|
||||
def lower_unit
|
||||
int length
|
||||
|
||||
(opt.p =~ /([\-n]?)(\d+)([dwmyDWMY])(\d+)/).each { all, nx, x, y, z ->
|
||||
offset = x.toInteger(); unit = y; length = z.toInteger() - 1;
|
||||
if (nx) offset = -offset;
|
||||
}
|
||||
|
||||
start = Calendar.getInstance()
|
||||
start.set(Calendar.HOUR_OF_DAY, start.getMinimum(Calendar.HOUR_OF_DAY));
|
||||
start.set(Calendar.MINUTE, start.getMinimum(Calendar.MINUTE));
|
||||
start.set(Calendar.SECOND, start.getMinimum(Calendar.SECOND));
|
||||
|
||||
end = Calendar.getInstance()
|
||||
end.set(Calendar.HOUR_OF_DAY, end.getMaximum(Calendar.HOUR_OF_DAY));
|
||||
end.set(Calendar.MINUTE, end.getMaximum(Calendar.MINUTE));
|
||||
end.set(Calendar.SECOND, end.getMaximum(Calendar.SECOND));
|
||||
|
||||
switch (unit) {
|
||||
case 'd': case 'D': default:
|
||||
unit = Calendar.DAY_OF_YEAR
|
||||
lower_unit = Calendar.HOUR_OF_DAY
|
||||
break
|
||||
case 'w': case 'W':
|
||||
unit = Calendar.WEEK_OF_YEAR
|
||||
lower_unit = Calendar.DAY_OF_WEEK
|
||||
break
|
||||
case 'm': case 'M':
|
||||
unit = Calendar.MONTH
|
||||
lower_unit = Calendar.DAY_OF_MONTH
|
||||
break
|
||||
case 'y': case 'Y':
|
||||
unit = Calendar.YEAR
|
||||
lower_unit = Calendar.DAY_OF_YEAR
|
||||
break
|
||||
}
|
||||
|
||||
start.add(unit, offset)
|
||||
start.set(lower_unit, start.getActualMinimum(lower_unit))
|
||||
|
||||
end.setTime(start.getTime())
|
||||
end.add(unit, length)
|
||||
end.set(lower_unit, end.getActualMaximum(lower_unit))
|
||||
|
||||
start = start.getTime()
|
||||
end = end.getTime()
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
os.println "Period: ${start.format(paramDateFormat)} to ${end.format(paramDateFormat)}, inclusive."
|
||||
|
||||
Timeline timeline = Timeline.readFromStream(is);
|
||||
|
||||
def curDate = new Date(0)
|
||||
def prevMarker = false
|
||||
|
||||
for (marker in timeline) {
|
||||
if (marker.timestamp < start) continue
|
||||
|
||||
if (prevMarker) {
|
||||
long duration = marker.timestamp.time - prevMarker.timestamp.time
|
||||
long seconds = duration / 1000
|
||||
long minutes = seconds / 60
|
||||
long hours = minutes / 60
|
||||
long days = hours / 24
|
||||
|
||||
seconds %= 60
|
||||
minutes %= 60
|
||||
hours %= 24
|
||||
|
||||
if (!excluded || !(prevMarker.mark =~ excluded)) {
|
||||
os.println " - From ${prevMarker.timestamp.format(timeFormat)} to ${marker.timestamp.format(timeFormat)}"
|
||||
os.println (" - Duration: "
|
||||
+ (days ? "${days} days, " : "")
|
||||
+ (hours ? "${hours} hours, " : "")
|
||||
+ (minutes ? "${minutes} minutes, " : "")
|
||||
+ "${seconds} seconds.")
|
||||
os.println ""
|
||||
}
|
||||
prevMarker = false
|
||||
}
|
||||
|
||||
if (marker.timestamp > end) break
|
||||
|
||||
if (!sameDay(curDate, marker.timestamp) && (!excluded || !(marker.mark =~ excluded))) {
|
||||
os.println marker.timestamp.format(dateFormat)
|
||||
os.println ""
|
||||
curDate = marker.timestamp
|
||||
}
|
||||
|
||||
if (!excluded || !(marker.mark =~ excluded))
|
||||
os.print wrapToWidth("${marker.mark} - ${marker.notes}", 78, " ", "")
|
||||
prevMarker = marker
|
||||
|
||||
}
|
||||
|
||||
if (prevMarker) {
|
||||
os.println " - Started at ${prevMarker.timestamp.format(timeFormat)}"
|
||||
os.println " - Duration: on-going"
|
||||
os.println ""
|
||||
}
|
||||
|
||||
os.flush()
|
||||
if (!toStdout) os.close()
|
Loading…
x
Reference in New Issue
Block a user