Skip to content

Commit

Permalink
Refactored code. Added metatable, modified table.go to keep track of …
Browse files Browse the repository at this point in the history
…error imdex, more readme information, no more searching with string, no more "reading from the table (?), and other cleanup.
  • Loading branch information
sokosam committed Nov 19, 2024
1 parent c6681c9 commit 7861322
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 156 deletions.
Submodule vehicle_control_system deleted from 4fddb9
Submodule vehicle_control_system deleted from a04e3a
53 changes: 46 additions & 7 deletions scripts/can_errgo/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
# To test the virtual can and go script
# CAN Error TUI

This is a Tangible User Interface program designed to track CAN Errors including their count, recency, description, and name.

Users can:

- Acknowledge the error ( Hide the error until it is sent again)
- Ignore the error ( Hides, resets, and stops reading all future occurrences of the error)
- View ignored errors and choose to unignore those errors

## Setup

Install required dependencies:

```bash
sudo apt-get update
sudo apt-get upgrade
sudo apt install net-tools iproute2 can-utils linux-modules-extra-$(uname -r)
```

Kill all existing instances of ip-links:

`ip link show`
`sudo ip link delete <ip-link>`
```bash
ip link show
sudo ip link delete <ip-link>
```

Setup the can network:

`./setup_vcan.sh <CAN_PORT>`
```bash
./setup_vcan.sh <CAN_PORT>
```

Start the CLI viewer:
`go run error_tui.go -i <CAN_PORT>`
## Usage

Start the CLI viewer for single use:

```bash
go run error_tui.go -i <CAN_PORT> -w <WARN_TIMER>
```

OR

Build the go file:

```bash
go build -o <BUILD_NAME>
./<BUILD_NAME> -i <CAN_PORT> -w <WARN_TIMER>
```

You can now run:
`cansend <CAN_PORT> <MSG_ID>#<MSG_DATA>`

```bash
cansend <CAN_PORT> <MSG>
```
84 changes: 41 additions & 43 deletions scripts/can_errgo/components/table.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package components

// Modified from "github.com/charmbracelet/bubbles/table"
// Repurposed for use in CAN error handling application

