mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Output from 'dep status': PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED cloud.google.com/go v0.23.0 v0.23.0 0fd7230 v0.23.0 1 github.com/briandowns/openweathermap ^0.11.0 0.11 1b87579 0.11 1 github.com/gdamore/encoding branch master branch master b23993c b23993c 1 github.com/gdamore/tcell ^1.0.0 v1.0.0 061d51a v1.0.0 2 github.com/go-test/deep ^1.0.1 v1.0.1 6592d9c v1.0.1 1 github.com/golang/protobuf v1.1.0 v1.1.0 b4deda0 v1.1.0 1 github.com/google/go-github branch master branch master 2ae5df7 2ae5df7 1 github.com/google/go-querystring branch master branch master 53e6ce1 53e6ce1 1 github.com/jessevdk/go-flags ^1.4.0 v1.4.0 c6ca198 v1.4.0 1 github.com/lucasb-eyer/go-colorful v1.0 v1.0 345fbb3 v1.0 1 github.com/mattn/go-runewidth v0.0.2 v0.0.2 9e777a8 v0.0.2 1 github.com/olebedev/config branch master branch master 9a10d05 9a10d05 1 github.com/radovskyb/watcher ^1.0.2 v1.0.2 6145e14 v1.0.2 1 github.com/rivo/tview branch master branch master 71ecf1f 71ecf1f 1 github.com/yfronto/newrelic branch master branch master f7fa0c6 f7fa0c6 1 golang.org/x/net branch master branch master 1e49130 1e49130 2 golang.org/x/oauth2 branch master branch master 1e0a3fa 1e0a3fa 5 golang.org/x/text v0.3.0 v0.3.0 f21a4df v0.3.0 5 google.golang.org/api branch master branch master 00e3bb8 00e3bb8 4 google.golang.org/appengine v1.0.0 v1.0.0 150dc57 v1.0.0 10 gopkg.in/yaml.v2 ^2.2.1 v2.2.1 5420a8b v2.2.1 1 See https://golang.github.io/dep/docs/daily-dep.html
132 lines
3.6 KiB
Go
132 lines
3.6 KiB
Go
package tview
|
|
|
|
import (
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
// Modal is a centered message window used to inform the user or prompt them
|
|
// for an immediate decision. It needs to have at least one button (added via
|
|
// AddButtons()) or it will never disappear.
|
|
//
|
|
// See https://github.com/rivo/tview/wiki/Modal for an example.
|
|
type Modal struct {
|
|
*Box
|
|
|
|
// The framed embedded in the modal.
|
|
frame *Frame
|
|
|
|
// The form embedded in the modal's frame.
|
|
form *Form
|
|
|
|
// The message text (original, not word-wrapped).
|
|
text string
|
|
|
|
// The text color.
|
|
textColor tcell.Color
|
|
|
|
// The optional callback for when the user clicked one of the buttons. It
|
|
// receives the index of the clicked button and the button's label.
|
|
done func(buttonIndex int, buttonLabel string)
|
|
}
|
|
|
|
// NewModal returns a new modal message window.
|
|
func NewModal() *Modal {
|
|
m := &Modal{
|
|
Box: NewBox(),
|
|
textColor: Styles.PrimaryTextColor,
|
|
}
|
|
m.form = NewForm().
|
|
SetButtonsAlign(AlignCenter).
|
|
SetButtonBackgroundColor(Styles.PrimitiveBackgroundColor).
|
|
SetButtonTextColor(Styles.PrimaryTextColor)
|
|
m.form.SetBackgroundColor(Styles.ContrastBackgroundColor).SetBorderPadding(0, 0, 0, 0)
|
|
m.frame = NewFrame(m.form).SetBorders(0, 0, 1, 0, 0, 0)
|
|
m.frame.SetBorder(true).
|
|
SetBackgroundColor(Styles.ContrastBackgroundColor).
|
|
SetBorderPadding(1, 1, 1, 1)
|
|
m.focus = m
|
|
return m
|
|
}
|
|
|
|
// SetTextColor sets the color of the message text.
|
|
func (m *Modal) SetTextColor(color tcell.Color) *Modal {
|
|
m.textColor = color
|
|
return m
|
|
}
|
|
|
|
// SetDoneFunc sets a handler which is called when one of the buttons was
|
|
// pressed. It receives the index of the button as well as its label text. The
|
|
// handler is also called when the user presses the Escape key. The index will
|
|
// then be negative and the label text an emptry string.
|
|
func (m *Modal) SetDoneFunc(handler func(buttonIndex int, buttonLabel string)) *Modal {
|
|
m.done = handler
|
|
return m
|
|
}
|
|
|
|
// SetText sets the message text of the window. The text may contain line
|
|
// breaks. Note that words are wrapped, too, based on the final size of the
|
|
// window.
|
|
func (m *Modal) SetText(text string) *Modal {
|
|
m.text = text
|
|
return m
|
|
}
|
|
|
|
// AddButtons adds buttons to the window. There must be at least one button and
|
|
// a "done" handler so the window can be closed again.
|
|
func (m *Modal) AddButtons(labels []string) *Modal {
|
|
for index, label := range labels {
|
|
func(i int, l string) {
|
|
m.form.AddButton(label, func() {
|
|
if m.done != nil {
|
|
m.done(i, l)
|
|
}
|
|
})
|
|
}(index, label)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Focus is called when this primitive receives focus.
|
|
func (m *Modal) Focus(delegate func(p Primitive)) {
|
|
delegate(m.form)
|
|
}
|
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
func (m *Modal) HasFocus() bool {
|
|
return m.form.HasFocus()
|
|
}
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
func (m *Modal) Draw(screen tcell.Screen) {
|
|
// Calculate the width of this modal.
|
|
buttonsWidth := 0
|
|
for _, button := range m.form.buttons {
|
|
buttonsWidth += StringWidth(button.label) + 4 + 2
|
|
}
|
|
buttonsWidth -= 2
|
|
screenWidth, screenHeight := screen.Size()
|
|
width := screenWidth / 3
|
|
if width < buttonsWidth {
|
|
width = buttonsWidth
|
|
}
|
|
// width is now without the box border.
|
|
|
|
// Reset the text and find out how wide it is.
|
|
m.frame.Clear()
|
|
lines := WordWrap(m.text, width)
|
|
for _, line := range lines {
|
|
m.frame.AddText(line, true, AlignCenter, m.textColor)
|
|
}
|
|
|
|
// Set the modal's position and size.
|
|
height := len(lines) + 6
|
|
width += 4
|
|
x := (screenWidth - width) / 2
|
|
y := (screenHeight - height) / 2
|
|
m.SetRect(x, y, width, height)
|
|
|
|
// Draw the frame.
|
|
m.frame.SetRect(x, y, width, height)
|
|
m.frame.Draw(screen)
|
|
}
|