repo
stringlengths
5
67
sha
stringlengths
40
40
path
stringlengths
4
234
url
stringlengths
85
339
language
stringclasses
6 values
split
stringclasses
3 values
doc
stringlengths
3
51.2k
sign
stringlengths
5
8.01k
problem
stringlengths
13
51.2k
output
stringlengths
0
3.87M
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
example/logging/main.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/example/logging/main.go#L62-L107
go
train
// Service setup. // Define service config. // Create the service. // Setup the logger. // Handle service controls (optional). // Run the service.
func main()
// Service setup. // Define service config. // Create the service. // Setup the logger. // Handle service controls (optional). // Run the service. func main()
{ svcFlag := flag.String("service", "", "Control the system service.") flag.Parse() svcConfig := &service.Config{ Name: "GoServiceExampleLogging", DisplayName: "Go Service Example for Logging", Description: "This is an example Go service that outputs log messages.", Dependencies: []string{ "Requires=network.target", "After=network-online.target syslog.target"}, } prg := &program{} s, err := service.New(prg, svcConfig) if err != nil { log.Fatal(err) } errs := make(chan error, 5) logger, err = s.Logger(errs) if err != nil { log.Fatal(err) } go func() { for { err := <-errs if err != nil { log.Print(err) } } }() if len(*svcFlag) != 0 { err := service.Control(s, *svcFlag) if err != nil { log.Printf("Valid actions: %q\n", service.ControlAction) log.Fatal(err) } return } err = s.Run() if err != nil { logger.Error(err) } }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L72-L74
go
train
// Error logs an error message.
func (l WindowsLogger) Error(v ...interface{}) error
// Error logs an error message. func (l WindowsLogger) Error(v ...interface{}) error
{ return l.send(l.ev.Error(3, fmt.Sprint(v...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L87-L89
go
train
// Errorf logs an error message.
func (l WindowsLogger) Errorf(format string, a ...interface{}) error
// Errorf logs an error message. func (l WindowsLogger) Errorf(format string, a ...interface{}) error
{ return l.send(l.ev.Error(3, fmt.Sprintf(format, a...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L92-L94
go
train
// Warningf logs an warning message.
func (l WindowsLogger) Warningf(format string, a ...interface{}) error
// Warningf logs an warning message. func (l WindowsLogger) Warningf(format string, a ...interface{}) error
{ return l.send(l.ev.Warning(2, fmt.Sprintf(format, a...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L97-L99
go
train
// Infof logs an info message.
func (l WindowsLogger) Infof(format string, a ...interface{}) error
// Infof logs an info message. func (l WindowsLogger) Infof(format string, a ...interface{}) error
{ return l.send(l.ev.Info(1, fmt.Sprintf(format, a...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L102-L104
go
train
// NError logs an error message and an event ID.
func (l WindowsLogger) NError(eventID uint32, v ...interface{}) error
// NError logs an error message and an event ID. func (l WindowsLogger) NError(eventID uint32, v ...interface{}) error
{ return l.send(l.ev.Error(eventID, fmt.Sprint(v...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L107-L109
go
train
// NWarning logs an warning message and an event ID.
func (l WindowsLogger) NWarning(eventID uint32, v ...interface{}) error
// NWarning logs an warning message and an event ID. func (l WindowsLogger) NWarning(eventID uint32, v ...interface{}) error
{ return l.send(l.ev.Warning(eventID, fmt.Sprint(v...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L112-L114
go
train
// NInfo logs an info message and an event ID.
func (l WindowsLogger) NInfo(eventID uint32, v ...interface{}) error
// NInfo logs an info message and an event ID. func (l WindowsLogger) NInfo(eventID uint32, v ...interface{}) error
{ return l.send(l.ev.Info(eventID, fmt.Sprint(v...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L117-L119
go
train
// NErrorf logs an error message and an event ID.
func (l WindowsLogger) NErrorf(eventID uint32, format string, a ...interface{}) error
// NErrorf logs an error message and an event ID. func (l WindowsLogger) NErrorf(eventID uint32, format string, a ...interface{}) error
{ return l.send(l.ev.Error(eventID, fmt.Sprintf(format, a...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L122-L124
go
train
// NWarningf logs an warning message and an event ID.
func (l WindowsLogger) NWarningf(eventID uint32, format string, a ...interface{}) error
// NWarningf logs an warning message and an event ID. func (l WindowsLogger) NWarningf(eventID uint32, format string, a ...interface{}) error
{ return l.send(l.ev.Warning(eventID, fmt.Sprintf(format, a...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L127-L129
go
train
// NInfof logs an info message and an event ID.
func (l WindowsLogger) NInfof(eventID uint32, format string, a ...interface{}) error
// NInfof logs an info message and an event ID. func (l WindowsLogger) NInfof(eventID uint32, format string, a ...interface{}) error
{ return l.send(l.ev.Info(eventID, fmt.Sprintf(format, a...))) }
kardianos/service
0e5bec1b9eec14f9070a6f49ad7e0242f1545d66
service_windows.go
https://github.com/kardianos/service/blob/0e5bec1b9eec14f9070a6f49ad7e0242f1545d66/service_windows.go#L406-L422
go
train
// getStopTimeout fetches the time before windows will kill the service.
func getStopTimeout() time.Duration
// getStopTimeout fetches the time before windows will kill the service. func getStopTimeout() time.Duration
{ // For default and paths see https://support.microsoft.com/en-us/kb/146092 defaultTimeout := time.Millisecond * 20000 key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control`, registry.READ) if err != nil { return defaultTimeout } sv, _, err := key.GetStringValue("WaitToKillServiceTimeout") if err != nil { return defaultTimeout } v, err := strconv.Atoi(sv) if err != nil { return defaultTimeout } return time.Millisecond * time.Duration(v) }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L22-L24
go
train
// Edit calls f(v, key, ch, mod)
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier)
// Edit calls f(v, key, ch, mod) func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier)
{ f(v, key, ch, mod) }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L30-L53
go
train
// simpleEditor is used as the default gocui editor.
func simpleEditor(v *View, key Key, ch rune, mod Modifier)
// simpleEditor is used as the default gocui editor. func simpleEditor(v *View, key Key, ch rune, mod Modifier)
{ switch { case ch != 0 && mod == 0: v.EditWrite(ch) case key == KeySpace: v.EditWrite(' ') case key == KeyBackspace || key == KeyBackspace2: v.EditDelete(true) case key == KeyDelete: v.EditDelete(false) case key == KeyInsert: v.Overwrite = !v.Overwrite case key == KeyEnter: v.EditNewLine() case key == KeyArrowDown: v.MoveCursor(0, 1, false) case key == KeyArrowUp: v.MoveCursor(0, -1, false) case key == KeyArrowLeft: v.MoveCursor(-1, 0, false) case key == KeyArrowRight: v.MoveCursor(1, 0, false) } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L56-L59
go
train
// EditWrite writes a rune at the cursor position.
func (v *View) EditWrite(ch rune)
// EditWrite writes a rune at the cursor position. func (v *View) EditWrite(ch rune)
{ v.writeRune(v.cx, v.cy, ch) v.MoveCursor(1, 0, true) }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L63-L106
go
train
// EditDelete deletes a rune at the cursor position. back determines the // direction.
func (v *View) EditDelete(back bool)
// EditDelete deletes a rune at the cursor position. back determines the // direction. func (v *View) EditDelete(back bool)
{ x, y := v.ox+v.cx, v.oy+v.cy if y < 0 { return } else if y >= len(v.viewLines) { v.MoveCursor(-1, 0, true) return } maxX, _ := v.Size() if back { if x == 0 { // start of the line if y < 1 { return } var maxPrevWidth int if v.Wrap { maxPrevWidth = maxX } else { maxPrevWidth = maxInt } if v.viewLines[y].linesX == 0 { // regular line v.mergeLines(v.cy - 1) if len(v.viewLines[y-1].line) < maxPrevWidth { v.MoveCursor(-1, 0, true) } } else { // wrapped line v.deleteRune(len(v.viewLines[y-1].line)-1, v.cy-1) v.MoveCursor(-1, 0, true) } } else { // middle/end of the line v.deleteRune(v.cx-1, v.cy) v.MoveCursor(-1, 0, true) } } else { if x == len(v.viewLines[y].line) { // end of the line v.mergeLines(v.cy) } else { // start/middle of the line v.deleteRune(v.cx, v.cy) } } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L109-L114
go
train
// EditNewLine inserts a new line under the cursor.
func (v *View) EditNewLine()
// EditNewLine inserts a new line under the cursor. func (v *View) EditNewLine()
{ v.breakLine(v.cx, v.cy) v.ox = 0 v.cx = 0 v.MoveCursor(0, 1, true) }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L118-L231
go
train
// MoveCursor moves the cursor taking into account the width of the line/view, // displacing the origin if necessary.
func (v *View) MoveCursor(dx, dy int, writeMode bool)
// MoveCursor moves the cursor taking into account the width of the line/view, // displacing the origin if necessary. func (v *View) MoveCursor(dx, dy int, writeMode bool)
{ maxX, maxY := v.Size() cx, cy := v.cx+dx, v.cy+dy x, y := v.ox+cx, v.oy+cy var curLineWidth, prevLineWidth int // get the width of the current line if writeMode { if v.Wrap { curLineWidth = maxX - 1 } else { curLineWidth = maxInt } } else { if y >= 0 && y < len(v.viewLines) { curLineWidth = len(v.viewLines[y].line) if v.Wrap && curLineWidth >= maxX { curLineWidth = maxX - 1 } } else { curLineWidth = 0 } } // get the width of the previous line if y-1 >= 0 && y-1 < len(v.viewLines) { prevLineWidth = len(v.viewLines[y-1].line) } else { prevLineWidth = 0 } // adjust cursor's x position and view's x origin if x > curLineWidth { // move to next line if dx > 0 { // horizontal movement cy++ if writeMode || v.oy+cy < len(v.viewLines) { if !v.Wrap { v.ox = 0 } v.cx = 0 } } else { // vertical movement if curLineWidth > 0 { // move cursor to the EOL if v.Wrap { v.cx = curLineWidth } else { ncx := curLineWidth - v.ox if ncx < 0 { v.ox += ncx if v.ox < 0 { v.ox = 0 } v.cx = 0 } else { v.cx = ncx } } } else { if writeMode || v.oy+cy < len(v.viewLines) { if !v.Wrap { v.ox = 0 } v.cx = 0 } } } } else if cx < 0 { if !v.Wrap && v.ox > 0 { // move origin to the left v.ox += cx v.cx = 0 } else { // move to previous line cy-- if prevLineWidth > 0 { if !v.Wrap { // set origin so the EOL is visible nox := prevLineWidth - maxX + 1 if nox < 0 { v.ox = 0 } else { v.ox = nox } } v.cx = prevLineWidth } else { if !v.Wrap { v.ox = 0 } v.cx = 0 } } } else { // stay on the same line if v.Wrap { v.cx = cx } else { if cx >= maxX { v.ox += cx - maxX + 1 v.cx = maxX } else { v.cx = cx } } } // adjust cursor's y position and view's y origin if cy < 0 { if v.oy > 0 { v.oy-- } } else if writeMode || v.oy+cy < len(v.viewLines) { if cy >= maxY { v.oy++ } else { v.cy = cy } } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L237-L274
go
train
// writeRune writes a rune into the view's internal buffer, at the // position corresponding to the point (x, y). The length of the internal // buffer is increased if the point is out of bounds. Overwrite mode is // governed by the value of View.overwrite.
func (v *View) writeRune(x, y int, ch rune) error
// writeRune writes a rune into the view's internal buffer, at the // position corresponding to the point (x, y). The length of the internal // buffer is increased if the point is out of bounds. Overwrite mode is // governed by the value of View.overwrite. func (v *View) writeRune(x, y int, ch rune) error
{ v.tainted = true x, y, err := v.realPosition(x, y) if err != nil { return err } if x < 0 || y < 0 { return errors.New("invalid point") } if y >= len(v.lines) { s := make([][]cell, y-len(v.lines)+1) v.lines = append(v.lines, s...) } olen := len(v.lines[y]) var s []cell if x >= len(v.lines[y]) { s = make([]cell, x-len(v.lines[y])+1) } else if !v.Overwrite { s = make([]cell, 1) } v.lines[y] = append(v.lines[y], s...) if !v.Overwrite || (v.Overwrite && x >= olen-1) { copy(v.lines[y][x+1:], v.lines[y][x:]) } v.lines[y][x] = cell{ fgColor: v.FgColor, bgColor: v.BgColor, chr: ch, } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L278-L291
go
train
// deleteRune removes a rune from the view's internal buffer, at the // position corresponding to the point (x, y).
func (v *View) deleteRune(x, y int) error
// deleteRune removes a rune from the view's internal buffer, at the // position corresponding to the point (x, y). func (v *View) deleteRune(x, y int) error
{ v.tainted = true x, y, err := v.realPosition(x, y) if err != nil { return err } if x < 0 || y < 0 || y >= len(v.lines) || x >= len(v.lines[y]) { return errors.New("invalid point") } v.lines[y] = append(v.lines[y][:x], v.lines[y][x+1:]...) return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L294-L311
go
train
// mergeLines merges the lines "y" and "y+1" if possible.
func (v *View) mergeLines(y int) error
// mergeLines merges the lines "y" and "y+1" if possible. func (v *View) mergeLines(y int) error
{ v.tainted = true _, y, err := v.realPosition(0, y) if err != nil { return err } if y < 0 || y >= len(v.lines) { return errors.New("invalid point") } if y < len(v.lines)-1 { // otherwise we don't need to merge anything v.lines[y] = append(v.lines[y], v.lines[y+1]...) v.lines = append(v.lines[:y+1], v.lines[y+2:]...) } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
edit.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/edit.go#L315-L344
go
train
// breakLine breaks a line of the internal buffer at the position corresponding // to the point (x, y).
func (v *View) breakLine(x, y int) error
// breakLine breaks a line of the internal buffer at the position corresponding // to the point (x, y). func (v *View) breakLine(x, y int) error
{ v.tainted = true x, y, err := v.realPosition(x, y) if err != nil { return err } if y < 0 || y >= len(v.lines) { return errors.New("invalid point") } var left, right []cell if x < len(v.lines[y]) { // break line left = make([]cell, len(v.lines[y][:x])) copy(left, v.lines[y][:x]) right = make([]cell, len(v.lines[y][x:])) copy(right, v.lines[y][x:]) } else { // new empty line left = v.lines[y] } lines := make([][]cell, len(v.lines)+1) lines[y] = left lines[y+1] = right copy(lines, v.lines[:y]) copy(lines[y+2:], v.lines[y+1:]) v.lines = lines return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
keybinding.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/keybinding.go#L19-L28
go
train
// newKeybinding returns a new Keybinding object.
func newKeybinding(viewname string, key Key, ch rune, mod Modifier, handler func(*Gui, *View) error) (kb *keybinding)
// newKeybinding returns a new Keybinding object. func newKeybinding(viewname string, key Key, ch rune, mod Modifier, handler func(*Gui, *View) error) (kb *keybinding)
{ kb = &keybinding{ viewName: viewname, key: key, ch: ch, mod: mod, handler: handler, } return kb }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
keybinding.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/keybinding.go#L31-L33
go
train
// matchKeypress returns if the keybinding matches the keypress.
func (kb *keybinding) matchKeypress(key Key, ch rune, mod Modifier) bool
// matchKeypress returns if the keybinding matches the keypress. func (kb *keybinding) matchKeypress(key Key, ch rune, mod Modifier) bool
{ return kb.key == key && kb.ch == ch && kb.mod == mod }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
keybinding.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/keybinding.go#L36-L41
go
train
// matchView returns if the keybinding matches the current view.
func (kb *keybinding) matchView(v *View) bool
// matchView returns if the keybinding matches the current view. func (kb *keybinding) matchView(v *View) bool
{ if kb.viewName == "" { return true } return v != nil && kb.viewName == v.name }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L89-L95
go
train
// String returns a string from a given cell slice.
func (l lineType) String() string
// String returns a string from a given cell slice. func (l lineType) String() string
{ str := "" for _, c := range l { str += string(c.chr) } return str }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L98-L111
go
train
// newView returns a new View object.
func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View
// newView returns a new View object. func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View
{ v := &View{ name: name, x0: x0, y0: y0, x1: x1, y1: y1, Frame: true, Editor: DefaultEditor, tainted: true, ei: newEscapeInterpreter(mode), } return v }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L114-L116
go
train
// Size returns the number of visible columns and rows in the View.
func (v *View) Size() (x, y int)
// Size returns the number of visible columns and rows in the View. func (v *View) Size() (x, y int)
{ return v.x1 - v.x0 - 1, v.y1 - v.y0 - 1 }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L126-L160
go
train
// setRune sets a rune at the given point relative to the view. It applies the // specified colors, taking into account if the cell must be highlighted. Also, // it checks if the position is valid.
func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error
// setRune sets a rune at the given point relative to the view. It applies the // specified colors, taking into account if the cell must be highlighted. Also, // it checks if the position is valid. func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error
{ maxX, maxY := v.Size() if x < 0 || x >= maxX || y < 0 || y >= maxY { return errors.New("invalid point") } var ( ry, rcy int err error ) if v.Highlight { _, ry, err = v.realPosition(x, y) if err != nil { return err } _, rcy, err = v.realPosition(v.cx, v.cy) if err != nil { return err } } if v.Mask != 0 { fgColor = v.FgColor bgColor = v.BgColor ch = v.Mask } else if v.Highlight && ry == rcy { fgColor = v.SelFgColor bgColor = v.SelBgColor } termbox.SetCell(v.x0+x+1, v.y0+y+1, ch, termbox.Attribute(fgColor), termbox.Attribute(bgColor)) return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L164-L172
go
train
// SetCursor sets the cursor position of the view at the given point, // relative to the view. It checks if the position is valid.
func (v *View) SetCursor(x, y int) error
// SetCursor sets the cursor position of the view at the given point, // relative to the view. It checks if the position is valid. func (v *View) SetCursor(x, y int) error
{ maxX, maxY := v.Size() if x < 0 || x >= maxX || y < 0 || y >= maxY { return errors.New("invalid point") } v.cx = x v.cy = y return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L175-L177
go
train
// Cursor returns the cursor position of the view.
func (v *View) Cursor() (x, y int)
// Cursor returns the cursor position of the view. func (v *View) Cursor() (x, y int)
{ return v.cx, v.cy }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L184-L191
go
train
// SetOrigin sets the origin position of the view's internal buffer, // so the buffer starts to be printed from this point, which means that // it is linked with the origin point of view. It can be used to // implement Horizontal and Vertical scrolling with just incrementing // or decrementing ox and oy.
func (v *View) SetOrigin(x, y int) error
// SetOrigin sets the origin position of the view's internal buffer, // so the buffer starts to be printed from this point, which means that // it is linked with the origin point of view. It can be used to // implement Horizontal and Vertical scrolling with just incrementing // or decrementing ox and oy. func (v *View) SetOrigin(x, y int) error
{ if x < 0 || y < 0 { return errors.New("invalid point") } v.ox = x v.oy = y return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L194-L196
go
train
// Origin returns the origin position of the view.
func (v *View) Origin() (x, y int)
// Origin returns the origin position of the view. func (v *View) Origin() (x, y int)
{ return v.ox, v.oy }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L202-L231
go
train
// Write appends a byte slice into the view's internal buffer. Because // View implements the io.Writer interface, it can be passed as parameter // of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must // be called to clear the view's buffer.
func (v *View) Write(p []byte) (n int, err error)
// Write appends a byte slice into the view's internal buffer. Because // View implements the io.Writer interface, it can be passed as parameter // of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must // be called to clear the view's buffer. func (v *View) Write(p []byte) (n int, err error)
{ v.tainted = true for _, ch := range bytes.Runes(p) { switch ch { case '\n': v.lines = append(v.lines, nil) case '\r': nl := len(v.lines) if nl > 0 { v.lines[nl-1] = nil } else { v.lines = make([][]cell, 1) } default: cells := v.parseInput(ch) if cells == nil { continue } nl := len(v.lines) if nl > 0 { v.lines[nl-1] = append(v.lines[nl-1], cells...) } else { v.lines = append(v.lines, cells) } } } return len(p), nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L236-L263
go
train
// parseInput parses char by char the input written to the View. It returns nil // while processing ESC sequences. Otherwise, it returns a cell slice that // contains the processed data.
func (v *View) parseInput(ch rune) []cell
// parseInput parses char by char the input written to the View. It returns nil // while processing ESC sequences. Otherwise, it returns a cell slice that // contains the processed data. func (v *View) parseInput(ch rune) []cell
{ cells := []cell{} isEscape, err := v.ei.parseOne(ch) if err != nil { for _, r := range v.ei.runes() { c := cell{ fgColor: v.FgColor, bgColor: v.BgColor, chr: r, } cells = append(cells, c) } v.ei.reset() } else { if isEscape { return nil } c := cell{ fgColor: v.ei.curFgColor, bgColor: v.ei.curBgColor, chr: ch, } cells = append(cells, c) } return cells }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L268-L279
go
train
// Read reads data into p. It returns the number of bytes read into p. // At EOF, err will be io.EOF. Calling Read() after Rewind() makes the // cache to be refreshed with the contents of the view.
func (v *View) Read(p []byte) (n int, err error)
// Read reads data into p. It returns the number of bytes read into p. // At EOF, err will be io.EOF. Calling Read() after Rewind() makes the // cache to be refreshed with the contents of the view. func (v *View) Read(p []byte) (n int, err error)
{ if v.readOffset == 0 { v.readCache = v.Buffer() } if v.readOffset < len(v.readCache) { n = copy(p, v.readCache[v.readOffset:]) v.readOffset += n } else { err = io.EOF } return }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L288-L361
go
train
// draw re-draws the view's contents.
func (v *View) draw() error
// draw re-draws the view's contents. func (v *View) draw() error
{ maxX, maxY := v.Size() if v.Wrap { if maxX == 0 { return errors.New("X size of the view cannot be 0") } v.ox = 0 } if v.tainted { v.viewLines = nil for i, line := range v.lines { if v.Wrap { if len(line) < maxX { vline := viewLine{linesX: 0, linesY: i, line: line} v.viewLines = append(v.viewLines, vline) continue } else { for n := 0; n <= len(line); n += maxX { if len(line[n:]) <= maxX { vline := viewLine{linesX: n, linesY: i, line: line[n:]} v.viewLines = append(v.viewLines, vline) } else { vline := viewLine{linesX: n, linesY: i, line: line[n : n+maxX]} v.viewLines = append(v.viewLines, vline) } } } } else { vline := viewLine{linesX: 0, linesY: i, line: line} v.viewLines = append(v.viewLines, vline) } } v.tainted = false } if v.Autoscroll && len(v.viewLines) > maxY { v.oy = len(v.viewLines) - maxY } y := 0 for i, vline := range v.viewLines { if i < v.oy { continue } if y >= maxY { break } x := 0 for j, c := range vline.line { if j < v.ox { continue } if x >= maxX { break } fgColor := c.fgColor if fgColor == ColorDefault { fgColor = v.FgColor } bgColor := c.bgColor if bgColor == ColorDefault { bgColor = v.BgColor } if err := v.setRune(x, y, c.chr, fgColor, bgColor); err != nil { return err } x++ } y++ } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L365-L388
go
train
// realPosition returns the position in the internal buffer corresponding to the // point (x, y) of the view.
func (v *View) realPosition(vx, vy int) (x, y int, err error)
// realPosition returns the position in the internal buffer corresponding to the // point (x, y) of the view. func (v *View) realPosition(vx, vy int) (x, y int, err error)
{ vx = v.ox + vx vy = v.oy + vy if vx < 0 || vy < 0 { return 0, 0, errors.New("invalid point") } if len(v.viewLines) == 0 { return vx, vy, nil } if vy < len(v.viewLines) { vline := v.viewLines[vy] x = vline.linesX + vx y = vline.linesY } else { vline := v.viewLines[len(v.viewLines)-1] x = vx y = vline.linesY + vy - len(v.viewLines) + 1 } return x, y, nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L391-L398
go
train
// Clear empties the view's internal buffer.
func (v *View) Clear()
// Clear empties the view's internal buffer. func (v *View) Clear()
{ v.tainted = true v.lines = nil v.viewLines = nil v.readOffset = 0 v.clearRunes() }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L401-L409
go
train
// clearRunes erases all the cells in the view.
func (v *View) clearRunes()
// clearRunes erases all the cells in the view. func (v *View) clearRunes()
{ maxX, maxY := v.Size() for x := 0; x < maxX; x++ { for y := 0; y < maxY; y++ { termbox.SetCell(v.x0+x+1, v.y0+y+1, ' ', termbox.Attribute(v.FgColor), termbox.Attribute(v.BgColor)) } } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L413-L421
go
train
// BufferLines returns the lines in the view's internal // buffer.
func (v *View) BufferLines() []string
// BufferLines returns the lines in the view's internal // buffer. func (v *View) BufferLines() []string
{ lines := make([]string, len(v.lines)) for i, l := range v.lines { str := lineType(l).String() str = strings.Replace(str, "\x00", " ", -1) lines[i] = str } return lines }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L425-L431
go
train
// Buffer returns a string with the contents of the view's internal // buffer.
func (v *View) Buffer() string
// Buffer returns a string with the contents of the view's internal // buffer. func (v *View) Buffer() string
{ str := "" for _, l := range v.lines { str += lineType(l).String() + "\n" } return strings.Replace(str, "\x00", " ", -1) }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L435-L443
go
train
// ViewBufferLines returns the lines in the view's internal // buffer that is shown to the user.
func (v *View) ViewBufferLines() []string
// ViewBufferLines returns the lines in the view's internal // buffer that is shown to the user. func (v *View) ViewBufferLines() []string
{ lines := make([]string, len(v.viewLines)) for i, l := range v.viewLines { str := lineType(l.line).String() str = strings.Replace(str, "\x00", " ", -1) lines[i] = str } return lines }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L447-L453
go
train
// ViewBuffer returns a string with the contents of the view's buffer that is // shown to the user.
func (v *View) ViewBuffer() string
// ViewBuffer returns a string with the contents of the view's buffer that is // shown to the user. func (v *View) ViewBuffer() string
{ str := "" for _, l := range v.viewLines { str += lineType(l.line).String() + "\n" } return strings.Replace(str, "\x00", " ", -1) }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L457-L468
go
train
// Line returns a string with the line of the view's internal buffer // at the position corresponding to the point (x, y).
func (v *View) Line(y int) (string, error)
// Line returns a string with the line of the view's internal buffer // at the position corresponding to the point (x, y). func (v *View) Line(y int) (string, error)
{ _, y, err := v.realPosition(0, y) if err != nil { return "", err } if y < 0 || y >= len(v.lines) { return "", errors.New("invalid point") } return lineType(v.lines[y]).String(), nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
view.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/view.go#L472-L497
go
train
// Word returns a string with the word of the view's internal buffer // at the position corresponding to the point (x, y).
func (v *View) Word(x, y int) (string, error)
// Word returns a string with the word of the view's internal buffer // at the position corresponding to the point (x, y). func (v *View) Word(x, y int) (string, error)
{ x, y, err := v.realPosition(x, y) if err != nil { return "", err } if x < 0 || y < 0 || y >= len(v.lines) || x >= len(v.lines[y]) { return "", errors.New("invalid point") } str := lineType(v.lines[y]).String() nl := strings.LastIndexFunc(str[:x], indexFunc) if nl == -1 { nl = 0 } else { nl = nl + 1 } nr := strings.IndexFunc(str[x:], indexFunc) if nr == -1 { nr = len(str) } else { nr = nr + x } return string(str[nl:nr]), nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
escape.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/escape.go#L36-L53
go
train
// runes in case of error will output the non-parsed runes as a string.
func (ei *escapeInterpreter) runes() []rune
// runes in case of error will output the non-parsed runes as a string. func (ei *escapeInterpreter) runes() []rune
{ switch ei.state { case stateNone: return []rune{0x1b} case stateEscape: return []rune{0x1b, ei.curch} case stateCSI: return []rune{0x1b, '[', ei.curch} case stateParams: ret := []rune{0x1b, '['} for _, s := range ei.csiParam { ret = append(ret, []rune(s)...) ret = append(ret, ';') } return append(ret, ei.curch) } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
escape.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/escape.go#L57-L65
go
train
// newEscapeInterpreter returns an escapeInterpreter that will be able to parse // terminal escape sequences.
func newEscapeInterpreter(mode OutputMode) *escapeInterpreter
// newEscapeInterpreter returns an escapeInterpreter that will be able to parse // terminal escape sequences. func newEscapeInterpreter(mode OutputMode) *escapeInterpreter
{ ei := &escapeInterpreter{ state: stateNone, curFgColor: ColorDefault, curBgColor: ColorDefault, mode: mode, } return ei }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
escape.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/escape.go#L68-L73
go
train
// reset sets the escapeInterpreter in initial state.
func (ei *escapeInterpreter) reset()
// reset sets the escapeInterpreter in initial state. func (ei *escapeInterpreter) reset()
{ ei.state = stateNone ei.curFgColor = ColorDefault ei.curBgColor = ColorDefault ei.csiParam = nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
escape.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/escape.go#L78-L141
go
train
// parseOne parses a rune. If isEscape is true, it means that the rune is part // of an escape sequence, and as such should not be printed verbatim. Otherwise, // it's not an escape sequence.
func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error)
// parseOne parses a rune. If isEscape is true, it means that the rune is part // of an escape sequence, and as such should not be printed verbatim. Otherwise, // it's not an escape sequence. func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error)
{ // Sanity checks if len(ei.csiParam) > 20 { return false, errCSITooLong } if len(ei.csiParam) > 0 && len(ei.csiParam[len(ei.csiParam)-1]) > 255 { return false, errCSITooLong } ei.curch = ch switch ei.state { case stateNone: if ch == 0x1b { ei.state = stateEscape return true, nil } return false, nil case stateEscape: if ch == '[' { ei.state = stateCSI return true, nil } return false, errNotCSI case stateCSI: switch { case ch >= '0' && ch <= '9': ei.csiParam = append(ei.csiParam, "") case ch == 'm': ei.csiParam = append(ei.csiParam, "0") default: return false, errCSIParseError } ei.state = stateParams fallthrough case stateParams: switch { case ch >= '0' && ch <= '9': ei.csiParam[len(ei.csiParam)-1] += string(ch) return true, nil case ch == ';': ei.csiParam = append(ei.csiParam, "") return true, nil case ch == 'm': var err error switch ei.mode { case OutputNormal: err = ei.outputNormal() case Output256: err = ei.output256() } if err != nil { return false, errCSIParseError } ei.state = stateNone ei.csiParam = nil return true, nil default: return false, errCSIParseError } } return false, nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
escape.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/escape.go#L145-L174
go
train
// outputNormal provides 8 different colors: // black, red, green, yellow, blue, magenta, cyan, white
func (ei *escapeInterpreter) outputNormal() error
// outputNormal provides 8 different colors: // black, red, green, yellow, blue, magenta, cyan, white func (ei *escapeInterpreter) outputNormal() error
{ for _, param := range ei.csiParam { p, err := strconv.Atoi(param) if err != nil { return errCSIParseError } switch { case p >= 30 && p <= 37: ei.curFgColor = Attribute(p - 30 + 1) case p == 39: ei.curFgColor = ColorDefault case p >= 40 && p <= 47: ei.curBgColor = Attribute(p - 40 + 1) case p == 49: ei.curBgColor = ColorDefault case p == 1: ei.curFgColor |= AttrBold case p == 4: ei.curFgColor |= AttrUnderline case p == 7: ei.curFgColor |= AttrReverse case p == 0: ei.curFgColor = ColorDefault ei.curBgColor = ColorDefault } } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
escape.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/escape.go#L181-L229
go
train
// output256 allows you to leverage the 256-colors terminal mode: // 0x01 - 0x08: the 8 colors as in OutputNormal // 0x09 - 0x10: Color* | AttrBold // 0x11 - 0xe8: 216 different colors // 0xe9 - 0x1ff: 24 different shades of grey
func (ei *escapeInterpreter) output256() error
// output256 allows you to leverage the 256-colors terminal mode: // 0x01 - 0x08: the 8 colors as in OutputNormal // 0x09 - 0x10: Color* | AttrBold // 0x11 - 0xe8: 216 different colors // 0xe9 - 0x1ff: 24 different shades of grey func (ei *escapeInterpreter) output256() error
{ if len(ei.csiParam) < 3 { return ei.outputNormal() } mode, err := strconv.Atoi(ei.csiParam[1]) if err != nil { return errCSIParseError } if mode != 5 { return ei.outputNormal() } fgbg, err := strconv.Atoi(ei.csiParam[0]) if err != nil { return errCSIParseError } color, err := strconv.Atoi(ei.csiParam[2]) if err != nil { return errCSIParseError } switch fgbg { case 38: ei.curFgColor = Attribute(color + 1) for _, param := range ei.csiParam[3:] { p, err := strconv.Atoi(param) if err != nil { return errCSIParseError } switch { case p == 1: ei.curFgColor |= AttrBold case p == 4: ei.curFgColor |= AttrUnderline case p == 7: ei.curFgColor |= AttrReverse } } case 48: ei.curBgColor = Attribute(color + 1) default: return errCSIParseError } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L72-L91
go
train
// NewGui returns a new Gui object with a given output mode.
func NewGui(mode OutputMode) (*Gui, error)
// NewGui returns a new Gui object with a given output mode. func NewGui(mode OutputMode) (*Gui, error)
{ if err := termbox.Init(); err != nil { return nil, err } g := &Gui{} g.outputMode = mode termbox.SetOutputMode(termbox.OutputMode(mode)) g.tbEvents = make(chan termbox.Event, 20) g.userEvents = make(chan userEvent, 20) g.maxX, g.maxY = termbox.Size() g.BgColor, g.FgColor = ColorDefault, ColorDefault g.SelBgColor, g.SelFgColor = ColorDefault, ColorDefault return g, nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L100-L102
go
train
// Size returns the terminal's size.
func (g *Gui) Size() (x, y int)
// Size returns the terminal's size. func (g *Gui) Size() (x, y int)
{ return g.maxX, g.maxY }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L107-L113
go
train
// SetRune writes a rune at the given point, relative to the top-left // corner of the terminal. It checks if the position is valid and applies // the given colors.
func (g *Gui) SetRune(x, y int, ch rune, fgColor, bgColor Attribute) error
// SetRune writes a rune at the given point, relative to the top-left // corner of the terminal. It checks if the position is valid and applies // the given colors. func (g *Gui) SetRune(x, y int, ch rune, fgColor, bgColor Attribute) error
{ if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY { return errors.New("invalid point") } termbox.SetCell(x, y, ch, termbox.Attribute(fgColor), termbox.Attribute(bgColor)) return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L117-L123
go
train
// Rune returns the rune contained in the cell at the given position. // It checks if the position is valid.
func (g *Gui) Rune(x, y int) (rune, error)
// Rune returns the rune contained in the cell at the given position. // It checks if the position is valid. func (g *Gui) Rune(x, y int) (rune, error)
{ if x < 0 || y < 0 || x >= g.maxX || y >= g.maxY { return ' ', errors.New("invalid point") } c := termbox.CellBuffer()[y*g.maxX+x] return c.Ch, nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L130-L152
go
train
// SetView creates a new view with its top-left corner at (x0, y0) // and the bottom-right one at (x1, y1). If a view with the same name // already exists, its dimensions are updated; otherwise, the error // ErrUnknownView is returned, which allows to assert if the View must // be initialized. It checks if the position is valid.
func (g *Gui) SetView(name string, x0, y0, x1, y1 int) (*View, error)
// SetView creates a new view with its top-left corner at (x0, y0) // and the bottom-right one at (x1, y1). If a view with the same name // already exists, its dimensions are updated; otherwise, the error // ErrUnknownView is returned, which allows to assert if the View must // be initialized. It checks if the position is valid. func (g *Gui) SetView(name string, x0, y0, x1, y1 int) (*View, error)
{ if x0 >= x1 || y0 >= y1 { return nil, errors.New("invalid dimensions") } if name == "" { return nil, errors.New("invalid name") } if v, err := g.View(name); err == nil { v.x0 = x0 v.y0 = y0 v.x1 = x1 v.y1 = y1 v.tainted = true return v, nil } v := newView(name, x0, y0, x1, y1, g.outputMode) v.BgColor, v.FgColor = g.BgColor, g.FgColor v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor g.views = append(g.views, v) return v, ErrUnknownView }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L155-L164
go
train
// SetViewOnTop sets the given view on top of the existing ones.
func (g *Gui) SetViewOnTop(name string) (*View, error)
// SetViewOnTop sets the given view on top of the existing ones. func (g *Gui) SetViewOnTop(name string) (*View, error)
{ for i, v := range g.views { if v.name == name { s := append(g.views[:i], g.views[i+1:]...) g.views = append(s, v) return v, nil } } return nil, ErrUnknownView }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L185-L192
go
train
// View returns a pointer to the view with the given name, or error // ErrUnknownView if a view with that name does not exist.
func (g *Gui) View(name string) (*View, error)
// View returns a pointer to the view with the given name, or error // ErrUnknownView if a view with that name does not exist. func (g *Gui) View(name string) (*View, error)
{ for _, v := range g.views { if v.name == name { return v, nil } } return nil, ErrUnknownView }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L196-L205
go
train
// ViewByPosition returns a pointer to a view matching the given position, or // error ErrUnknownView if a view in that position does not exist.
func (g *Gui) ViewByPosition(x, y int) (*View, error)
// ViewByPosition returns a pointer to a view matching the given position, or // error ErrUnknownView if a view in that position does not exist. func (g *Gui) ViewByPosition(x, y int) (*View, error)
{ // traverse views in reverse order checking top views first for i := len(g.views); i > 0; i-- { v := g.views[i-1] if x > v.x0 && x < v.x1 && y > v.y0 && y < v.y1 { return v, nil } } return nil, ErrUnknownView }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L209-L216
go
train
// ViewPosition returns the coordinates of the view with the given name, or // error ErrUnknownView if a view with that name does not exist.
func (g *Gui) ViewPosition(name string) (x0, y0, x1, y1 int, err error)
// ViewPosition returns the coordinates of the view with the given name, or // error ErrUnknownView if a view with that name does not exist. func (g *Gui) ViewPosition(name string) (x0, y0, x1, y1 int, err error)
{ for _, v := range g.views { if v.name == name { return v.x0, v.y0, v.x1, v.y1, nil } } return 0, 0, 0, 0, ErrUnknownView }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L219-L227
go
train
// DeleteView deletes a view by name.
func (g *Gui) DeleteView(name string) error
// DeleteView deletes a view by name. func (g *Gui) DeleteView(name string) error
{ for i, v := range g.views { if v.name == name { g.views = append(g.views[:i], g.views[i+1:]...) return nil } } return ErrUnknownView }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L249-L259
go
train
// SetKeybinding creates a new keybinding. If viewname equals to "" // (empty string) then the keybinding will apply to all views. key must // be a rune or a Key.
func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, handler func(*Gui, *View) error) error
// SetKeybinding creates a new keybinding. If viewname equals to "" // (empty string) then the keybinding will apply to all views. key must // be a rune or a Key. func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, handler func(*Gui, *View) error) error
{ var kb *keybinding k, ch, err := getKey(key) if err != nil { return err } kb = newKeybinding(viewname, k, ch, mod, handler) g.keybindings = append(g.keybindings, kb) return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L262-L275
go
train
// DeleteKeybinding deletes a keybinding.
func (g *Gui) DeleteKeybinding(viewname string, key interface{}, mod Modifier) error
// DeleteKeybinding deletes a keybinding. func (g *Gui) DeleteKeybinding(viewname string, key interface{}, mod Modifier) error
{ k, ch, err := getKey(key) if err != nil { return err } for i, kb := range g.keybindings { if kb.viewName == viewname && kb.ch == ch && kb.key == k && kb.mod == mod { g.keybindings = append(g.keybindings[:i], g.keybindings[i+1:]...) return nil } } return errors.New("keybinding not found") }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L278-L286
go
train
// DeleteKeybindings deletes all keybindings of view.
func (g *Gui) DeleteKeybindings(viewname string)
// DeleteKeybindings deletes all keybindings of view. func (g *Gui) DeleteKeybindings(viewname string)
{ var s []*keybinding for _, kb := range g.keybindings { if kb.viewName != viewname { s = append(s, kb) } } g.keybindings = s }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L290-L299
go
train
// getKey takes an empty interface with a key and returns the corresponding // typed Key or rune.
func getKey(key interface{}) (Key, rune, error)
// getKey takes an empty interface with a key and returns the corresponding // typed Key or rune. func getKey(key interface{}) (Key, rune, error)
{ switch t := key.(type) { case Key: return t, 0, nil case rune: return 0, t, nil default: return 0, 0, errors.New("unknown type") } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L311-L313
go
train
// Update executes the passed function. This method can be called safely from a // goroutine in order to update the GUI. It is important to note that the // passed function won't be executed immediately, instead it will be added to // the user events queue. Given that Update spawns a goroutine, the order in // which the user events will be handled is not guaranteed.
func (g *Gui) Update(f func(*Gui) error)
// Update executes the passed function. This method can be called safely from a // goroutine in order to update the GUI. It is important to note that the // passed function won't be executed immediately, instead it will be added to // the user events queue. Given that Update spawns a goroutine, the order in // which the user events will be handled is not guaranteed. func (g *Gui) Update(f func(*Gui) error)
{ go func() { g.userEvents <- userEvent{f: f} }() }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L334-L341
go
train
// SetManager sets the given GUI managers. It deletes all views and // keybindings.
func (g *Gui) SetManager(managers ...Manager)
// SetManager sets the given GUI managers. It deletes all views and // keybindings. func (g *Gui) SetManager(managers ...Manager)
{ g.managers = managers g.currentView = nil g.views = nil g.keybindings = nil go func() { g.tbEvents <- termbox.Event{Type: termbox.EventResize} }() }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L345-L347
go
train
// SetManagerFunc sets the given manager function. It deletes all views and // keybindings.
func (g *Gui) SetManagerFunc(manager func(*Gui) error)
// SetManagerFunc sets the given manager function. It deletes all views and // keybindings. func (g *Gui) SetManagerFunc(manager func(*Gui) error)
{ g.SetManager(ManagerFunc(manager)) }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L351-L388
go
train
// MainLoop runs the main loop until an error is returned. A successful // finish should return ErrQuit.
func (g *Gui) MainLoop() error
// MainLoop runs the main loop until an error is returned. A successful // finish should return ErrQuit. func (g *Gui) MainLoop() error
{ go func() { for { g.tbEvents <- termbox.PollEvent() } }() inputMode := termbox.InputAlt if g.InputEsc { inputMode = termbox.InputEsc } if g.Mouse { inputMode |= termbox.InputMouse } termbox.SetInputMode(inputMode) if err := g.flush(); err != nil { return err } for { select { case ev := <-g.tbEvents: if err := g.handleEvent(&ev); err != nil { return err } case ev := <-g.userEvents: if err := ev.f(g); err != nil { return err } } if err := g.consumeevents(); err != nil { return err } if err := g.flush(); err != nil { return err } } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L391-L406
go
train
// consumeevents handles the remaining events in the events pool.
func (g *Gui) consumeevents() error
// consumeevents handles the remaining events in the events pool. func (g *Gui) consumeevents() error
{ for { select { case ev := <-g.tbEvents: if err := g.handleEvent(&ev); err != nil { return err } case ev := <-g.userEvents: if err := ev.f(g); err != nil { return err } default: return nil } } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L410-L419
go
train
// handleEvent handles an event, based on its type (key-press, error, // etc.)
func (g *Gui) handleEvent(ev *termbox.Event) error
// handleEvent handles an event, based on its type (key-press, error, // etc.) func (g *Gui) handleEvent(ev *termbox.Event) error
{ switch ev.Type { case termbox.EventKey, termbox.EventMouse: return g.onKey(ev) case termbox.EventError: return ev.Err default: return nil } }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L422-L468
go
train
// flush updates the gui, re-drawing frames and buffers.
func (g *Gui) flush() error
// flush updates the gui, re-drawing frames and buffers. func (g *Gui) flush() error
{ termbox.Clear(termbox.Attribute(g.FgColor), termbox.Attribute(g.BgColor)) maxX, maxY := termbox.Size() // if GUI's size has changed, we need to redraw all views if maxX != g.maxX || maxY != g.maxY { for _, v := range g.views { v.tainted = true } } g.maxX, g.maxY = maxX, maxY for _, m := range g.managers { if err := m.Layout(g); err != nil { return err } } for _, v := range g.views { if v.Frame { var fgColor, bgColor Attribute if g.Highlight && v == g.currentView { fgColor = g.SelFgColor bgColor = g.SelBgColor } else { fgColor = g.FgColor bgColor = g.BgColor } if err := g.drawFrameEdges(v, fgColor, bgColor); err != nil { return err } if err := g.drawFrameCorners(v, fgColor, bgColor); err != nil { return err } if v.Title != "" { if err := g.drawTitle(v, fgColor, bgColor); err != nil { return err } } } if err := g.draw(v); err != nil { return err } } termbox.Flush() return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L471-L508
go
train
// drawFrameEdges draws the horizontal and vertical edges of a view.
func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error
// drawFrameEdges draws the horizontal and vertical edges of a view. func (g *Gui) drawFrameEdges(v *View, fgColor, bgColor Attribute) error
{ runeH, runeV := '─', '│' if g.ASCII { runeH, runeV = '-', '|' } for x := v.x0 + 1; x < v.x1 && x < g.maxX; x++ { if x < 0 { continue } if v.y0 > -1 && v.y0 < g.maxY { if err := g.SetRune(x, v.y0, runeH, fgColor, bgColor); err != nil { return err } } if v.y1 > -1 && v.y1 < g.maxY { if err := g.SetRune(x, v.y1, runeH, fgColor, bgColor); err != nil { return err } } } for y := v.y0 + 1; y < v.y1 && y < g.maxY; y++ { if y < 0 { continue } if v.x0 > -1 && v.x0 < g.maxX { if err := g.SetRune(v.x0, y, runeV, fgColor, bgColor); err != nil { return err } } if v.x1 > -1 && v.x1 < g.maxX { if err := g.SetRune(v.x1, y, runeV, fgColor, bgColor); err != nil { return err } } } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L511-L530
go
train
// drawFrameCorners draws the corners of the view.
func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error
// drawFrameCorners draws the corners of the view. func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error
{ runeTL, runeTR, runeBL, runeBR := '┌', '┐', '└', '┘' if g.ASCII { runeTL, runeTR, runeBL, runeBR = '+', '+', '+', '+' } corners := []struct { x, y int ch rune }{{v.x0, v.y0, runeTL}, {v.x1, v.y0, runeTR}, {v.x0, v.y1, runeBL}, {v.x1, v.y1, runeBR}} for _, c := range corners { if c.x >= 0 && c.y >= 0 && c.x < g.maxX && c.y < g.maxY { if err := g.SetRune(c.x, c.y, c.ch, fgColor, bgColor); err != nil { return err } } } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L533-L550
go
train
// drawTitle draws the title of the view.
func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error
// drawTitle draws the title of the view. func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error
{ if v.y0 < 0 || v.y0 >= g.maxY { return nil } for i, ch := range v.Title { x := v.x0 + i + 2 if x < 0 { continue } else if x > v.x1-2 || x >= g.maxX { break } if err := g.SetRune(x, v.y0, ch, fgColor, bgColor); err != nil { return err } } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L553-L585
go
train
// draw manages the cursor and calls the draw function of a view.
func (g *Gui) draw(v *View) error
// draw manages the cursor and calls the draw function of a view. func (g *Gui) draw(v *View) error
{ if g.Cursor { if curview := g.currentView; curview != nil { vMaxX, vMaxY := curview.Size() if curview.cx < 0 { curview.cx = 0 } else if curview.cx >= vMaxX { curview.cx = vMaxX - 1 } if curview.cy < 0 { curview.cy = 0 } else if curview.cy >= vMaxY { curview.cy = vMaxY - 1 } gMaxX, gMaxY := g.Size() cx, cy := curview.x0+curview.cx+1, curview.y0+curview.cy+1 if cx >= 0 && cx < gMaxX && cy >= 0 && cy < gMaxY { termbox.SetCursor(cx, cy) } else { termbox.HideCursor() } } } else { termbox.HideCursor() } v.clearRunes() if err := v.draw(); err != nil { return err } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L590-L618
go
train
// onKey manages key-press events. A keybinding handler is called when // a key-press or mouse event satisfies a configured keybinding. Furthermore, // currentView's internal buffer is modified if currentView.Editable is true.
func (g *Gui) onKey(ev *termbox.Event) error
// onKey manages key-press events. A keybinding handler is called when // a key-press or mouse event satisfies a configured keybinding. Furthermore, // currentView's internal buffer is modified if currentView.Editable is true. func (g *Gui) onKey(ev *termbox.Event) error
{ switch ev.Type { case termbox.EventKey: matched, err := g.execKeybindings(g.currentView, ev) if err != nil { return err } if matched { break } if g.currentView != nil && g.currentView.Editable && g.currentView.Editor != nil { g.currentView.Editor.Edit(g.currentView, Key(ev.Key), ev.Ch, Modifier(ev.Mod)) } case termbox.EventMouse: mx, my := ev.MouseX, ev.MouseY v, err := g.ViewByPosition(mx, my) if err != nil { break } if err := v.SetCursor(mx-v.x0-1, my-v.y0-1); err != nil { return err } if _, err := g.execKeybindings(v, ev); err != nil { return err } } return nil }
jroimartin/gocui
c055c87ae801372cd74a0839b972db4f7697ae5f
gui.go
https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/gui.go#L622-L636
go
train
// execKeybindings executes the keybinding handlers that match the passed view // and event. The value of matched is true if there is a match and no errors.
func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err error)
// execKeybindings executes the keybinding handlers that match the passed view // and event. The value of matched is true if there is a match and no errors. func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err error)
{ matched = false for _, kb := range g.keybindings { if kb.handler == nil { continue } if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(v) { if err := kb.handler(g, v); err != nil { return false, err } matched = true } } return matched, nil }
envoyproxy/protoc-gen-validate
fcf5978a9d1e0d26f3239a013c54dc65dae6c768
templates/java/register.go
https://github.com/envoyproxy/protoc-gen-validate/blob/fcf5978a9d1e0d26f3239a013c54dc65dae6c768/templates/java/register.go#L193-L208
go
train
// Replace invalid identifier characters with an underscore
func makeInvalidClassnameCharactersUnderscores(name string) string
// Replace invalid identifier characters with an underscore func makeInvalidClassnameCharactersUnderscores(name string) string
{ var sb strings.Builder for _, c := range name { switch { case c >= '0' && c <= '9': sb.WriteRune(c) case c >= 'a' && c <= 'z': sb.WriteRune(c) case c >= 'A' && c <= 'Z': sb.WriteRune(c) default: sb.WriteRune('_') } } return sb.String() }
envoyproxy/protoc-gen-validate
fcf5978a9d1e0d26f3239a013c54dc65dae6c768
templates/shared/reflection.go
https://github.com/envoyproxy/protoc-gen-validate/blob/fcf5978a9d1e0d26f3239a013c54dc65dae6c768/templates/shared/reflection.go#L24-L28
go
train
// Has returns true if the provided Message has the a field fld.
func Has(msg proto.Message, fld string) bool
// Has returns true if the provided Message has the a field fld. func Has(msg proto.Message, fld string) bool
{ val := extractVal(msg) return val.IsValid() && val.FieldByName(fld).IsValid() }
envoyproxy/protoc-gen-validate
fcf5978a9d1e0d26f3239a013c54dc65dae6c768
templates/shared/disabled.go
https://github.com/envoyproxy/protoc-gen-validate/blob/fcf5978a9d1e0d26f3239a013c54dc65dae6c768/templates/shared/disabled.go#L9-L12
go
train
// Disabled returns true if validations are disabled for msg
func Disabled(msg pgs.Message) (disabled bool, err error)
// Disabled returns true if validations are disabled for msg func Disabled(msg pgs.Message) (disabled bool, err error)
{ _, err = msg.Extension(validate.E_Disabled, &disabled) return }
envoyproxy/protoc-gen-validate
fcf5978a9d1e0d26f3239a013c54dc65dae6c768
templates/shared/disabled.go
https://github.com/envoyproxy/protoc-gen-validate/blob/fcf5978a9d1e0d26f3239a013c54dc65dae6c768/templates/shared/disabled.go#L15-L18
go
train
// RequiredOneOf returns true if the oneof field requires a field to be set
func RequiredOneOf(oo pgs.OneOf) (required bool, err error)
// RequiredOneOf returns true if the oneof field requires a field to be set func RequiredOneOf(oo pgs.OneOf) (required bool, err error)
{ _, err = oo.Extension(validate.E_Required, &required) return }
envoyproxy/protoc-gen-validate
fcf5978a9d1e0d26f3239a013c54dc65dae6c768
templates/shared/well_known.go
https://github.com/envoyproxy/protoc-gen-validate/blob/fcf5978a9d1e0d26f3239a013c54dc65dae6c768/templates/shared/well_known.go#L17-L47
go
train
// Needs returns true if a well-known string validator is needed for this // message.
func Needs(m pgs.Message, wk WellKnown) bool
// Needs returns true if a well-known string validator is needed for this // message. func Needs(m pgs.Message, wk WellKnown) bool
{ for _, f := range m.Fields() { var rules validate.FieldRules if _, err := f.Extension(validate.E_Rules, &rules); err != nil { continue } switch { case f.Type().IsRepeated() && f.Type().Element().ProtoType() == pgs.StringT: if strRulesNeeds(rules.GetRepeated().GetItems().GetString_(), wk) { return true } case f.Type().IsMap(): if f.Type().Key().ProtoType() == pgs.StringT && strRulesNeeds(rules.GetMap().GetKeys().GetString_(), wk) { return true } if f.Type().Element().ProtoType() == pgs.StringT && strRulesNeeds(rules.GetMap().GetValues().GetString_(), wk) { return true } case f.Type().ProtoType() == pgs.StringT: if strRulesNeeds(rules.GetString_(), wk) { return true } } } return false }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L19-L21
go
train
// AddrAdd will add an IP address to a link device. // Equivalent to: `ip addr add $addr dev $link`
func AddrAdd(link Link, addr *Addr) error
// AddrAdd will add an IP address to a link device. // Equivalent to: `ip addr add $addr dev $link` func AddrAdd(link Link, addr *Addr) error
{ return pkgHandle.AddrAdd(link, addr) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L25-L28
go
train
// AddrAdd will add an IP address to a link device. // Equivalent to: `ip addr add $addr dev $link`
func (h *Handle) AddrAdd(link Link, addr *Addr) error
// AddrAdd will add an IP address to a link device. // Equivalent to: `ip addr add $addr dev $link` func (h *Handle) AddrAdd(link Link, addr *Addr) error
{ req := h.newNetlinkRequest(unix.RTM_NEWADDR, unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.NLM_F_ACK) return h.addrHandle(link, addr, req) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L32-L34
go
train
// AddrReplace will replace (or, if not present, add) an IP address on a link device. // Equivalent to: `ip addr replace $addr dev $link`
func AddrReplace(link Link, addr *Addr) error
// AddrReplace will replace (or, if not present, add) an IP address on a link device. // Equivalent to: `ip addr replace $addr dev $link` func AddrReplace(link Link, addr *Addr) error
{ return pkgHandle.AddrReplace(link, addr) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L38-L41
go
train
// AddrReplace will replace (or, if not present, add) an IP address on a link device. // Equivalent to: `ip addr replace $addr dev $link`
func (h *Handle) AddrReplace(link Link, addr *Addr) error
// AddrReplace will replace (or, if not present, add) an IP address on a link device. // Equivalent to: `ip addr replace $addr dev $link` func (h *Handle) AddrReplace(link Link, addr *Addr) error
{ req := h.newNetlinkRequest(unix.RTM_NEWADDR, unix.NLM_F_CREATE|unix.NLM_F_REPLACE|unix.NLM_F_ACK) return h.addrHandle(link, addr, req) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L45-L47
go
train
// AddrDel will delete an IP address from a link device. // Equivalent to: `ip addr del $addr dev $link`
func AddrDel(link Link, addr *Addr) error
// AddrDel will delete an IP address from a link device. // Equivalent to: `ip addr del $addr dev $link` func AddrDel(link Link, addr *Addr) error
{ return pkgHandle.AddrDel(link, addr) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L51-L54
go
train
// AddrDel will delete an IP address from a link device. // Equivalent to: `ip addr del $addr dev $link`
func (h *Handle) AddrDel(link Link, addr *Addr) error
// AddrDel will delete an IP address from a link device. // Equivalent to: `ip addr del $addr dev $link` func (h *Handle) AddrDel(link Link, addr *Addr) error
{ req := h.newNetlinkRequest(unix.RTM_DELADDR, unix.NLM_F_ACK) return h.addrHandle(link, addr, req) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L144-L146
go
train
// AddrList gets a list of IP addresses in the system. // Equivalent to: `ip addr show`. // The list can be filtered by link and ip family.
func AddrList(link Link, family int) ([]Addr, error)
// AddrList gets a list of IP addresses in the system. // Equivalent to: `ip addr show`. // The list can be filtered by link and ip family. func AddrList(link Link, family int) ([]Addr, error)
{ return pkgHandle.AddrList(link, family) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L151-L188
go
train
// AddrList gets a list of IP addresses in the system. // Equivalent to: `ip addr show`. // The list can be filtered by link and ip family.
func (h *Handle) AddrList(link Link, family int) ([]Addr, error)
// AddrList gets a list of IP addresses in the system. // Equivalent to: `ip addr show`. // The list can be filtered by link and ip family. func (h *Handle) AddrList(link Link, family int) ([]Addr, error)
{ req := h.newNetlinkRequest(unix.RTM_GETADDR, unix.NLM_F_DUMP) msg := nl.NewIfInfomsg(family) req.AddData(msg) msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWADDR) if err != nil { return nil, err } indexFilter := 0 if link != nil { base := link.Attrs() h.ensureIndex(base) indexFilter = base.Index } var res []Addr for _, m := range msgs { addr, msgFamily, ifindex, err := parseAddr(m) if err != nil { return res, err } if link != nil && ifindex != indexFilter { // Ignore messages from other interfaces continue } if family != FAMILY_ALL && msgFamily != family { continue } res = append(res, addr) } return res, nil }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L272-L274
go
train
// AddrSubscribe takes a chan down which notifications will be sent // when addresses change. Close the 'done' chan to stop subscription.
func AddrSubscribe(ch chan<- AddrUpdate, done <-chan struct{}) error
// AddrSubscribe takes a chan down which notifications will be sent // when addresses change. Close the 'done' chan to stop subscription. func AddrSubscribe(ch chan<- AddrUpdate, done <-chan struct{}) error
{ return addrSubscribeAt(netns.None(), netns.None(), ch, done, nil, false) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L278-L280
go
train
// AddrSubscribeAt works like AddrSubscribe plus it allows the caller // to choose the network namespace in which to subscribe (ns).
func AddrSubscribeAt(ns netns.NsHandle, ch chan<- AddrUpdate, done <-chan struct{}) error
// AddrSubscribeAt works like AddrSubscribe plus it allows the caller // to choose the network namespace in which to subscribe (ns). func AddrSubscribeAt(ns netns.NsHandle, ch chan<- AddrUpdate, done <-chan struct{}) error
{ return addrSubscribeAt(ns, netns.None(), ch, done, nil, false) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
addr_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/addr_linux.go#L293-L299
go
train
// AddrSubscribeWithOptions work like AddrSubscribe but enable to // provide additional options to modify the behavior. Currently, the // namespace can be provided as well as an error callback.
func AddrSubscribeWithOptions(ch chan<- AddrUpdate, done <-chan struct{}, options AddrSubscribeOptions) error
// AddrSubscribeWithOptions work like AddrSubscribe but enable to // provide additional options to modify the behavior. Currently, the // namespace can be provided as well as an error callback. func AddrSubscribeWithOptions(ch chan<- AddrUpdate, done <-chan struct{}, options AddrSubscribeOptions) error
{ if options.Namespace == nil { none := netns.None() options.Namespace = &none } return addrSubscribeAt(*options.Namespace, netns.None(), ch, done, options.ErrorCallback, options.ListExisting) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
class_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/class_linux.go#L32-L63
go
train
// NewHtbClass NOTE: function is in here because it uses other linux functions
func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass
// NewHtbClass NOTE: function is in here because it uses other linux functions func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass
{ mtu := 1600 rate := cattrs.Rate / 8 ceil := cattrs.Ceil / 8 buffer := cattrs.Buffer cbuffer := cattrs.Cbuffer if ceil == 0 { ceil = rate } if buffer == 0 { buffer = uint32(float64(rate)/Hz() + float64(mtu)) } buffer = uint32(Xmittime(rate, buffer)) if cbuffer == 0 { cbuffer = uint32(float64(ceil)/Hz() + float64(mtu)) } cbuffer = uint32(Xmittime(ceil, cbuffer)) return &HtbClass{ ClassAttrs: attrs, Rate: rate, Ceil: ceil, Buffer: buffer, Cbuffer: cbuffer, Quantum: 10, Level: 0, Prio: 0, } }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
class_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/class_linux.go#L73-L75
go
train
// ClassDel will delete a class from the system. // Equivalent to: `tc class del $class`
func (h *Handle) ClassDel(class Class) error
// ClassDel will delete a class from the system. // Equivalent to: `tc class del $class` func (h *Handle) ClassDel(class Class) error
{ return h.classModify(unix.RTM_DELTCLASS, 0, class) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
class_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/class_linux.go#L87-L89
go
train
// ClassChange will change a class in place // Equivalent to: `tc class change $class` // The parent and handle MUST NOT be changed.
func (h *Handle) ClassChange(class Class) error
// ClassChange will change a class in place // Equivalent to: `tc class change $class` // The parent and handle MUST NOT be changed. func (h *Handle) ClassChange(class Class) error
{ return h.classModify(unix.RTM_NEWTCLASS, 0, class) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
class_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/class_linux.go#L105-L107
go
train
// ClassReplace will replace a class to the system. // quivalent to: `tc class replace $class` // The handle MAY be changed. // If a class already exist with this parent/handle pair, the class is changed. // If a class does not already exist with this parent/handle, a new class is created.
func (h *Handle) ClassReplace(class Class) error
// ClassReplace will replace a class to the system. // quivalent to: `tc class replace $class` // The handle MAY be changed. // If a class already exist with this parent/handle pair, the class is changed. // If a class does not already exist with this parent/handle, a new class is created. func (h *Handle) ClassReplace(class Class) error
{ return h.classModify(unix.RTM_NEWTCLASS, unix.NLM_F_CREATE, class) }
vishvananda/netlink
fd97bf4e47867b5e794234baa6b8a7746135ec10
class_linux.go
https://github.com/vishvananda/netlink/blob/fd97bf4e47867b5e794234baa6b8a7746135ec10/class_linux.go#L117-L123
go
train
// ClassAdd will add a class to the system. // Equivalent to: `tc class add $class`
func (h *Handle) ClassAdd(class Class) error
// ClassAdd will add a class to the system. // Equivalent to: `tc class add $class` func (h *Handle) ClassAdd(class Class) error
{ return h.classModify( unix.RTM_NEWTCLASS, unix.NLM_F_CREATE|unix.NLM_F_EXCL, class, ) }