import (
"strings"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
Expand All @@ -13,18 +12,25 @@ import (
"github.com/mattn/go-runewidth"
)

// MetaRow is the implementation of a Error Row with its corresponding error index within the 64-bit CAN frame
type MetaRow struct{
Row Row
Index int
}

// Model defines a state for the table widget.
type Model struct {
KeyMap KeyMap
Help help.Model

cols []Column
rows []Row
metaRow []MetaRow
cursor int
focus bool
styles Styles
ShowCursor bool


viewport viewport.Model
start int
end int
Expand Down Expand Up @@ -160,10 +166,10 @@ func WithColumns(cols []Column) Option {
}
}

// WithRows sets the table rows (data).
func WithRows(rows []Row) Option {
//WithRows sets the table rows (data).
func WithRows(rows []MetaRow) Option {
return func(m *Model) {
m.rows = rows
m.metaRow = rows
}
}

Expand Down Expand Up @@ -268,7 +274,7 @@ func (m Model) HelpView() string {
// UpdateViewport updates the list content based on the previously defined
// columns and rows.
func (m *Model) UpdateViewport() {
renderedRows := make([]string, 0, len(m.rows))
renderedRows := make([]string, 0, len(m.metaRow))

// Render only rows from: m.cursor-m.viewport.Height to: m.cursor+m.viewport.Height
// Constant runtime, independent of number of rows in a table.
Expand All @@ -278,7 +284,7 @@ func (m *Model) UpdateViewport() {
} else {
m.start = 0
}
m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.rows))
m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.metaRow))
for i := m.start; i < m.end; i++ {
renderedRows = append(renderedRows, m.renderRow(i))
}
Expand All @@ -291,26 +297,34 @@ func (m *Model) UpdateViewport() {
// SelectedRow returns the selected row.
// You can cast it to your own implementation.
func (m Model) SelectedRow() Row {
if m.cursor < 0 || m.cursor >= len(m.rows) {
if m.cursor < 0 || m.cursor >= len(m.metaRow) {
return nil
}

return m.rows[m.cursor]
return m.metaRow[m.cursor].Row
}

// SelectedIndex returns the Error Index of the selected row.
func (m Model) SelectedIndex() int {
if m.cursor < 0 || m.cursor >= len(m.metaRow) {
return -1
}
return m.metaRow[m.cursor].Index
}

// Rows returns the current rows.
func (m Model) Rows() []Row {
return m.rows
// Rows returns the current metaRow.
func (m Model) Rows() []MetaRow {
return m.metaRow
}

// Columns returns the current columns.
func (m Model) Columns() []Column {
return m.cols
}

// SetRows sets a new rows state.
func (m *Model) SetRows(r []Row) {
m.rows = r
// SetRows sets a new metaRow state.
func (m *Model) SetRows(r []MetaRow) {
m.metaRow = r
m.UpdateViewport()
}

Expand Down Expand Up @@ -350,23 +364,23 @@ func (m Model) Cursor() int {
// ForceCursor forces the cursor to a specific row, regardless if the row is out of bounds.
func (m *Model) ForceCursor(n int) {
// Clamp the cursor position within bounds
m.cursor = clamp(n, 0, len(m.rows)-1)
m.cursor = clamp(n, 0, len(m.metaRow)-1)
m.UpdateViewport()
}

// SetCursor sets the cursor position in the table.
func (m *Model) SetCursor(n int) {
m.cursor = clamp(n, 0, len(m.rows)-1)
m.cursor = clamp(n, 0, len(m.metaRow)-1)
m.UpdateViewport()
}
// SetCursor sets the cursor position in the table.
func (m *Model) SetCursorAndViewport(n int) {
m.cursor = clamp(n, 0, len(m.rows)-1)
m.cursor = clamp(n, 0, len(m.metaRow)-1)

switch {
case m.start == 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor))
case m.end == len(m.rows) && m.cursor > m.viewport.Height:
case m.end == len(m.metaRow) && m.cursor > m.viewport.Height:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.viewport.Height))
}

Expand All @@ -378,10 +392,10 @@ func (m *Model) SetShowCursor(show bool) {
m.UpdateViewport()
}

// MoveUp moves the selection up by any number of rows.
// MoveUp moves the selection up by any number of metaRow.
// It can not go above the first row.
func (m *Model) MoveUp(n int) {
m.cursor = clamp(m.cursor-n, 0, len(m.rows)-1)
m.cursor = clamp(m.cursor-n, 0, len(m.metaRow)-1)
switch {
case m.start == 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor))
Expand All @@ -393,14 +407,14 @@ func (m *Model) MoveUp(n int) {
m.UpdateViewport()
}

// MoveDown moves the selection down by any number of rows.
// MoveDown moves the selection down by any number of metaRow.
// It can not go below the last row.
func (m *Model) MoveDown(n int) {
m.cursor = clamp(m.cursor+n, 0, len(m.rows)-1)
m.cursor = clamp(m.cursor+n, 0, len(m.metaRow)-1)
m.UpdateViewport()

switch {
case m.end == len(m.rows) && m.viewport.YOffset > 0:
case m.end == len(m.metaRow) && m.viewport.YOffset > 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.viewport.Height))
case m.cursor > (m.end-m.start)/2 && m.viewport.YOffset > 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.cursor))
Expand All @@ -417,23 +431,7 @@ func (m *Model) GotoTop() {

// GotoBottom moves the selection to the last row.
func (m *Model) GotoBottom() {
m.MoveDown(len(m.rows))
}

// FromValues create the table rows from a simple string. It uses `\n` by
// default for getting all the rows and the given separator for the fields on
// each row.
func (m *Model) FromValues(value, separator string) {
rows := []Row{}
for _, line := range strings.Split(value, "\n") {
r := Row{}
for _, field := range strings.Split(line, separator) {
r = append(r, field)
}
rows = append(rows, r)
}

m.SetRows(rows)
m.MoveDown(len(m.metaRow))
}

func (m Model) headersView() string {
Expand All @@ -451,7 +449,7 @@ func (m Model) headersView() string {

func (m *Model) renderRow(r int) string {
s := make([]string, 0, len(m.cols))
for i, value := range m.rows[r] {
for i, value := range m.metaRow[r].Row {
if m.cols[i].Width <= 0 {
continue
}
Expand Down
Loading

0 comments on commit 7861322

Please sign in to comment.