Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
22 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
|
||
chart "github.com/wcharczuk/go-chart" | ||
"github.com/wcharczuk/go-chart/drawing" | ||
) | ||
|
||
// custom version of chart.Legend | ||
func legend(c *chart.Chart, userDefaults ...chart.Style) chart.Renderable { | ||
return func(r chart.Renderer, cb chart.Box, chartDefaults chart.Style) { | ||
legendDefaults := chart.Style{ | ||
FillColor: drawing.ColorWhite, | ||
FontColor: chart.DefaultTextColor, | ||
FontSize: 8.0, | ||
StrokeColor: chart.DefaultAxisColor, | ||
StrokeWidth: chart.DefaultAxisLineWidth, | ||
} | ||
|
||
legendPadding := chart.Box{ | ||
Top: 5, | ||
Left: 5, | ||
Right: 5, | ||
Bottom: 5, | ||
} | ||
lineTextGap := 5 | ||
lineLengthMinimum := 25 | ||
|
||
var legendStyle chart.Style | ||
if len(userDefaults) > 0 { | ||
legendStyle = userDefaults[0].InheritFrom(chartDefaults.InheritFrom(legendDefaults)) | ||
} else { | ||
legendStyle = chartDefaults.InheritFrom(legendDefaults) | ||
} | ||
|
||
var labels []string | ||
var lines []chart.Style | ||
for _, s := range c.Series { | ||
if s.GetStyle().IsZero() || s.GetStyle().Show { | ||
if _, isAnnotationSeries := s.(chart.AnnotationSeries); !isAnnotationSeries { | ||
labels = append(labels, s.GetName()) | ||
lines = append(lines, s.GetStyle()) | ||
} | ||
} | ||
} | ||
|
||
legend := chart.Box{ | ||
Top: cb.Top, | ||
Left: cb.Left, | ||
// bottom and right will be sized by the legend content | ||
} | ||
|
||
legendContent := chart.Box{ | ||
Top: legend.Top + legendPadding.Top, | ||
Left: legend.Left + legendPadding.Left, | ||
Right: legend.Left + legendPadding.Left, | ||
Bottom: legend.Top + legendPadding.Top, | ||
} | ||
|
||
legendStyle.GetTextOptions().WriteToRenderer(r) | ||
|
||
// measure | ||
labelCount := 0 | ||
for x := 0; x < len(labels); x++ { | ||
if len(labels[x]) > 0 { | ||
tb := r.MeasureText(labels[x]) | ||
legendContent.Bottom += tb.Height() + 5 | ||
right := legendContent.Left + tb.Width() + lineTextGap + lineLengthMinimum | ||
legendContent.Right = int(math.Max(float64(legendContent.Right), float64(right))) | ||
labelCount++ | ||
} | ||
} | ||
|
||
legend = legend.Grow(legendContent) | ||
legend.Right = legendContent.Right + legendPadding.Right | ||
legend.Bottom = legendContent.Bottom + legendPadding.Bottom | ||
|
||
chart.Draw.Box(r, legend, legendStyle) | ||
|
||
legendStyle.GetTextOptions().WriteToRenderer(r) | ||
|
||
ycursor := legendContent.Top | ||
tx := legendContent.Left | ||
legendCount := 0 | ||
var label string | ||
for x := 0; x < len(labels); x++ { | ||
label = labels[x] | ||
if len(label) > 0 { | ||
if legendCount > 0 { | ||
ycursor += 5 | ||
} | ||
|
||
tb := r.MeasureText(label) | ||
|
||
ty := ycursor + tb.Height() | ||
r.Text(label, tx, ty) | ||
|
||
th2 := tb.Height() >> 1 | ||
|
||
lx := tx + tb.Width() + lineTextGap | ||
ly := ty - th2 | ||
lx2 := legendContent.Right - legendPadding.Right | ||
|
||
r.SetStrokeColor(lines[x].GetStrokeColor()) | ||
r.SetStrokeWidth(lines[x].GetStrokeWidth()) | ||
r.SetStrokeDashArray(lines[x].GetStrokeDashArray()) | ||
|
||
r.MoveTo(lx, ly) | ||
r.LineTo(lx2, ly) | ||
r.Stroke() | ||
|
||
ycursor += tb.Height() | ||
legendCount++ | ||
} | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package window | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Gets the size of the current front | ||
var script = ` | ||
tell application "iTerm" | ||
--set w to front window | ||
--set w to window id 1887 | ||
set w to %s | ||
set b to bounds of w | ||
set width to ((item 3 of b) - (item 1 of b)) as text | ||
set height to ((item 4 of b) - (item 2 of b)) as text | ||
do shell script "echo " & (id of w) & " " & width & " " & height | ||
end tell` | ||
|
||
var winID string | ||
|
||
// Size returns current window height and width. | ||
func Size() (int, int, error) { | ||
win := "front window" | ||
if winID != "" { | ||
win = "window id " + winID | ||
} | ||
cmd := exec.Command("osascript", "-e", fmt.Sprintf(script, win)) | ||
cmd.Stdin = strings.NewReader("some input") | ||
var out bytes.Buffer | ||
cmd.Stdout = &out | ||
if err := cmd.Run(); err != nil { | ||
return 0, 0, fmt.Errorf("Cannot execute osascript: %v", err) | ||
} | ||
rv := strings.Split(strings.TrimSpace(out.String()), " ") | ||
if len(rv) != 3 { | ||
return 0, 0, fmt.Errorf("invalid output: %s", out.String()) | ||
} | ||
winID = rv[0] | ||
width, _ := strconv.Atoi(rv[1]) | ||
height, _ := strconv.Atoi(rv[2]) | ||
return width, height, nil | ||
} |