Skip to content

Commit

Permalink
add block widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Mar 27, 2024
1 parent 21d6a8e commit 636f3b2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
27 changes: 17 additions & 10 deletions pkg/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ type Widget struct {
gasAvgBlock *text.Text
gasAvgTransaction *text.Text
latestGas *text.Text
transaction *text.Text
block *text.Text
transactions *text.Text
blocks *text.Text
height *text.Text
blockProgress *donut.Donut
}

Expand Down Expand Up @@ -138,18 +139,23 @@ func DrawDashboard() (widget *Widget, err error) {
}

// Transaction parsing widget
if widget.transaction, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
if widget.transactions, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.transaction.Write("Transactions will appear as soon as they are confirmed in a block."); err != nil {
if err := widget.transactions.Write("Transactions will appear as soon as they are confirmed in a height."); err != nil {
return widget, err
}

// Blocks parsing widget
if widget.blocks, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}

// Create Blocks parsing widget
if widget.block, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
if widget.height, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.block.Write("⌛ loading"); err != nil {
if err := widget.height.Write("⌛ loading"); err != nil {
return widget, err
}

Expand Down Expand Up @@ -210,7 +216,7 @@ func (w *Widget) drawView() (err error) {
container.Left(
container.Border(linestyle.Light),
container.BorderTitle("Latest Block"),
container.PlaceWidget(w.block),
container.PlaceWidget(w.height),
),
container.Right(
container.Border(linestyle.Light),
Expand Down Expand Up @@ -281,13 +287,15 @@ func (w *Widget) drawView() (err error) {
),
),
container.Bottom(
// empty
container.Border(linestyle.Light),
container.BorderTitle("Latest Blocks"),
container.PlaceWidget(w.blocks),
),
),
), container.Right(
container.Border(linestyle.Light),
container.BorderTitle("Latest Confirmed Transactions"),
container.PlaceWidget(w.transaction),
container.PlaceWidget(w.transactions),
),
),
),
Expand All @@ -309,7 +317,6 @@ func (w *Widget) Run(ctx context.Context) error {
quitter := func(k *terminalapi.Keyboard) {
if k.Key == 'q' || k.Key == 'Q' || k.Key == keyboard.KeyEsc {
cancel()
w.Cleanup()
}
}
return termdash.Run(ctx, w.terminal, w.container, termdash.KeyboardSubscriber(quitter))
Expand Down
14 changes: 9 additions & 5 deletions pkg/view/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,24 @@ func (w *Widget) SetLatestGas(txt string, opts ...text.WriteOption) error {
return w.latestGas.Write(txt, opts...)
}

func (w *Widget) SetBlock(height int64, opts ...text.WriteOption) error {
w.block.Reset()
return w.block.Write(number.WithComma(height), opts...)
func (w *Widget) SetHeight(height int64, opts ...text.WriteOption) error {
w.height.Reset()
return w.height.Write(number.WithComma(height), opts...)
}

func (w *Widget) AddTransaction(txt string, opts ...text.WriteOption) error {
now := time.Now().Format("2006-01-02 03:04:05 PM")
if err := w.transaction.Write(
if err := w.transactions.Write(
fmt.Sprintf("\n\nNew Transaction (%s)\n", now),
text.WriteCellOpts(cell.Bold(), cell.Inverse()),
); err != nil {
return err
}
return w.transaction.Write(txt, opts...)
return w.transactions.Write(txt, opts...)
}

func (w *Widget) AddBlock(txt string, opts ...text.WriteOption) error {
return w.blocks.Write(txt+"\n", opts...)
}

func (w *Widget) SetBlockProgress(percent int, opts ...donut.Option) error {
Expand Down
14 changes: 13 additions & 1 deletion services/explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,22 @@ func Run(ctx context.Context, host string) error {
})()

cancel, err := c.NewBlock(ctx, func(block types.EventDataNewBlock) error {
if err := v.SetHeight(block.Block.Height); err != nil {
return err
}

info.Lock()
info.blocks++
info.Unlock()
return v.SetBlock(block.Block.Height)

return v.AddBlock(
fmt.Sprintf(
"%d %s (%d txs)",
block.Block.Height,
block.Block.Header.Hash(),
block.Block.Txs.Len(),
),
)
})
defer cancel()
if err != nil {
Expand Down

0 comments on commit 636f3b2

Please sign in to comment.