Skip to content

Commit

Permalink
Add text widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Sep 12, 2020
1 parent f774434 commit b8f8d80
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
27 changes: 15 additions & 12 deletions gui/gui.go
Expand Up @@ -34,7 +34,7 @@ func Run() error {
return fmt.Errorf("failed to generate container: %w", err)
}

w, err := newWidgets(ctx, c)
w, err := newWidgets()
if err != nil {
return fmt.Errorf("failed to generate widgets: %w", err)
}
Expand All @@ -50,7 +50,7 @@ func Run() error {
switch k.Key {
case keyboard.KeyEsc, keyboard.KeyCtrlC:
cancel()
case keyboard.KeyEnter:
case keyboard.KeySpace:
resultCh := make(chan *attacker.Result)
go func() {
// TODO: Enalble to poplulate from input
Expand All @@ -65,19 +65,22 @@ func Run() error {
}

func gridLayout(w *widgets) ([]container.Option, error) {
rows := []grid.Element{
grid.RowHeightPerc(99,
grid.Widget(w.plotChart,
container.Border(linestyle.Light),
container.BorderTitle("Plot"),
),
),
}
col := grid.ColWidthPerc(99, rows...)
raw1 := grid.RowHeightPerc(50,
grid.ColWidthPerc(99, grid.Widget(w.plotChart, container.Border(linestyle.Light), container.BorderTitle("Plot"))),
)
raw2 := grid.RowHeightPerc(47,
grid.ColWidthPerc(50, grid.Widget(w.urlInput, container.Border(linestyle.Light), container.BorderTitle("Input"))),
grid.ColWidthPerc(49, grid.Widget(w.metrics, container.Border(linestyle.Light), container.BorderTitle("Metrics"))),
)
raw3 := grid.RowHeightPerc(2,
grid.ColWidthPerc(99, grid.Widget(w.navi, container.Border(linestyle.Light))),
)

builder := grid.New()
builder.Add(
col,
raw1,
raw2,
raw3,
)

return builder.Build()
Expand Down
65 changes: 55 additions & 10 deletions gui/widgets.go
Expand Up @@ -6,9 +6,8 @@ import (

"github.com/k0kubun/pp"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/widgets/button"
"github.com/mum4k/termdash/widgets/linechart"
"github.com/mum4k/termdash/widgets/text"
"github.com/mum4k/termdash/widgets/textinput"

"github.com/nakabonne/ali/attacker"
Expand All @@ -20,21 +19,35 @@ const (
)

type widgets struct {
URLInput *textinput.TextInput
attackButton *button.Button
plotChart *linechart.LineChart
urlInput *textinput.TextInput
plotChart *linechart.LineChart
metrics *text.Text
navi *text.Text
}

func newWidgets(ctx context.Context, c *container.Container) (*widgets, error) {
l, err := newLineChart()
func newWidgets() (*widgets, error) {
lc, err := newLineChart()
if err != nil {
return nil, err
}
rt, err := newRollText()
if err != nil {
return nil, err
}
wt, err := newWrapText("Ctrl-c: quit, Space: attack")
if err != nil {
return nil, err
}
ti, err := newTextInput()
if err != nil {
return nil, err
}

return &widgets{
URLInput: nil,
attackButton: nil,
plotChart: l,
urlInput: ti,
plotChart: lc,
metrics: rt,
navi: wt,
}, nil
}

Expand All @@ -46,6 +59,38 @@ func newLineChart() (*linechart.LineChart, error) {
)
}

func newRollText() (*text.Text, error) {
return text.New(text.RollContent())
}

func newWrapText(s string) (*text.Text, error) {
t, err := text.New(text.WrapAtWords())
if err != nil {
return nil, err
}
if err := t.Write(s); err != nil {
return nil, err
}
return t, nil
}

func newTextInput() (*textinput.TextInput, error) {
input, err := textinput.New(
textinput.Label("Target URL: ", cell.FgColor(cell.ColorBlue)),
textinput.MaxWidthCells(20),
textinput.PlaceHolder("enter any text"),
textinput.OnSubmit(func(text string) error {
// TODO: Handle on submit action, for instance using channel.
pp.Println("input text", text)
return nil
}),
)
if err != nil {
return nil, err
}
return input, err
}

func redrawChart(ctx context.Context, lineChart *linechart.LineChart, resultCh chan *attacker.Result) {
values := []float64{}
for {
Expand Down

0 comments on commit b8f8d80

Please sign in to comment.