_id
stringlengths 2
7
| title
stringlengths 1
118
| partition
stringclasses 3
values | text
stringlengths 52
85.5k
| language
stringclasses 1
value | meta_information
dict |
|---|---|---|---|---|---|
q6500
|
FileLabel
|
train
|
func FileLabel(fpath string) (string, error) {
if fpath == "" {
return "", ErrEmptyPath
}
label, err := lgetxattr(fpath, xattrNameSelinux)
if err != nil {
return "", err
}
// Trim the NUL byte at the end of the byte buffer, if present.
if len(label) > 0 && label[len(label)-1] == '\x00' {
label = label[:len(label)-1]
}
return string(label), nil
}
|
go
|
{
"resource": ""
}
|
q6501
|
SetKeyLabel
|
train
|
func SetKeyLabel(label string) error {
err := writeCon("/proc/self/attr/keycreate", label)
if os.IsNotExist(err) {
return nil
}
if label == "" && os.IsPermission(err) && !GetEnabled() {
return nil
}
return err
}
|
go
|
{
"resource": ""
}
|
q6502
|
Get
|
train
|
func (c Context) Get() string {
if c["level"] != "" {
return fmt.Sprintf("%s:%s:%s:%s", c["user"], c["role"], c["type"], c["level"])
}
return fmt.Sprintf("%s:%s:%s", c["user"], c["role"], c["type"])
}
|
go
|
{
"resource": ""
}
|
q6503
|
ClearLabels
|
train
|
func ClearLabels() {
state.Lock()
state.mcsList = make(map[string]bool)
state.Unlock()
}
|
go
|
{
"resource": ""
}
|
q6504
|
EnforceMode
|
train
|
func EnforceMode() int {
var enforce int
enforceS, err := readCon(selinuxEnforcePath())
if err != nil {
return -1
}
enforce, err = strconv.Atoi(string(enforceS))
if err != nil {
return -1
}
return enforce
}
|
go
|
{
"resource": ""
}
|
q6505
|
badPrefix
|
train
|
func badPrefix(fpath string) error {
if fpath == "" {
return ErrEmptyPath
}
badPrefixes := []string{"/usr"}
for _, prefix := range badPrefixes {
if strings.HasPrefix(fpath, prefix) {
return fmt.Errorf("relabeling content in %s is not allowed", prefix)
}
}
return nil
}
|
go
|
{
"resource": ""
}
|
q6506
|
Chcon
|
train
|
func Chcon(fpath string, label string, recurse bool) error {
if fpath == "" {
return ErrEmptyPath
}
if label == "" {
return nil
}
if err := badPrefix(fpath); err != nil {
return err
}
callback := func(p string, info os.FileInfo, err error) error {
e := SetFileLabel(p, label)
if os.IsNotExist(e) {
return nil
}
return e
}
if recurse {
return filepath.Walk(fpath, callback)
}
return SetFileLabel(fpath, label)
}
|
go
|
{
"resource": ""
}
|
q6507
|
DupSecOpt
|
train
|
func DupSecOpt(src string) ([]string, error) {
if src == "" {
return nil, nil
}
con, err := NewContext(src)
if err != nil {
return nil, err
}
if con["user"] == "" ||
con["role"] == "" ||
con["type"] == "" {
return nil, nil
}
dup := []string{"user:" + con["user"],
"role:" + con["role"],
"type:" + con["type"],
}
if con["level"] != "" {
dup = append(dup, "level:"+con["level"])
}
return dup, nil
}
|
go
|
{
"resource": ""
}
|
q6508
|
addToPathMap
|
train
|
func addToPathMap(info *descriptor.SourceCodeInfo, i interface{}, path []int32) {
loc := findLoc(info, path)
if loc != nil {
pathMap[i] = loc
}
switch d := i.(type) {
case *descriptor.FileDescriptorProto:
for index, descriptor := range d.MessageType {
addToPathMap(info, descriptor, newPath(path, 4, index))
}
for index, descriptor := range d.EnumType {
addToPathMap(info, descriptor, newPath(path, 5, index))
}
for index, descriptor := range d.Service {
addToPathMap(info, descriptor, newPath(path, 6, index))
}
case *descriptor.DescriptorProto:
for index, descriptor := range d.Field {
addToPathMap(info, descriptor, newPath(path, 2, index))
}
for index, descriptor := range d.NestedType {
addToPathMap(info, descriptor, newPath(path, 3, index))
}
for index, descriptor := range d.EnumType {
addToPathMap(info, descriptor, newPath(path, 4, index))
}
case *descriptor.EnumDescriptorProto:
for index, descriptor := range d.Value {
addToPathMap(info, descriptor, newPath(path, 2, index))
}
case *descriptor.ServiceDescriptorProto:
for index, descriptor := range d.Method {
addToPathMap(info, descriptor, newPath(path, 2, index))
}
}
}
|
go
|
{
"resource": ""
}
|
q6509
|
goTypeWithPackage
|
train
|
func goTypeWithPackage(f *descriptor.FieldDescriptorProto) string {
pkg := ""
if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE || *f.Type == descriptor.FieldDescriptorProto_TYPE_ENUM {
if isTimestampPackage(*f.TypeName) {
pkg = "timestamp"
} else {
pkg = getPackageTypeName(*f.TypeName)
}
}
return goType(pkg, f)
}
|
go
|
{
"resource": ""
}
|
q6510
|
goType
|
train
|
func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
if pkg != "" {
pkg = pkg + "."
}
switch *f.Type {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]float64"
}
return "float64"
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]float32"
}
return "float32"
case descriptor.FieldDescriptorProto_TYPE_INT64:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]int64"
}
return "int64"
case descriptor.FieldDescriptorProto_TYPE_UINT64:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]uint64"
}
return "uint64"
case descriptor.FieldDescriptorProto_TYPE_INT32:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]int32"
}
return "int32"
case descriptor.FieldDescriptorProto_TYPE_UINT32:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]uint32"
}
return "uint32"
case descriptor.FieldDescriptorProto_TYPE_BOOL:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]bool"
}
return "bool"
case descriptor.FieldDescriptorProto_TYPE_STRING:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]string"
}
return "string"
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return fmt.Sprintf("[]*%s%s", pkg, shortType(*f.TypeName))
}
return fmt.Sprintf("*%s%s", pkg, shortType(*f.TypeName))
case descriptor.FieldDescriptorProto_TYPE_BYTES:
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
return "[]byte"
}
return "byte"
case descriptor.FieldDescriptorProto_TYPE_ENUM:
return fmt.Sprintf("*%s%s", pkg, shortType(*f.TypeName))
default:
return "interface{}"
}
}
|
go
|
{
"resource": ""
}
|
q6511
|
lowerGoNormalize
|
train
|
func lowerGoNormalize(s string) string {
fmtd := xstrings.ToCamelCase(s)
fmtd = xstrings.FirstRuneToLower(fmtd)
return formatID(s, fmtd)
}
|
go
|
{
"resource": ""
}
|
q6512
|
goNormalize
|
train
|
func goNormalize(s string) string {
fmtd := xstrings.ToCamelCase(s)
return formatID(s, fmtd)
}
|
go
|
{
"resource": ""
}
|
q6513
|
formatID
|
train
|
func formatID(base string, formatted string) string {
if formatted == "" {
return formatted
}
switch {
case base == "id":
// id -> ID
return "ID"
case strings.HasPrefix(base, "id_"):
// id_some -> IDSome
return "ID" + formatted[2:]
case strings.HasSuffix(base, "_id"):
// some_id -> SomeID
return formatted[:len(formatted)-2] + "ID"
case strings.HasSuffix(base, "_ids"):
// some_ids -> SomeIDs
return formatted[:len(formatted)-3] + "IDs"
}
return formatted
}
|
go
|
{
"resource": ""
}
|
q6514
|
SetSizePolicy
|
train
|
func (w *WidgetBase) SetSizePolicy(h, v SizePolicy) {
w.sizePolicyX = h
w.sizePolicyY = v
}
|
go
|
{
"resource": ""
}
|
q6515
|
Draw
|
train
|
func (p *Progress) Draw(painter *Painter) {
painter.DrawRune(0, 0, '[')
painter.DrawRune(p.Size().X-1, 0, ']')
start := 1
end := p.Size().X - 1
curr := int((float64(p.current) / float64(p.max)) * float64(end-start))
for i := start; i < curr; i++ {
painter.DrawRune(i, 0, '=')
}
for i := curr + start; i < end; i++ {
painter.DrawRune(i, 0, '-')
}
painter.DrawRune(curr, 0, '>')
}
|
go
|
{
"resource": ""
}
|
q6516
|
NewPainter
|
train
|
func NewPainter(s Surface, p *Theme) *Painter {
return &Painter{
theme: p,
surface: s,
style: p.Style("normal"),
mask: image.Rectangle{
Min: image.ZP,
Max: s.Size(),
},
}
}
|
go
|
{
"resource": ""
}
|
q6517
|
Translate
|
train
|
func (p *Painter) Translate(x, y int) {
p.transforms = append(p.transforms, image.Point{x, y})
}
|
go
|
{
"resource": ""
}
|
q6518
|
Restore
|
train
|
func (p *Painter) Restore() {
if len(p.transforms) > 0 {
p.transforms = p.transforms[:len(p.transforms)-1]
}
}
|
go
|
{
"resource": ""
}
|
q6519
|
Repaint
|
train
|
func (p *Painter) Repaint(w Widget) {
p.mask = image.Rectangle{
Min: image.ZP,
Max: p.surface.Size(),
}
p.surface.HideCursor()
w.Resize(p.surface.Size())
p.Begin()
w.Draw(p)
p.End()
}
|
go
|
{
"resource": ""
}
|
q6520
|
DrawCursor
|
train
|
func (p *Painter) DrawCursor(x, y int) {
wp := p.mapLocalToWorld(image.Point{x, y})
p.surface.SetCursor(wp.X, wp.Y)
}
|
go
|
{
"resource": ""
}
|
q6521
|
DrawRune
|
train
|
func (p *Painter) DrawRune(x, y int, r rune) {
wp := p.mapLocalToWorld(image.Point{x, y})
if (p.mask.Min.X <= wp.X) && (wp.X < p.mask.Max.X) && (p.mask.Min.Y <= wp.Y) && (wp.Y < p.mask.Max.Y) {
p.surface.SetCell(wp.X, wp.Y, r, p.style)
}
}
|
go
|
{
"resource": ""
}
|
q6522
|
DrawText
|
train
|
func (p *Painter) DrawText(x, y int, text string) {
for _, r := range text {
p.DrawRune(x, y, r)
x += runeWidth(r)
}
}
|
go
|
{
"resource": ""
}
|
q6523
|
DrawHorizontalLine
|
train
|
func (p *Painter) DrawHorizontalLine(x1, x2, y int) {
for x := x1; x < x2; x++ {
p.DrawRune(x, y, '─')
}
}
|
go
|
{
"resource": ""
}
|
q6524
|
DrawVerticalLine
|
train
|
func (p *Painter) DrawVerticalLine(x, y1, y2 int) {
for y := y1; y < y2; y++ {
p.DrawRune(x, y, '│')
}
}
|
go
|
{
"resource": ""
}
|
q6525
|
DrawRect
|
train
|
func (p *Painter) DrawRect(x, y, w, h int) {
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
m := i + x
n := j + y
switch {
case i == 0 && j == 0:
p.DrawRune(m, n, '┌')
case i == w-1 && j == 0:
p.DrawRune(m, n, '┐')
case i == 0 && j == h-1:
p.DrawRune(m, n, '└')
case i == w-1 && j == h-1:
p.DrawRune(m, n, '┘')
case i == 0 || i == w-1:
p.DrawRune(m, n, '│')
case j == 0 || j == h-1:
p.DrawRune(m, n, '─')
}
}
}
}
|
go
|
{
"resource": ""
}
|
q6526
|
FillRect
|
train
|
func (p *Painter) FillRect(x, y, w, h int) {
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
p.DrawRune(i+x, j+y, ' ')
}
}
}
|
go
|
{
"resource": ""
}
|
q6527
|
WithStyle
|
train
|
func (p *Painter) WithStyle(n string, fn func(*Painter)) {
prev := p.style
new := prev.mergeIn(p.theme.Style(n))
p.SetStyle(new)
fn(p)
p.SetStyle(prev)
}
|
go
|
{
"resource": ""
}
|
q6528
|
WithMask
|
train
|
func (p *Painter) WithMask(r image.Rectangle, fn func(*Painter)) {
tmp := p.mask
defer func() { p.mask = tmp }()
p.mask = p.mask.Intersect(image.Rectangle{
Min: p.mapLocalToWorld(r.Min),
Max: p.mapLocalToWorld(r.Max),
})
fn(p)
}
|
go
|
{
"resource": ""
}
|
q6529
|
CursorPos
|
train
|
func (r *RuneBuffer) CursorPos() image.Point {
if r.width == 0 {
return image.ZP
}
sp := r.SplitByLine()
var x, y int
remaining := r.idx
for _, l := range sp {
if utf8.RuneCountInString(l) < remaining {
y++
remaining -= utf8.RuneCountInString(l) + 1
} else {
x = remaining
break
}
}
return image.Pt(stringWidth(string(r.buf[:x])), y)
}
|
go
|
{
"resource": ""
}
|
q6530
|
MoveForward
|
train
|
func (r *RuneBuffer) MoveForward() {
if r.idx == len(r.buf) {
return
}
r.idx++
}
|
go
|
{
"resource": ""
}
|
q6531
|
MoveToLineStart
|
train
|
func (r *RuneBuffer) MoveToLineStart() {
for i := r.idx; i > 0; i-- {
if r.buf[i-1] == '\n' {
r.idx = i
return
}
}
r.idx = 0
}
|
go
|
{
"resource": ""
}
|
q6532
|
MoveToLineEnd
|
train
|
func (r *RuneBuffer) MoveToLineEnd() {
for i := r.idx; i < len(r.buf)-1; i++ {
if r.buf[i] == '\n' {
r.idx = i
return
}
}
r.idx = len(r.buf)
}
|
go
|
{
"resource": ""
}
|
q6533
|
Backspace
|
train
|
func (r *RuneBuffer) Backspace() {
if r.idx == 0 {
return
}
r.idx--
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
}
|
go
|
{
"resource": ""
}
|
q6534
|
Delete
|
train
|
func (r *RuneBuffer) Delete() {
if r.idx == len(r.buf) {
return
}
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
}
|
go
|
{
"resource": ""
}
|
q6535
|
Kill
|
train
|
func (r *RuneBuffer) Kill() {
newlineIdx := strings.IndexRune(string(r.buf[r.idx:]), '\n')
if newlineIdx < 0 {
r.buf = r.buf[:r.idx]
} else {
r.buf = append(r.buf[:r.idx], r.buf[r.idx+newlineIdx+1:]...)
}
}
|
go
|
{
"resource": ""
}
|
q6536
|
WrapString
|
train
|
func WrapString(s string, width int) string {
if len(s) <= 1 {
return s
}
// Output buffer. Does not include the most recent word.
var buf bytes.Buffer
// Trailing word.
var word bytes.Buffer
var wordLen int
spaceLeft := width
var prev rune
for _, curr := range s {
if curr == rune('\n') {
// Received a newline.
if word.Len() > spaceLeft {
spaceLeft = width
buf.WriteRune(curr)
} else {
spaceLeft = width
}
word.WriteTo(&buf)
wordLen = 0
} else if unicode.IsSpace(prev) && !unicode.IsSpace(curr) {
// At the start of a new word.
// Does the last word fit on this line, or the next?
if wordLen > spaceLeft {
spaceLeft = width - wordLen
buf.WriteRune('\n')
} else {
spaceLeft -= wordLen
}
// fmt.Printf("42: writing %q with %d spaces remaining out of %d\n", word.String(), spaceLeft, width)
word.WriteTo(&buf)
wordLen = 0
}
word.WriteRune(curr)
wordLen += runewidth.RuneWidth(curr)
prev = curr
}
// Close out the final word.
if wordLen > spaceLeft {
buf.WriteRune('\n')
}
word.WriteTo(&buf)
return buf.String()
}
|
go
|
{
"resource": ""
}
|
q6537
|
Scroll
|
train
|
func (s *ScrollArea) Scroll(dx, dy int) {
s.topLeft.X += dx
s.topLeft.Y += dy
}
|
go
|
{
"resource": ""
}
|
q6538
|
ScrollToBottom
|
train
|
func (s *ScrollArea) ScrollToBottom() {
s.topLeft.Y = s.Widget.SizeHint().Y - s.Size().Y
}
|
go
|
{
"resource": ""
}
|
q6539
|
Draw
|
train
|
func (s *ScrollArea) Draw(p *Painter) {
p.Translate(-s.topLeft.X, -s.topLeft.Y)
defer p.Restore()
off := image.Point{s.topLeft.X, s.topLeft.Y}
p.WithMask(image.Rectangle{Min: off, Max: s.Size().Add(off)}, func(p *Painter) {
s.Widget.Draw(p)
})
}
|
go
|
{
"resource": ""
}
|
q6540
|
Resize
|
train
|
func (s *ScrollArea) Resize(size image.Point) {
s.Widget.Resize(s.Widget.SizeHint())
s.WidgetBase.Resize(size)
if s.autoscroll {
s.ScrollToBottom()
}
}
|
go
|
{
"resource": ""
}
|
q6541
|
FocusNext
|
train
|
func (c *SimpleFocusChain) FocusNext(current Widget) Widget {
for i, w := range c.widgets {
if w != current {
continue
}
if i < len(c.widgets)-1 {
return c.widgets[i+1]
}
return c.widgets[0]
}
return nil
}
|
go
|
{
"resource": ""
}
|
q6542
|
FocusDefault
|
train
|
func (c *SimpleFocusChain) FocusDefault() Widget {
if len(c.widgets) == 0 {
return nil
}
return c.widgets[0]
}
|
go
|
{
"resource": ""
}
|
q6543
|
Resize
|
train
|
func (l *Label) Resize(size image.Point) {
if l.Size() != size {
l.cacheSizeHint = nil
}
l.WidgetBase.Resize(size)
}
|
go
|
{
"resource": ""
}
|
q6544
|
Draw
|
train
|
func (l *Label) Draw(p *Painter) {
lines := l.lines()
style := "label"
if l.styleName != "" {
style += "." + l.styleName
}
p.WithStyle(style, func(p *Painter) {
for i, line := range lines {
p.DrawText(0, i, line)
}
})
}
|
go
|
{
"resource": ""
}
|
q6545
|
SizeHint
|
train
|
func (l *Label) SizeHint() image.Point {
if l.cacheSizeHint != nil {
return *l.cacheSizeHint
}
var max int
lines := l.lines()
for _, line := range lines {
if w := stringWidth(line); w > max {
max = w
}
}
sizeHint := image.Point{max, len(lines)}
l.cacheSizeHint = &sizeHint
return sizeHint
}
|
go
|
{
"resource": ""
}
|
q6546
|
SetText
|
train
|
func (l *Label) SetText(text string) {
l.cacheSizeHint = nil
l.text = text
}
|
go
|
{
"resource": ""
}
|
q6547
|
Append
|
train
|
func (b *Box) Append(w Widget) {
b.children = append(b.children, w)
}
|
go
|
{
"resource": ""
}
|
q6548
|
Prepend
|
train
|
func (b *Box) Prepend(w Widget) {
b.children = append([]Widget{w}, b.children...)
}
|
go
|
{
"resource": ""
}
|
q6549
|
Insert
|
train
|
func (b *Box) Insert(i int, w Widget) {
if len(b.children) < i || i < 0 {
return
}
b.children = append(b.children, nil)
copy(b.children[i+1:], b.children[i:])
b.children[i] = w
}
|
go
|
{
"resource": ""
}
|
q6550
|
Remove
|
train
|
func (b *Box) Remove(i int) {
if len(b.children) <= i || i < 0 {
return
}
b.children = append(b.children[:i], b.children[i+1:]...)
}
|
go
|
{
"resource": ""
}
|
q6551
|
IsFocused
|
train
|
func (b *Box) IsFocused() bool {
for _, w := range b.children {
if w.IsFocused() {
return true
}
}
return false
}
|
go
|
{
"resource": ""
}
|
q6552
|
Draw
|
train
|
func (b *Box) Draw(p *Painter) {
style := "box"
if b.IsFocused() {
style += ".focused"
}
p.WithStyle(style, func(p *Painter) {
sz := b.Size()
if b.border {
p.WithStyle(style+".border", func(p *Painter) {
p.DrawRect(0, 0, sz.X, sz.Y)
})
p.WithStyle(style, func(p *Painter) {
p.WithMask(image.Rect(0, 0, sz.X-1, 1), func(p *Painter) {
p.DrawText(1, 0, b.title)
})
})
p.FillRect(1, 1, sz.X-2, sz.Y-2)
p.Translate(1, 1)
defer p.Restore()
} else {
p.FillRect(0, 0, sz.X, sz.Y)
}
var off image.Point
for _, child := range b.children {
switch b.Alignment() {
case Horizontal:
p.Translate(off.X, 0)
case Vertical:
p.Translate(0, off.Y)
}
p.WithMask(image.Rectangle{
Min: image.ZP,
Max: child.Size(),
}, func(p *Painter) {
child.Draw(p)
})
p.Restore()
off = off.Add(child.Size())
}
})
}
|
go
|
{
"resource": ""
}
|
q6553
|
MinSizeHint
|
train
|
func (b *Box) MinSizeHint() image.Point {
var minSize image.Point
for _, child := range b.children {
size := child.MinSizeHint()
if b.Alignment() == Horizontal {
minSize.X += size.X
if size.Y > minSize.Y {
minSize.Y = size.Y
}
} else {
minSize.Y += size.Y
if size.X > minSize.X {
minSize.X = size.X
}
}
}
if b.border {
minSize = minSize.Add(image.Point{2, 2})
}
return minSize
}
|
go
|
{
"resource": ""
}
|
q6554
|
SizeHint
|
train
|
func (b *Box) SizeHint() image.Point {
var sizeHint image.Point
for _, child := range b.children {
size := child.SizeHint()
if b.Alignment() == Horizontal {
sizeHint.X += size.X
if size.Y > sizeHint.Y {
sizeHint.Y = size.Y
}
} else {
sizeHint.Y += size.Y
if size.X > sizeHint.X {
sizeHint.X = size.X
}
}
}
if b.border {
sizeHint = sizeHint.Add(image.Point{2, 2})
}
return sizeHint
}
|
go
|
{
"resource": ""
}
|
q6555
|
Resize
|
train
|
func (b *Box) Resize(size image.Point) {
b.size = size
inner := b.size
if b.border {
inner = b.size.Sub(image.Point{2, 2})
}
b.layoutChildren(inner)
}
|
go
|
{
"resource": ""
}
|
q6556
|
Name
|
train
|
func (ev *KeyEvent) Name() string {
s := ""
m := []string{}
if ev.Modifiers&ModShift != 0 {
m = append(m, "Shift")
}
if ev.Modifiers&ModAlt != 0 {
m = append(m, "Alt")
}
if ev.Modifiers&ModMeta != 0 {
m = append(m, "Meta")
}
if ev.Modifiers&ModCtrl != 0 {
m = append(m, "Ctrl")
}
ok := false
if s, ok = keyNames[ev.Key]; !ok {
if ev.Key == KeyRune {
s = string(ev.Rune)
} else {
s = "Unknown"
}
}
if len(m) != 0 {
if ev.Modifiers&ModCtrl != 0 && strings.HasPrefix(s, "Ctrl-") {
s = s[5:]
}
return fmt.Sprintf("%s+%s", strings.Join(m, "+"), s)
}
return s
}
|
go
|
{
"resource": ""
}
|
q6557
|
NewGrid
|
train
|
func NewGrid(cols, rows int) *Grid {
return &Grid{
cols: cols,
rows: rows,
cells: make(map[image.Point]Widget),
columnStretch: make(map[int]int),
rowStretch: make(map[int]int),
}
}
|
go
|
{
"resource": ""
}
|
q6558
|
Draw
|
train
|
func (g *Grid) Draw(p *Painter) {
s := g.Size()
if g.hasBorder {
border := 1
// Draw outmost border.
p.DrawRect(0, 0, s.X, s.Y)
// Draw column dividers.
var coloff int
for i := 0; i < g.cols-1; i++ {
x := g.colWidths[i] + coloff + border
p.DrawVerticalLine(x, 0, s.Y-1)
p.DrawRune(x, 0, '┬')
p.DrawRune(x, s.Y-1, '┴')
coloff = x
}
// Draw row dividers.
var rowoff int
for j := 0; j < g.rows-1; j++ {
y := g.rowHeights[j] + rowoff + border
p.DrawHorizontalLine(0, s.X-1, y)
p.DrawRune(0, y, '├')
p.DrawRune(s.X-1, y, '┤')
rowoff = y
}
// Polish the intersections.
rowoff = 0
for j := 0; j < g.rows-1; j++ {
y := g.rowHeights[j] + rowoff + border
coloff = 0
for i := 0; i < g.cols-1; i++ {
x := g.colWidths[i] + coloff + border
p.DrawRune(x, y, '┼')
coloff = x
}
rowoff = y
}
}
// Draw cell content.
for i := 0; i < g.cols; i++ {
for j := 0; j < g.rows; j++ {
pos := image.Point{i, j}
wp := g.mapCellToLocal(pos)
if w, ok := g.cells[pos]; ok {
p.Translate(wp.X, wp.Y)
p.WithMask(image.Rectangle{
Min: image.ZP,
Max: w.Size(),
}, func(p *Painter) {
w.Draw(p)
})
p.Restore()
}
}
}
}
|
go
|
{
"resource": ""
}
|
q6559
|
MinSizeHint
|
train
|
func (g *Grid) MinSizeHint() image.Point {
if g.cols == 0 || g.rows == 0 {
return image.Point{}
}
var width int
for i := 0; i < g.cols; i++ {
width += g.minColumnWidth(i)
}
var height int
for j := 0; j < g.rows; j++ {
height += g.minRowHeight(j)
}
if g.hasBorder {
width += g.cols + 1
height += g.rows + 1
}
return image.Point{width, height}
}
|
go
|
{
"resource": ""
}
|
q6560
|
SizeHint
|
train
|
func (g *Grid) SizeHint() image.Point {
if g.cols == 0 || g.rows == 0 {
return image.Point{}
}
var maxWidth int
for i := 0; i < g.cols; i++ {
if w := g.columnWidth(i); w > maxWidth {
maxWidth = w
}
}
var maxHeight int
for j := 0; j < g.rows; j++ {
if h := g.rowHeight(j); h > maxHeight {
maxHeight = h
}
}
width := maxWidth * g.cols
height := maxHeight * g.rows
if g.hasBorder {
width += g.cols + 1
height += g.rows + 1
}
return image.Point{width, height}
}
|
go
|
{
"resource": ""
}
|
q6561
|
Resize
|
train
|
func (g *Grid) Resize(size image.Point) {
g.size = size
inner := g.size
if g.hasBorder {
inner.X = g.size.X - (g.cols + 1)
inner.Y = g.size.Y - (g.rows + 1)
}
g.layoutChildren(inner)
}
|
go
|
{
"resource": ""
}
|
q6562
|
SetCell
|
train
|
func (g *Grid) SetCell(pos image.Point, w Widget) {
g.cells[pos] = w
}
|
go
|
{
"resource": ""
}
|
q6563
|
AppendRow
|
train
|
func (g *Grid) AppendRow(row ...Widget) {
g.rows++
if len(row) > g.cols {
g.cols = len(row)
}
for i, cell := range row {
pos := image.Point{i, g.rows - 1}
g.SetCell(pos, cell)
}
}
|
go
|
{
"resource": ""
}
|
q6564
|
RemoveRows
|
train
|
func (g *Grid) RemoveRows() {
g.rows = 0
g.cells = make(map[image.Point]Widget)
}
|
go
|
{
"resource": ""
}
|
q6565
|
SetRowStretch
|
train
|
func (g *Grid) SetRowStretch(row, stretch int) {
g.rowStretch[row] = stretch
}
|
go
|
{
"resource": ""
}
|
q6566
|
Quit
|
train
|
func (ui *tcellUI) Quit() {
logger.Printf("Quitting")
ui.screen.Fini()
ui.quit <- struct{}{}
}
|
go
|
{
"resource": ""
}
|
q6567
|
trimRightLen
|
train
|
func trimRightLen(s string, n int) string {
if n <= 0 {
return s
}
c := utf8.RuneCountInString(s)
runeCount := 0
var i int
for i = range s {
if runeCount >= c-n {
break
}
runeCount++
}
return s[:i]
}
|
go
|
{
"resource": ""
}
|
q6568
|
Draw
|
train
|
func (b *StatusBar) Draw(p *Painter) {
p.WithStyle("statusbar", func(p *Painter) {
p.FillRect(0, 0, b.Size().X, 1)
p.DrawText(0, 0, b.text)
p.DrawText(b.Size().X-stringWidth(b.permText), 0, b.permText)
})
}
|
go
|
{
"resource": ""
}
|
q6569
|
Draw
|
train
|
func (s *StyledBox) Draw(p *tui.Painter) {
p.WithStyle(s.Style, func(p *tui.Painter) {
s.Box.Draw(p)
})
}
|
go
|
{
"resource": ""
}
|
q6570
|
SizeHint
|
train
|
func (e *TextEdit) SizeHint() image.Point {
var max int
lines := strings.Split(e.text.String(), "\n")
for _, line := range lines {
if w := stringWidth(line); w > max {
max = w
}
}
return image.Point{max, e.text.heightForWidth(max)}
}
|
go
|
{
"resource": ""
}
|
q6571
|
NewPadder
|
train
|
func NewPadder(x, y int, w Widget) *Padder {
return &Padder{
widget: w,
padding: image.Point{x, y},
}
}
|
go
|
{
"resource": ""
}
|
q6572
|
Draw
|
train
|
func (p *Padder) Draw(painter *Painter) {
painter.Translate(p.padding.X, p.padding.Y)
defer painter.Restore()
p.widget.Draw(painter)
}
|
go
|
{
"resource": ""
}
|
q6573
|
Size
|
train
|
func (p *Padder) Size() image.Point {
return p.widget.Size().Add(p.padding.Mul(2))
}
|
go
|
{
"resource": ""
}
|
q6574
|
Resize
|
train
|
func (p *Padder) Resize(size image.Point) {
p.widget.Resize(size.Sub(p.padding.Mul(2)))
}
|
go
|
{
"resource": ""
}
|
q6575
|
Draw
|
train
|
func (b *Button) Draw(p *Painter) {
style := "button"
if b.IsFocused() {
style += ".focused"
}
p.WithStyle(style, func(p *Painter) {
lines := strings.Split(b.text, "\n")
for i, line := range lines {
p.FillRect(0, i, b.Size().X, 1)
p.DrawText(0, i, line)
}
})
}
|
go
|
{
"resource": ""
}
|
q6576
|
SizeHint
|
train
|
func (b *Button) SizeHint() image.Point {
if len(b.text) == 0 {
return b.MinSizeHint()
}
var size image.Point
lines := strings.Split(b.text, "\n")
for _, line := range lines {
if w := stringWidth(line); w > size.X {
size.X = w
}
}
size.Y = len(lines)
return size
}
|
go
|
{
"resource": ""
}
|
q6577
|
OnKeyEvent
|
train
|
func (b *Button) OnKeyEvent(ev KeyEvent) {
if !b.IsFocused() {
return
}
if ev.Key == KeyEnter && b.onActivated != nil {
b.onActivated(b)
}
}
|
go
|
{
"resource": ""
}
|
q6578
|
mergeIn
|
train
|
func (s Style) mergeIn(delta Style) Style {
result := s
if delta.Fg != ColorDefault {
result.Fg = delta.Fg
}
if delta.Bg != ColorDefault {
result.Bg = delta.Bg
}
if delta.Reverse != DecorationInherit {
result.Reverse = delta.Reverse
}
if delta.Bold != DecorationInherit {
result.Bold = delta.Bold
}
if delta.Underline != DecorationInherit {
result.Underline = delta.Underline
}
return result
}
|
go
|
{
"resource": ""
}
|
q6579
|
SetStyle
|
train
|
func (p *Theme) SetStyle(n string, i Style) {
p.styles[n] = i
}
|
go
|
{
"resource": ""
}
|
q6580
|
HasStyle
|
train
|
func (p *Theme) HasStyle(name string) bool {
_, ok := p.styles[name]
return ok
}
|
go
|
{
"resource": ""
}
|
q6581
|
NewTable
|
train
|
func NewTable(cols, rows int) *Table {
return &Table{
Grid: NewGrid(cols, rows),
}
}
|
go
|
{
"resource": ""
}
|
q6582
|
RemoveRow
|
train
|
func (t *Table) RemoveRow(index int) {
t.Grid.RemoveRow(index)
if t.selected == index {
t.selected = -1
} else if t.selected > index {
t.selected--
}
}
|
go
|
{
"resource": ""
}
|
q6583
|
Draw
|
train
|
func (l *List) Draw(p *Painter) {
for i, item := range l.items {
style := "list.item"
if i == l.selected-l.pos {
style += ".selected"
}
p.WithStyle(style, func(p *Painter) {
p.FillRect(0, i, l.Size().X, 1)
p.DrawText(0, i, item)
})
}
}
|
go
|
{
"resource": ""
}
|
q6584
|
SizeHint
|
train
|
func (l *List) SizeHint() image.Point {
var width int
for _, item := range l.items {
if w := stringWidth(item); w > width {
width = w
}
}
return image.Point{width, len(l.items)}
}
|
go
|
{
"resource": ""
}
|
q6585
|
OnKeyEvent
|
train
|
func (l *List) OnKeyEvent(ev KeyEvent) {
if !l.IsFocused() {
return
}
switch ev.Key {
case KeyUp:
l.moveUp()
case KeyDown:
l.moveDown()
case KeyEnter:
if l.onItemActivated != nil {
l.onItemActivated(l)
}
}
switch ev.Rune {
case 'k':
l.moveUp()
case 'j':
l.moveDown()
}
}
|
go
|
{
"resource": ""
}
|
q6586
|
AddItems
|
train
|
func (l *List) AddItems(items ...string) {
l.items = append(l.items, items...)
}
|
go
|
{
"resource": ""
}
|
q6587
|
RemoveItems
|
train
|
func (l *List) RemoveItems() {
l.items = []string{}
l.pos = 0
l.selected = -1
if l.onSelectionChanged != nil {
l.onSelectionChanged(l)
}
}
|
go
|
{
"resource": ""
}
|
q6588
|
RemoveItem
|
train
|
func (l *List) RemoveItem(i int) {
// Adjust pos and selected before removing.
if l.pos >= len(l.items) {
l.pos--
}
if l.selected == i {
l.selected = -1
} else if l.selected > i {
l.selected--
}
// Copy items following i to position i.
copy(l.items[i:], l.items[i+1:])
// Shrink items by one.
l.items[len(l.items)-1] = ""
l.items = l.items[:len(l.items)-1]
if l.onSelectionChanged != nil {
l.onSelectionChanged(l)
}
}
|
go
|
{
"resource": ""
}
|
q6589
|
unquote
|
train
|
func unquote(quoted string) (s string, triple bool, err error) {
// Check for raw prefix: means don't interpret the inner \.
raw := false
if strings.HasPrefix(quoted, "r") {
raw = true
quoted = quoted[1:]
}
if len(quoted) < 2 {
err = fmt.Errorf("string literal too short")
return
}
if quoted[0] != '"' && quoted[0] != '\'' || quoted[0] != quoted[len(quoted)-1] {
err = fmt.Errorf("string literal has invalid quotes")
return
}
// Check for triple quoted string.
quote := quoted[0]
if len(quoted) >= 6 && quoted[1] == quote && quoted[2] == quote && quoted[:3] == quoted[len(quoted)-3:] {
triple = true
quoted = quoted[3 : len(quoted)-3]
} else {
quoted = quoted[1 : len(quoted)-1]
}
// Now quoted is the quoted data, but no quotes.
// If we're in raw mode or there are no escapes or
// carriage returns, we're done.
var unquoteChars string
if raw {
unquoteChars = "\r"
} else {
unquoteChars = "\\\r"
}
if !strings.ContainsAny(quoted, unquoteChars) {
s = quoted
return
}
// Otherwise process quoted string.
// Each iteration processes one escape sequence along with the
// plain text leading up to it.
var buf bytes.Buffer
for {
// Remove prefix before escape sequence.
i := strings.IndexAny(quoted, unquoteChars)
if i < 0 {
i = len(quoted)
}
buf.WriteString(quoted[:i])
quoted = quoted[i:]
if len(quoted) == 0 {
break
}
// Process carriage return.
if quoted[0] == '\r' {
buf.WriteByte('\n')
if len(quoted) > 1 && quoted[1] == '\n' {
quoted = quoted[2:]
} else {
quoted = quoted[1:]
}
continue
}
// Process escape sequence.
if len(quoted) == 1 {
err = fmt.Errorf(`truncated escape sequence \`)
return
}
switch quoted[1] {
default:
// In Python, if \z (for some byte z) is not a known escape sequence
// then it appears as literal text in the string.
buf.WriteString(quoted[:2])
quoted = quoted[2:]
case '\n':
// Ignore the escape and the line break.
quoted = quoted[2:]
case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', '\'', '"':
// One-char escape
buf.WriteByte(unesc[quoted[1]])
quoted = quoted[2:]
case '0', '1', '2', '3', '4', '5', '6', '7':
// Octal escape, up to 3 digits.
n := int(quoted[1] - '0')
quoted = quoted[2:]
for i := 1; i < 3; i++ {
if len(quoted) == 0 || quoted[0] < '0' || '7' < quoted[0] {
break
}
n = n*8 + int(quoted[0]-'0')
quoted = quoted[1:]
}
if n >= 256 {
// NOTE: Python silently discards the high bit,
// so that '\541' == '\141' == 'a'.
// Let's see if we can avoid doing that in BUILD files.
err = fmt.Errorf(`invalid escape sequence \%03o`, n)
return
}
buf.WriteByte(byte(n))
case 'x':
// Hexadecimal escape, exactly 2 digits.
if len(quoted) < 4 {
err = fmt.Errorf(`truncated escape sequence %s`, quoted)
return
}
n, err1 := strconv.ParseInt(quoted[2:4], 16, 0)
if err1 != nil {
err = fmt.Errorf(`invalid escape sequence %s`, quoted[:4])
return
}
buf.WriteByte(byte(n))
quoted = quoted[4:]
}
}
s = buf.String()
return
}
|
go
|
{
"resource": ""
}
|
q6590
|
indexByte
|
train
|
func indexByte(s string, b byte) int {
for i := 0; i < len(s); i++ {
if s[i] == b {
return i
}
}
return -1
}
|
go
|
{
"resource": ""
}
|
q6591
|
quote
|
train
|
func quote(unquoted string, triple bool) string {
q := `"`
if triple {
q = `"""`
}
var buf bytes.Buffer
buf.WriteString(q)
for i := 0; i < len(unquoted); i++ {
c := unquoted[i]
if c == '"' && triple && (i+1 < len(unquoted) && unquoted[i+1] != '"' || i+2 < len(unquoted) && unquoted[i+2] != '"') {
// Can pass up to two quotes through, because they are followed by a non-quote byte.
buf.WriteByte(c)
if i+1 < len(unquoted) && unquoted[i+1] == '"' {
buf.WriteByte(c)
i++
}
continue
}
if triple && c == '\n' {
// Can allow newline in triple-quoted string.
buf.WriteByte(c)
continue
}
if c == '\'' {
// Can allow ' since we always use ".
buf.WriteByte(c)
continue
}
if c == '\\' {
if i+1 < len(unquoted) && indexByte(notEsc, unquoted[i+1]) >= 0 {
// Can pass \ through when followed by a byte that
// known not to be a valid escape sequence and also
// that does not trigger an escape sequence of its own.
// Use this, because various BUILD files do.
buf.WriteByte('\\')
buf.WriteByte(unquoted[i+1])
i++
continue
}
}
if esc[c] != 0 {
buf.WriteByte('\\')
buf.WriteByte(esc[c])
continue
}
if c < 0x20 || c >= 0x80 {
// BUILD files are supposed to be Latin-1, so escape all control and high bytes.
// I'd prefer to use \x here, but Blaze does not implement
// \x in quoted strings (b/7272572).
buf.WriteByte('\\')
buf.WriteByte(hex[c>>6]) // actually octal but reusing hex digits 0-7.
buf.WriteByte(hex[(c>>3)&7])
buf.WriteByte(hex[c&7])
/*
buf.WriteByte('\\')
buf.WriteByte('x')
buf.WriteByte(hex[c>>4])
buf.WriteByte(hex[c&0xF])
*/
continue
}
buf.WriteByte(c)
continue
}
buf.WriteString(q)
return buf.String()
}
|
go
|
{
"resource": ""
}
|
q6592
|
AllocComments
|
train
|
func (cr *commentsRef) AllocComments() {
if cr.ref == nil {
cr.ref = new(Comments)
}
}
|
go
|
{
"resource": ""
}
|
q6593
|
AsFloat
|
train
|
func AsFloat(x Value) (f float64, ok bool) {
switch x := x.(type) {
case Float:
return float64(x), true
case Int:
return float64(x.Float()), true
}
return 0, false
}
|
go
|
{
"resource": ""
}
|
q6594
|
Globals
|
train
|
func (fn *Function) Globals() StringDict {
m := make(StringDict, len(fn.funcode.Prog.Globals))
for i, id := range fn.funcode.Prog.Globals {
if v := fn.globals[i]; v != nil {
m[id.Name] = v
}
}
return m
}
|
go
|
{
"resource": ""
}
|
q6595
|
NewBuiltin
|
train
|
func NewBuiltin(name string, fn func(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, error)) *Builtin {
return &Builtin{name: name, fn: fn}
}
|
go
|
{
"resource": ""
}
|
q6596
|
checkMutable
|
train
|
func (l *List) checkMutable(verb string, structural bool) error {
if l.frozen {
return fmt.Errorf("cannot %s frozen list", verb)
}
if structural && l.itercount > 0 {
return fmt.Errorf("cannot %s list during iteration", verb)
}
return nil
}
|
go
|
{
"resource": ""
}
|
q6597
|
Equal
|
train
|
func Equal(x, y Value) (bool, error) {
if x, ok := x.(String); ok {
return x == y, nil // fast path for an important special case
}
return EqualDepth(x, y, maxdepth)
}
|
go
|
{
"resource": ""
}
|
q6598
|
EqualDepth
|
train
|
func EqualDepth(x, y Value, depth int) (bool, error) {
return CompareDepth(syntax.EQL, x, y, depth)
}
|
go
|
{
"resource": ""
}
|
q6599
|
Compare
|
train
|
func Compare(op syntax.Token, x, y Value) (bool, error) {
return CompareDepth(op, x, y, maxdepth)
}
|
go
|
{
"resource": ""
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.