### Install Termdash Source: https://github.com/mum4k/termdash/wiki/Home Use the go get command to download the library and navigate to the project directory. ```go go get -u github.com/mum4k/termdash cd github.com/mum4k/termdash ``` -------------------------------- ### Install Termdash Source: https://context7.com/mum4k/termdash/llms.txt Install the Termdash library using the go get command. ```bash go get -u github.com/mum4k/termdash ``` -------------------------------- ### Run Termdash Demo Source: https://github.com/mum4k/termdash/wiki/Home Execute the demo binary to verify the installation and explore dashboard capabilities. ```go go run termdashdemo/termdashdemo.go ``` -------------------------------- ### SparkLine Widget Example Source: https://context7.com/mum4k/termdash/llms.txt Initializes and displays a SparkLine widget with customizable colors and labels. It periodically updates with random integer values. Requires context for cancellation. ```go package main import ( "context" "math/rand" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/sparkline" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) // Green sparkline with label greenSpark, _ := sparkline.New( sparkline.Label("CPU %", cell.FgColor(cell.ColorNumber(33))), sparkline.Color(cell.ColorGreen), ) // Red sparkline redSpark, _ := sparkline.New( sparkline.Label("Memory %", cell.FgColor(cell.ColorNumber(33))), sparkline.Color(cell.ColorRed), ) // Add random values periodically go func() { ticker := time.NewTicker(250 * time.Millisecond) defer ticker.Stop() for { select { case <-ticker.C: greenSpark.Add([]int{rand.Intn(101)}) redSpark.Add([]int{rand.Intn(101)}) case <-ctx.Done(): return } } }() c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("SparkLine Demo - Press Q to Quit"), container.SplitHorizontal( container.Top( container.Border(linestyle.Light), container.PlaceWidget(greenSpark), ), container.Bottom( container.Border(linestyle.Light), container.PlaceWidget(redSpark), ), ), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { panic(err) } } ``` -------------------------------- ### BarChart Widget Example Source: https://context7.com/mum4k/termdash/llms.txt Initializes and displays a BarChart widget with customizable bar and value colors, bar width, and labels. It periodically updates with random integer values. Requires context for cancellation. ```go package main import ( "context" "math/rand" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/barchart" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) bc, err := barchart.New( barchart.BarColors([]cell.Color{ cell.ColorBlue, cell.ColorRed, cell.ColorYellow, cell.ColorGreen, }), barchart.ValueColors([]cell.Color{ cell.ColorWhite, cell.ColorWhite, cell.ColorBlack, cell.ColorBlack, }), barchart.ShowValues(), barchart.BarWidth(10), barchart.Labels([]string{ "Q1", "Q2", "Q3", "Q4", }), ) if err != nil { panic(err) } // Update bar values periodically go func() { ticker := time.NewTicker(time.Second) defer ticker.Stop() for { select { case <-ticker.C: values := make([]int, 4) for i := range values { values[i] = rand.Intn(101) } bc.Values(values, 100) // Values and max value case <-ctx.Done(): return } } }() c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("BarChart Demo - Press Q to Quit"), container.PlaceWidget(bc), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { panic(err) } } ``` -------------------------------- ### SegmentDisplay Clock Example Source: https://context7.com/mum4k/termdash/llms.txt Displays a clock using the SegmentDisplay widget, updating every second. Requires importing necessary Termdash packages. The clock shows hours, minutes, and a blinking colon separator. ```go package main import ( "context" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/segmentdisplay" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) sd, err := segmentdisplay.New( segmentdisplay.MaximizeSegmentHeight(), ) if err != nil { panic(err) } // Display a clock go func() { ticker := time.NewTicker(time.Second) defer ticker.Stop() for { select { case <-ticker.C: now := time.Now() hours := now.Format("15") mins := now.Format("04") // Blinking colon separator := " " if now.Second()%2 == 0 { separator = ":" } chunks := []*segmentdisplay.TextChunk{ segmentdisplay.NewChunk( hours, segmentdisplay.WriteCellOpts(cell.FgColor(cell.ColorNumber(33))), ), segmentdisplay.NewChunk(separator), segmentdisplay.NewChunk( mins, segmentdisplay.WriteCellOpts(cell.FgColor(cell.ColorRed)), ), } sd.Write(chunks) case <-ctx.Done(): return } } }() c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("SegmentDisplay Demo - Press Q to Quit"), container.PlaceWidget(sd), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(time.Second)); err != nil { panic(err) } } ``` -------------------------------- ### Create Grid Layout with Termdash Source: https://github.com/mum4k/termdash/wiki/Grid-layout Constructs a grid layout for the terminal using the grid builder. This example demonstrates adding columns and rows with specified percentages and embedding widgets with borders. ```go t, err := tcell.New() if err != nil { return fmt.Errorf("tcell.New => %v", err) } b, err := button.New("button", func() error { return nil }) if err != nil { return fmt.Errorf("button.New => %v", err) } builder := grid.New() builder.Add( grid.ColWidthPerc(50, grid.Widget(addB, container.Border(linestyle.Light), ), ), ) builder.Add( grid.RowHeightPerc(50, grid.Widget(addB, container.Border(linestyle.Light), ), ), ) builder.Add( grid.ColWidthPerc(25, grid.Widget(addB, container.Border(linestyle.Light), ), ), ) builder.Add( grid.ColWidthPerc(25, grid.Widget(addB, container.Border(linestyle.Light), ), ), ) gridOpts, err := builder.Build() if err != nil { return fmt.Errorf("builder.Build => %v", err) } c, err := container.New(t, gridOpts...) if err != nil { return fmt.Errorf("container.New => %v", err) } ``` -------------------------------- ### Dynamic Layout Update with Container.Update Source: https://context7.com/mum4k/termdash/llms.txt Demonstrates updating a container's layout at runtime using its ID. Requires assigning an ID to the container. This example toggles between two text widgets using a button. ```go package main import ( "context" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/button" "github.com/mum4k/termdash/widgets/text" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) txt1, _ := text.New() txt1.Write("Widget A") txt2, _ := text.New() txt2.Write("Widget B") showingA := true // Create container with ID for dynamic updates c, err := container.New( t, container.ID("root"), container.Border(linestyle.Light), container.BorderTitle("Dynamic Layout - Press Q to Quit"), container.PlaceWidget(txt1), ) if err != nil { panic(err) } // Button to toggle widgets toggleBtn, _ := button.New("Toggle Widget", func() error { showingA = !showingA if showingA { return c.Update("root", container.Border(linestyle.Light), container.BorderTitle("Showing Widget A"), container.PlaceWidget(txt1), ) } return c.Update("root", container.Border(linestyle.Light), container.BorderTitle("Showing Widget B"), container.PlaceWidget(txt2), ) }) // Update to add button c.Update("root", container.Border(linestyle.Light), container.BorderTitle("Dynamic Layout - Press Q to Quit"), container.SplitHorizontal( container.Top( container.PlaceWidget(txt1), ), container.Bottom( container.PlaceWidget(toggleBtn), ), container.SplitPercent(80), ), ) quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(100*time.Millisecond)); err != nil { panic(err) } } ``` -------------------------------- ### Place Widget in Container Source: https://github.com/mum4k/termdash/wiki/Placing-widgets Use container.PlaceWidget to add a widget to a container. Ensure the terminal and widget are initialized correctly. This is the basic setup for a container holding a single widget. ```go t, err := tcell.New() if err != nil { panic(err) } deferr t.Close() b, err := button.New("hello world", func() error { return nil }, ) if err != nil { return fmt.Errorf("button.New => %v", err) } c, err := container.New( t, container.PlaceWidget(b), ) if err != nil { return fmt.Errorf("container.New => %v", err) } ``` -------------------------------- ### termdash.Run Source: https://github.com/mum4k/termdash/wiki/Termdash-API Starts the Termdash application, blocking until the provided context expires. Supports periodic screen redraws. ```APIDOC ## termdash.Run ### Description Starts a Termdash application. This function blocks until the provided context expires. The screen is redrawn periodically while running. ### Parameters - **ctx** (context.Context) - Required - The context to control the application lifecycle. - **t** (terminal.Terminal) - Required - The terminal interface. - **c** (container.Container) - Required - The container holding the widgets. - **options** (...termdash.Option) - Optional - Configuration options like RedrawInterval. ``` -------------------------------- ### Run Termdash with Run function Source: https://github.com/mum4k/termdash/wiki/Termdash-API Starts a Termdash application with a specified timeout context. The application blocks until the context expires. ```go // Create the terminal. t, err := tcell.New() if err != nil { return fmt.Errorf("tcell.New => %v", err) } defer t.Close() // Create the container without widgets. c, err := container.New(t) if err != nil { return fmt.Errorf("container.New => %v", err) } // Termdash runs until the context expires. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := termdash.Run(ctx, t, c); err != nil { return fmt.Errorf("termdash.Run => %v", err) } ``` -------------------------------- ### Run LineChart demonstration Source: https://github.com/mum4k/termdash/blob/master/README.md Executes the LineChart widget demo, which supports mouse-triggered zooming. ```bash go run widgets/linechart/linechartdemo/linechartdemo.go ``` -------------------------------- ### Run SegmentDisplay demonstration Source: https://github.com/mum4k/termdash/blob/master/README.md Executes the SegmentDisplay widget demo to simulate a 16-segment display. ```bash go run widgets/segmentdisplay/segmentdisplaydemo/segmentdisplaydemo.go ``` -------------------------------- ### Run barchart demo Source: https://github.com/mum4k/termdash/wiki/Barchart-API Execute the demonstration program to view the barchart widget in action. ```bash go run github.com/mum4k/termdash/widgets/barchart/barchartdemo/barchartdemo.go ``` -------------------------------- ### Initialize Tcell Terminal Source: https://github.com/mum4k/termdash/wiki/Tcell-API Creates a new terminal instance using the tcell library. This is the primary way to get a terminal for use with Termdash. ```APIDOC ## POST /api/users ### Description Creates a new user in the system. ### Method POST ### Endpoint /api/users ### Parameters #### Request Body - **username** (string) - Required - The desired username for the new user. - **email** (string) - Required - The email address for the new user. - **password** (string) - Required - The password for the new user. ### Request Example { "username": "johndoe", "email": "john.doe@example.com", "password": "securepassword123" } ### Response #### Success Response (201) - **id** (string) - The unique identifier for the newly created user. - **username** (string) - The username of the created user. - **email** (string) - The email address of the created user. #### Response Example { "id": "user-12345", "username": "johndoe", "email": "john.doe@example.com" } ``` -------------------------------- ### Create and Animate a Basic Gauge Widget Source: https://context7.com/mum4k/termdash/llms.txt Demonstrates creating a basic gauge with a border and title, and animating its progress using percentage updates. Requires importing the gauge widget and terminal components. ```go package main import ( "context" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/gauge" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) // Basic gauge with percentage basicGauge, _ := gauge.New( gauge.Height(1), gauge.Border(linestyle.Light), gauge.BorderTitle("Download Progress"), ) // Gauge with custom colors and threshold styledGauge, _ := gauge.New( gauge.Height(1), gauge.Color(cell.ColorNumber(33)), gauge.Border(linestyle.Light), gauge.BorderTitle("CPU Usage"), gauge.Threshold(80, linestyle.Light, cell.FgColor(cell.ColorRed)), // Red when > 80% ) // Gauge with label and no progress text labelGauge, _ := gauge.New( gauge.Height(3), gauge.TextLabel("Memory"), gauge.Color(cell.ColorGreen), gauge.HideTextProgress(), ) // Animate the gauges go func() { progress := 0 ticker := time.NewTicker(200 * time.Millisecond) defer ticker.Stop() for { select { case <-ticker.C: basicGauge.Percent(progress) styledGauge.Absolute(progress, 100) // Absolute value out of max labelGauge.Percent((progress + 30) % 100) progress = (progress + 2) % 101 case <-ctx.Done(): return } } }() c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("Gauge Demo - Press Q to Quit"), container.SplitHorizontal( container.Top(container.PlaceWidget(basicGauge)), container.Bottom( container.SplitHorizontal( container.Top(container.PlaceWidget(styledGauge)), container.Bottom(container.PlaceWidget(labelGauge)), ), ), ), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { panic(err) } } ``` -------------------------------- ### Apply Terminal Cell Colors and Styles Source: https://context7.com/mum4k/termdash/llms.txt Demonstrates how to set foreground and background colors using system, extended, and RGB color values. Also shows how to apply text modifiers like bold, italic, and underline. ```go package main import ( "github.com/mum4k/termdash/cell" ) func main() { // System colors (0-7) _ = cell.FgColor(cell.ColorRed) _ = cell.FgColor(cell.ColorGreen) _ = cell.FgColor(cell.ColorBlue) _ = cell.FgColor(cell.ColorYellow) _ = cell.FgColor(cell.ColorMagenta) _ = cell.FgColor(cell.ColorCyan) _ = cell.FgColor(cell.ColorWhite) _ = cell.FgColor(cell.ColorBlack) // Extended colors by number (0-255) _ = cell.FgColor(cell.ColorNumber(33)) // Blue shade _ = cell.FgColor(cell.ColorNumber(196)) // Red shade _ = cell.FgColor(cell.ColorNumber(220)) // Yellow/orange // RGB colors (6-bit: 0-5 per channel) _ = cell.FgColor(cell.ColorRGB6(5, 0, 0)) // Red _ = cell.FgColor(cell.ColorRGB6(0, 5, 0)) // Green _ = cell.FgColor(cell.ColorRGB6(0, 0, 5)) // Blue // RGB colors (24-bit: 0-255 per channel) _ = cell.FgColor(cell.ColorRGB24(255, 128, 0)) // Orange // Background colors _ = cell.BgColor(cell.ColorBlue) _ = cell.BgColor(cell.ColorNumber(240)) // Gray // Text modifiers _ = cell.Bold() _ = cell.Italic() _ = cell.Underline() } ``` -------------------------------- ### Run BarChart demonstration Source: https://github.com/mum4k/termdash/blob/master/README.md Executes the BarChart widget demo to display relative ratios of values. ```bash go run widgets/barchart/barchartdemo/barchartdemo.go ``` -------------------------------- ### Run SparkLine demonstration Source: https://github.com/mum4k/termdash/blob/master/README.md Executes the SparkLine widget demo to visualize values as vertical bars. ```bash go run widgets/sparkline/sparklinedemo/sparklinedemo.go ``` -------------------------------- ### Initialize and display barchart Source: https://github.com/mum4k/termdash/wiki/Barchart-API Create a new barchart instance and render it within a termdash container with sample data. ```go func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) bc, err := barchart.New() if err != nil { panic(err) } c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("PRESS Q TO QUIT"), container.PlaceWidget(bc), ) if err != nil { panic(err) } values := []int{1, 2, 3, 4, 5} max := 10 if err := bc.Values(values, max); err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' || k.Key == 'Q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { panic(err) } } ``` -------------------------------- ### Initialize and Animate a LineChart Source: https://context7.com/mum4k/termdash/llms.txt Demonstrates creating a LineChart widget, configuring axis colors, and updating series data dynamically using a ticker. ```go package main import ( "context" "math" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/linechart" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) lc, err := linechart.New( linechart.AxesCellOpts(cell.FgColor(cell.ColorRed)), linechart.YLabelCellOpts(cell.FgColor(cell.ColorGreen)), linechart.XLabelCellOpts(cell.FgColor(cell.ColorCyan)), ) if err != nil { panic(err) } // Generate sine wave data sineData := make([]float64, 200) for i := range sineData { sineData[i] = math.Sin(float64(i) / 100 * math.Pi) } // Animate the chart go func() { step := 0 ticker := time.NewTicker(80 * time.Millisecond) defer ticker.Stop() for { select { case <-ticker.C: step = (step + 1) % len(sineData) // Rotate the data rotated := append(sineData[step:], sineData[:step]...) // First series (blue) lc.Series("sine", rotated, linechart.SeriesCellOpts(cell.FgColor(cell.ColorNumber(33))), linechart.SeriesXLabels(map[int]string{ 0: "start", 100: "middle", }), ) // Second series (white, phase shifted) step2 := (step + 100) % len(sineData) rotated2 := append(sineData[step2:], sineData[:step2]...) lc.Series("cosine", rotated2, linechart.SeriesCellOpts(cell.FgColor(cell.ColorWhite)), ) case <-ctx.Done(): return } } }() c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("LineChart Demo - Press Q to Quit"), container.PlaceWidget(lc), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(100*time.Millisecond)); err != nil { panic(err) } } ``` -------------------------------- ### Initialize Tcell with Clear Style Source: https://github.com/mum4k/termdash/wiki/Tcell-API Set the foreground and background colors used when the screen is cleared. ```go t, err := tcell.New(tcell.ClearStyle(cell.ColorYellow, cell.ColorBlue)) if err != nil { return fmt.Errorf("tcell.New => %v", err) } ``` -------------------------------- ### Implement a Text Widget in Go Source: https://context7.com/mum4k/termdash/llms.txt Demonstrates creating simple, styled, and rolling text widgets within a termdash container. ```go package main import ( "context" "fmt" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/text" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() // Simple text widget simpleText, _ := text.New() simpleText.Write("Hello, World!") // Text with colors and styles styledText, _ := text.New(text.WrapAtRunes()) styledText.Write("Red text ", text.WriteCellOpts(cell.FgColor(cell.ColorRed))) styledText.Write("Bold text ", text.WriteCellOpts(cell.Bold())) styledText.Write("Underlined", text.WriteCellOpts(cell.Underline())) // Rolling text that scrolls content rollingText, _ := text.New(text.RollContent(), text.WrapAtWords()) rollingText.Write("This text rolls upward as new content is added.\n") ctx, cancel := context.WithCancel(context.Background()) // Goroutine to add new lines periodically go func() { i := 0 ticker := time.NewTicker(time.Second) defer ticker.Stop() for { select { case <-ticker.C: i++ rollingText.Write( fmt.Sprintf("Line %d added at %s\n", i, time.Now().Format("15:04:05")), text.WriteCellOpts(cell.FgColor(cell.ColorGreen)), ) case <-ctx.Done(): return } } }() c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("Text Widget Demo - Press Q to Quit"), container.PlaceWidget(rollingText), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' || k.Key == 'Q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { panic(err) } } ``` -------------------------------- ### Run Button Widget Demo Source: https://github.com/mum4k/termdash/blob/master/README.md Execute the demo for the Button widget to observe its functionality. This demonstrates user interaction with buttons in the terminal. ```go go run widgets/button/buttondemo/buttondemo.go ``` -------------------------------- ### Run Text Widget Demo Source: https://github.com/mum4k/termdash/blob/master/README.md Execute the demo for the Text widget to display text content, including features like trimming and scrolling. This is suitable for displaying logs or messages. ```go go run widgets/text/textdemo/textdemo.go ``` -------------------------------- ### Run Termdash Demo Source: https://github.com/mum4k/termdash/wiki/Dynamic-layout Execute the Termdash demo application to see dynamic layout changes in action. ```bash go run github.com/mum4k/termdash/termdashdemo/termdashdemo.go ``` -------------------------------- ### Create and Animate a Donut Widget Source: https://context7.com/mum4k/termdash/llms.txt Shows how to create a donut widget with custom colors and labels, and animate its progress using absolute values. Requires importing the donut widget and terminal components. ```go package main import ( "context" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/donut" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) // Donut with percentage greenDonut, _ := donut.New( donut.CellOpts(cell.FgColor(cell.ColorGreen)), donut.Label("Tasks", cell.FgColor(cell.ColorGreen)), ) // Donut with custom color blueDonut, _ := donut.New( donut.CellOpts(cell.FgColor(cell.ColorNumber(33))), ) // Animate donuts go func() { progress := 0 ticker := time.NewTicker(100 * time.Millisecond) defer ticker.Stop() for { select { case <-ticker.C: greenDonut.Percent(progress) blueDonut.Absolute(progress, 100) progress = (progress + 1) % 101 case <-ctx.Done(): return } } }() c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("Donut Demo - Press Q to Quit"), container.SplitVertical( container.Left(container.PlaceWidget(greenDonut)), container.Right(container.PlaceWidget(blueDonut)), ), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(100*time.Millisecond)); err != nil { panic(err) } } ``` -------------------------------- ### Run Donut Widget Demo Source: https://github.com/mum4k/termdash/blob/master/README.md Execute the demo for the Donut widget to visualize progress as a partial or complete donut shape. This provides an alternative to linear progress indicators. ```go go run widgets/donut/donutdemo/donutdemo.go ``` -------------------------------- ### Implement Button Widget in Go Source: https://context7.com/mum4k/termdash/llms.txt Creates interactive buttons with global keyboard shortcuts and custom styling. Requires the termdash/widgets/button package. ```go package main import ( "context" "fmt" "time" "github.com/mum4k/termdash" "github.com/mum4k/termdash/align" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/button" "github.com/mum4k/termdash/widgets/text" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) counter := 0 display, _ := text.New() display.Write(fmt.Sprintf("Count: %d", counter)) // Increment button with 'a' as global key addBtn, err := button.New("(a)dd", func() error { counter++ display.Reset() return display.Write(fmt.Sprintf("Count: %d", counter)) }, button.GlobalKey('a'), button.FillColor(cell.ColorNumber(33)), button.WidthFor("(s)ubtract"), // Match width to another button ) if err != nil { panic(err) } // Decrement button with 's' as global key subBtn, err := button.New("(s)ubtract", func() error { counter-- display.Reset() return display.Write(fmt.Sprintf("Count: %d", counter)) }, button.GlobalKey('s'), button.FillColor(cell.ColorNumber(220)), ) if err != nil { panic(err) } c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("Button Demo - Press Q to Quit"), container.SplitHorizontal( container.Top( container.PlaceWidget(display), ), container.Bottom( container.SplitVertical( container.Left( container.PlaceWidget(addBtn), container.AlignHorizontal(align.HorizontalRight), ), container.Right( container.PlaceWidget(subBtn), container.AlignHorizontal(align.HorizontalLeft), ), ), ), container.SplitPercent(60), ), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' || k.Key == 'Q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(100*time.Millisecond)); err != nil { panic(err) } } ``` -------------------------------- ### Run TextInput Form Demo Source: https://github.com/mum4k/termdash/blob/master/README.md Execute the demo for using the TextInput widget to create forms that support keyboard navigation. This showcases interactive form elements. ```go go run widgets/textinput/formdemo/formdemo.go ``` -------------------------------- ### Run TextInput Widget Demo Source: https://github.com/mum4k/termdash/blob/master/README.md Execute the demo for the TextInput widget to see how users can input and edit text. This is useful for creating interactive forms. ```go go run widgets/textinput/textinputdemo/textinputdemo.go ``` -------------------------------- ### Initialize Terminal with tcell Source: https://context7.com/mum4k/termdash/llms.txt Creates a new terminal instance using the tcell backend. Supports default 256-color mode, custom color modes, and custom clear styles. ```go package main import ( "fmt" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" ) func main() { // Create terminal with default 256-color mode t, err := tcell.New() if err != nil { panic(fmt.Errorf("tcell.New => %v", err)) } defer t.Close() // Or with custom color mode t256, err := tcell.New(tcell.ColorMode(terminalapi.ColorMode256)) if err != nil { panic(fmt.Errorf("tcell.New => %v", err)) } defer t256.Close() // Or with custom clear style (foreground/background colors) tStyled, err := tcell.New(tcell.ClearStyle(cell.ColorYellow, cell.ColorBlue)) if err != nil { panic(fmt.Errorf("tcell.New => %v", err)) } defer tStyled.Close() } ``` -------------------------------- ### Initialize a Tcell Terminal Source: https://github.com/mum4k/termdash/wiki/Tcell-API Create a new terminal instance using the tcell library. ```go t, err := tcell.New() if err != nil { return fmt.Errorf("tcell.New => %v", err) } ``` -------------------------------- ### Assigning a mouse button value Source: https://github.com/mum4k/termdash/wiki/Mouse-API Demonstrates how to declare a mouse.Button variable and assign a predefined button value. ```go var button mouse.Button button = mouse.ButtonLeft ``` -------------------------------- ### Run Gauge Widget Demo Source: https://github.com/mum4k/termdash/blob/master/README.md Execute the demo for the Gauge widget to visualize the progress of an operation. This is useful for progress bars and status indicators. ```go go run widgets/gauge/gaugedemo/gaugedemo.go ``` -------------------------------- ### Initialize Termdash Controller Source: https://github.com/mum4k/termdash/wiki/Termdash-API Creates a controller instance for manual screen redraw control, avoiding the blocking behavior of the Run function. ```go // Create the terminal. t, err := tcell.New() if err != nil { return fmt.Errorf("tcell.New => %v", err) } defer t.Close() // Create the container without widgets. c, err := container.New(t) if err != nil { return fmt.Errorf("container.New => %v", err) } // Create the controller. ctrl, err := termdash.NewController(t, c) if err != nil { return fmt.Errorf("termdash.NewController => %v", err) } // Close the controller and termdash once it isn't required anymore. defer ctrl.Close() // Redraw the screen manually. if err := ctrl.Redraw(); err != nil { return fmt.Errorf("ctrl.Redraw => %v", err) } ``` -------------------------------- ### Implement TextInput Widget in Go Source: https://context7.com/mum4k/termdash/llms.txt Creates a text input field with support for labels, placeholders, and submission callbacks. Requires the termdash/widgets/textinput package. ```go package main import ( "context" "github.com/mum4k/termdash" "github.com/mum4k/termdash/cell" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/keyboard" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" "github.com/mum4k/termdash/widgets/text" "github.com/mum4k/termdash/widgets/textinput" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() ctx, cancel := context.WithCancel(context.Background()) output, _ := text.New(text.RollContent()) // Create text input with various options input, err := textinput.New( textinput.Label("Enter text: ", cell.FgColor(cell.ColorNumber(33))), textinput.MaxWidthCells(30), textinput.PlaceHolder("Type something..."), textinput.Border(linestyle.Light), textinput.OnChange(func(data string) { // Called on every keystroke }), textinput.OnSubmit(func(text string) error { output.Write("Submitted: " + text + "\n") return nil }), textinput.ClearOnSubmit(), ) if err != nil { panic(err) } c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("TextInput Demo - Press Esc to Quit"), container.SplitHorizontal( container.Top( container.PlaceWidget(input), ), container.Bottom( container.Border(linestyle.Light), container.BorderTitle("Output"), container.PlaceWidget(output), ), container.SplitPercent(30), ), ) if err != nil { panic(err) } quitter := func(k *terminalapi.Keyboard) { if k.Key == keyboard.KeyEsc { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { panic(err) } } ``` -------------------------------- ### termbox.New Source: https://github.com/mum4k/termdash/wiki/Termbox-API Initializes a new Terminal instance using the termbox-go library. This is one of the valid options for providing a terminal instance to Termdash. ```APIDOC ## termbox.New ### Description Users of Termdash need to provide [[Termdash API|termdash-api]] with a terminal instance. Termbox is one of the valid options. Use the **New** function to create a Terminal instance. ### Method POST ### Endpoint /api/termbox ### Request Body - **options** (array) - Optional - An array of termbox.Option to configure the terminal. ### Request Example ```json { "options": [ { "type": "ColorMode", "value": "terminalapi.ColorModeGrayscale" } ] } ``` ### Response #### Success Response (200) - **terminal** (object) - The initialized terminal instance. - **error** (string) - An error message if initialization failed. #### Response Example ```json { "terminal": {}, "error": null } ``` ``` -------------------------------- ### Create Binary Tree Layout in Go Source: https://context7.com/mum4k/termdash/llms.txt Use container.SplitVertical and container.SplitHorizontal to recursively build a binary tree layout. Configure border styles and titles for each sub-container. Splits are defined by percentages. ```go package main import ( "context" "github.com/mum4k/termdash" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/tcell" "github.com/mum4k/termdash/terminal/terminalapi" ) func main() { t, err := tcell.New() if err != nil { panic(err) } defer t.Close() // Create a layout with binary tree splits c, err := container.New( t, container.Border(linestyle.Light), container.BorderTitle("Binary Tree Layout"), container.SplitVertical( container.Left( container.Border(linestyle.Light), container.BorderTitle("Left Panel"), ), container.Right( container.SplitHorizontal( container.Top( container.Border(linestyle.Light), container.BorderTitle("Top Right"), ), container.Bottom( container.SplitVertical( container.Left( container.Border(linestyle.Light), container.BorderTitle("Bottom Left"), ), container.Right( container.Border(linestyle.Light), container.BorderTitle("Bottom Right"), ), ), ), container.SplitPercent(40), // Top gets 40% ), ), container.SplitPercent(30), // Left gets 30% ), ) if err != nil { panic(err) } ctx, cancel := context.WithCancel(context.Background()) quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' || k.Key == 'Q' { cancel() } } if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { panic(err) } } ``` -------------------------------- ### Initialize Termdash with Keyboard Subscriber to Quit Source: https://github.com/mum4k/termdash/wiki/Termdash-API Adds a keyboard subscriber that cancels the context when 'q' or 'Q' is pressed, terminating the application. The subscriber must be thread-safe. ```go // Create terminal t and container c as in the examples above. // Context for Termdash, the application quits when this expires. // Since the context has no deadline, it will only expire when cancel() is called. ctx, cancel := context.WithCancel(context.Background()) // A keyboard subscriber that terminates the application by cancelling the context. quitter := func(k *terminalapi.Keyboard) { if k.Key == 'q' || k.Key == 'Q' { cancel() } } // Termdash will run until one of the two letters is pressed. if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { return fmt.Errorf("termdash.Run => %v", err) } ``` -------------------------------- ### Redirect Log Output to File Source: https://github.com/mum4k/termdash/wiki/Debugging Configure the standard log package to write all debug messages to a file. Ensure the file is created successfully before setting it as the log output. ```go f, err := os.Create("debug.log") if err != nil { log.Fatal(err) } log.SetOutput(f) ``` -------------------------------- ### Create Terminal Layout with Container Splits Source: https://github.com/mum4k/termdash/wiki/Binary-tree-layout Use container.New with SplitVertical and SplitHorizontal to construct a complex terminal layout. Ensure tcell is initialized and handle potential errors during container creation. ```go t, err := tcell.New() if err != nil { return fmt.Errorf("tcell.New => %v", err) } if _, err := container.New( t, container.SplitVertical( container.Left( container.Border(linestyle.Light), ), container.Right( container.SplitHorizontal( container.Top( container.Border(linestyle.Light), ), container.Bottom( container.SplitVertical( container.Left( container.Border(linestyle.Light), ), container.Right( container.Border(linestyle.Light), ), ), ), ), ), ), ); err != nil { return fmt.Errorf("container.New => %v", err) } ```