Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/tui refactor #38

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Feat/tui refactor #38

wants to merge 6 commits into from

Conversation

FilippoTrotter
Copy link
Collaborator

@FilippoTrotter FilippoTrotter commented Jul 16, 2024

improve user interaction in the tui

Summary by CodeRabbit

  • New Features

    • Enhanced the file selection functionality with support for dynamic terminal dimensions.
    • Improved UI elements to adjust based on window size, providing a more responsive experience.
  • Bug Fixes

    • Fixed handling of multiple file selections and window resizing.
  • Tests

    • Updated tests to reflect changes in model initialization and dimension handling.

Copy link
Contributor

coderabbitai bot commented Jul 16, 2024

Walkthrough

Walkthrough

The changes primarily focus on enhancing the terminal-based user interface by adding support for dynamic terminal dimensions. These include updating the initialization functions to accept width and height parameters, modifying the struct fields to store these dimensions, and adjusting the rendering logic to utilize the new dimensions. Additionally, new utility functions are introduced to fetch terminal sizes dynamically.

Changes

File Summary of Changes
cmd/root.go Modified InitialModel call to include width, added getTerminalSize function to retrieve dimensions.
utils/server/server.go Enhanced StartServer to initialize FilesSelector with terminal dimensions.
utils/tui/model.go Added width and height fields to Model, adjusted methods for dynamic dimensions.
utils/tui/model_test.go Updated model initialization in tests to include width and height, used temporary directories.
utils/tui/modelutils/file.go Added width field to FilesSelector, updated InitialModel and added resize handling.
utils/tui/modelutils/option.go Added width and height fields to ModeSelector, updated initialization and rendering logic.
utils/tui/modelutils/text.go Added width and height fields to LabelInput, updated initialization and rendering logic.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@FilippoTrotter FilippoTrotter linked an issue Jul 16, 2024 that may be closed by this pull request
2 tasks
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range, codebase verification and nitpick comments (1)
utils/tui/modelutils/text.go (1)

64-66: Document the design choice for setting width to half.

The design choice of setting the width to half in the tea.WindowSizeMsg handler is not documented in the codebase. It is recommended to add comments or documentation to explain the rationale behind this choice to ensure clarity and maintainability.

  • File: utils/tui/modelutils/text.go
  • Lines: 64-66
Analysis chain

Handle window size changes dynamically.

The update to handle tea.WindowSizeMsg is a good practice for responsive UIs. However, the width is again halved here. As mentioned earlier, ensure this is intentional and documented.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the design choice for setting width to half in Update method.

# Test: Search for comments or documentation explaining this choice. Expect: Comments or docs justifying the halved width.
rg --type go -A 5 $'width / 2'

Length of output: 561


Script:

#!/bin/bash
# Description: Search for comments or documentation explaining the choice of setting width to half in the codebase.

# Search for comments in Go files that might explain the rationale behind setting the width to half.
rg --type go -A 3 -B 3 '//.*width / 2'

# Search for any documentation files that might explain this design choice.
rg 'width / 2' README.md docs/

Length of output: 119

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4088ab0 and 4bfd850.

Files selected for processing (10)
  • cmd/root.go (1 hunks)
  • utils/server/server.go (1 hunks)
  • utils/tui/model.go (11 hunks)
  • utils/tui/model_test.go (16 hunks)
  • utils/tui/modelutils/file.go (7 hunks)
  • utils/tui/modelutils/file_test.go (12 hunks)
  • utils/tui/modelutils/option.go (5 hunks)
  • utils/tui/modelutils/option_test.go (4 hunks)
  • utils/tui/modelutils/text.go (4 hunks)
  • utils/tui/modelutils/text_test.go (2 hunks)
Additional context used
golangci-lint
utils/tui/modelutils/file.go

139-139: (*FilesSelector).doResize - result 0 (github.com/charmbracelet/bubbletea.Cmd) is always nil

(unparam)

utils/tui/model_test.go

[warning] 62-62: unused-parameter: parameter 'm' seems to be unused, consider removing or renaming it as _

(revive)

Additional comments not posted (26)
utils/tui/modelutils/option.go (5)

4-6: Approved import additions.

The addition of the lipgloss package is necessary for the new rendering logic in the View method.


16-17: Approved addition of Width and Height fields to ModeSelector.

These fields are essential for handling dynamic window sizes, aligning with the PR's objectives to enhance UI responsiveness.


75-75: Use of lipgloss for positioning in View method.

The use of lipgloss.Place for positioning the rendered text is a good use of the library's capabilities to enhance the layout of the TUI. This change supports the PR's goal of improving user interaction.

Also applies to: 101-101


20-27: Review the logic for setting Width in NewModeSelector.

The width is set to half of the provided value (width / 2). This might be intentional for layout purposes, but it's important to verify this design choice to ensure it aligns with the UI requirements.


58-60: Handle window size changes dynamically.

The update to handle tea.WindowSizeMsg is a good practice for responsive UIs. However, the width is again halved here. As mentioned earlier, ensure this is intentional and documented.

utils/server/server.go (1)

47-49: Proper initialization of FilesSelector with window dimensions.

The FilesSelector model is correctly initialized with adjusted window dimensions (Height-5 and Width-5). This adjustment likely accounts for margins or headers/footers, which is a thoughtful inclusion for UI layout considerations.

utils/tui/modelutils/text.go (3)

21-22: Approved addition of Width and Height fields to LabelInput.

These fields are essential for handling dynamic window sizes, aligning with the PR's objectives to enhance UI responsiveness.


95-95: Use of lipgloss for positioning in View method.

The use of lipgloss.Place for positioning the rendered text is a good use of the library's capabilities to enhance the layout of the TUI. This change supports the PR's goal of improving user interaction.


25-32: Review the logic for setting Width in NewLabelInput.

The width is set to half of the provided value (width / 2). This might be intentional for layout purposes, but it's important to verify this design choice to ensure it aligns with the UI requirements.

utils/tui/modelutils/option_test.go (1)

30-31: Updated test cases reflect new parameters correctly.

The test cases have been updated to include the new Width and Height parameters. The assertions check these fields appropriately, ensuring that the tests are comprehensive and reflect the changes in the ModeSelector struct.

Also applies to: 38-38, 50-50, 65-71, 76-82, 87-95, 100-107, 112-118, 151-151

utils/tui/modelutils/file.go (4)

23-24: Addition of new fields to FilesSelector struct.

The addition of WindowWidth and MultipleSelection fields are in line with the PR's objective to handle window dimensions and multiple selections.


Line range hint 27-53: Updated function signature and logic in InitialModel.

The InitialModel function now accepts windowWidth as a parameter, which aligns with the need to handle different window sizes. The function correctly initializes the WindowWidth field of the FilesSelector struct.


70-76: Logic for toggling MultipleSelection in the Update method.

The implementation allows toggling the MultipleSelection state with a space key. This is a straightforward and effective way to handle user input for selecting multiple files.


Line range hint 153-205: Enhancements in the View method to handle help messages and file selection.

The method now dynamically constructs help messages and file selection views based on the current state, which enhances the user interface. The use of lipgloss for styling and the dynamic construction of views based on WindowWidth and WindowHeight are well-implemented.

Tools
golangci-lint

139-139: (*FilesSelector).doResize - result 0 (github.com/charmbracelet/bubbletea.Cmd) is always nil

(unparam)

utils/tui/modelutils/file_test.go (1)

33-40: Updated test cases to include new windowWidth parameter.

All test cases have been updated to include the new windowWidth parameter in the InitialModel function calls. This change is necessary to test the new functionality related to window dimensions handling in the FilesSelector.

Also applies to: 48-48, 59-59, 70-70, 81-86, 93-93, 109-109, 119-120, 132-132, 143-143, 178-178, 189-189, 203-203, 217-217, 230-230

utils/tui/modelutils/text_test.go (1)

45-46: Updated test cases to include new Width and Height parameters.

All test cases have been updated to include the new Width and Height parameters in the NewLabelInput, InitLabelInput, and UpdateLabelInput function calls. This change is necessary to test the new functionality related to dimension handling in the label input functionality.

Also applies to: 53-53, 60-60, 74-82, 87-95, 100-109, 114-122, 127-135, 140-149

cmd/root.go (1)

103-103: Updated InitialModel function call to include new windowWidth parameter.

The update to include an additional windowWidth parameter in the InitialModel function call aligns with the changes made to the function's signature. This ensures that the TUI can handle different window sizes, improving user interaction.

utils/tui/model.go (5)

9-9: Import of lipgloss added.

The addition of lipgloss is consistent with the enhancements in UI styling and layout, which is crucial for a TUI application.


24-25: Addition of Width and Height to Model struct.

The addition of Width and Height fields is essential for handling window dimensions, which is a key feature in this PR. This change will allow the TUI to adapt dynamically to different window sizes.


52-55: Handling of tea.KeyMsg for state Final.

The logic to handle a tea.KeyMsg when the state is Final is straightforward and correct. If the application is in the Final state, it quits, which is expected behavior.


82-82: Dynamic initialization of selectors with dimensions.

The ModeSelector and LabelInput are dynamically initialized with the new width and height parameters. This is a crucial change for ensuring that the UI components adjust based on the available space, enhancing user experience.

Also applies to: 85-85, 99-99, 117-117, 124-124, 126-126, 151-151, 172-172, 186-186


222-253: Enhanced rendering logic in the View method.

The changes in the View method to dynamically adjust the layout based on the current state and window dimensions are well-implemented. Using lipgloss to handle the styling and layout ensures a more flexible and visually appealing TUI.

utils/tui/model_test.go (4)

15-19: Setup of temporary directory and file for testing.

Creating a temporary directory and file is a good practice for isolated testing, ensuring that tests do not interfere with the actual file system or leave residual files.


21-21: Initialization of Model with new dimensions in tests.

The tests have been updated to initialize the Model with the new width and height parameters. This is necessary to test the new functionality thoroughly.

Also applies to: 40-40, 60-60, 76-76, 163-163, 181-181


89-89: Initialization of ModeSelector with new dimensions in tests.

The ModeSelector is correctly initialized with width and height parameters in various test scenarios. This change is crucial for ensuring that the component behaves as expected under different dimensions.

Also applies to: 104-104, 118-118, 132-132, 164-164, 183-183, 199-199, 215-215, 230-230, 247-247, 262-262


431-431: Test case for FileSelection view.

The test case for verifying the FileSelection view is well-implemented. It checks that the output contains the expected string, ensuring that the view renders correctly.

utils/tui/model_test.go Show resolved Hide resolved
utils/tui/modelutils/file.go Outdated Show resolved Hide resolved
utils/tui/modelutils/file.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4bfd850 and 18ee858.

Files selected for processing (1)
  • cmd/root.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • cmd/root.go

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 18ee858 and bc34aef.

Files selected for processing (5)
  • cmd/root.go (3 hunks)
  • utils/tui/model.go (12 hunks)
  • utils/tui/modelutils/file.go (7 hunks)
  • utils/tui/modelutils/option.go (5 hunks)
  • utils/tui/modelutils/text.go (5 hunks)
Files skipped from review as they are similar to previous changes (3)
  • cmd/root.go
  • utils/tui/model.go
  • utils/tui/modelutils/text.go
Additional context used
golangci-lint
utils/tui/modelutils/option.go

[warning] 76-76: indent-error-flow: if block ends with a return statement, so drop this else and outdent its block

(revive)


78-78: unnecessary leading newline

(whitespace)

utils/tui/modelutils/file.go

137-137: (*FilesSelector).doResize - result 0 (github.com/charmbracelet/bubbletea.Cmd) is always nil

(unparam)

Additional comments not posted (9)
utils/tui/modelutils/option.go (5)

3-6: Imports updated to include lipgloss.

The import of github.com/charmbracelet/lipgloss is added to support the new rendering logic. This change is aligned with the PR's objectives to enhance the TUI.


16-17: New fields added to ModeSelector struct.

The addition of Width and Height fields supports dynamic sizing of the terminal interface, which is crucial for a responsive TUI design.


58-60: Dynamic resizing logic added to Update method.

The handling of tea.WindowSizeMsg allows the ModeSelector to adjust its dimensions dynamically. This is a significant improvement for usability in varying terminal sizes.


75-75: Use of lipgloss.Place for dynamic content placement.

The View method now uses lipgloss.Place to render content based on the dynamically adjusted dimensions, enhancing the layout flexibility.


20-27: Updated constructor function to handle new dimensions.

The NewModeSelector function now accepts additional parameters for width and height, initializing the new struct fields appropriately. Note the division by 2 on the width (line 27), which might be intentional for layout purposes but should be verified or documented to avoid confusion.

utils/tui/modelutils/file.go (4)

23-24: New fields added to FilesSelector struct.

The addition of WindowWidth and MultipleSelection fields supports enhanced functionality for window resizing and toggling multiple selections, respectively.


Line range hint 27-53: Updated constructor function to handle new dimensions.

The InitialModel function now includes parameters for windowWidth, allowing the FilesSelector to initialize with the correct dimensions. This is crucial for a responsive TUI design.


70-76: Toggle logic for MultipleSelection.

The Update method now handles toggling the MultipleSelection state, which is essential for user interactions involving multiple file selections. The logic is straightforward and aligns with user expectations.


131-141: Window resizing method added.

The addition of the doResize method allows dynamic resizing of the window. This method is crucial for maintaining the layout during window size changes.

Tools
golangci-lint

137-137: (*FilesSelector).doResize - result 0 (github.com/charmbracelet/bubbletea.Cmd) is always nil

(unparam)

for i, choice := range m.Choices {
cursor := " "
if m.cursor == i {
cursor = ">"
}
s += cursor + " " + choice + "\n"
}
return s
return lipgloss.Place(m.Width, m.Height, lipgloss.Left, lipgloss.Center, s)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static analysis suggestions for code simplification.

The static analysis tool suggests simplifying the control flow by removing unnecessary else blocks and leading newlines, which can enhance readability and maintainability.

- else {
-    s := ""
-    switch m.Speed {
+ switch m.Speed {
- s += Paint("silver").Render("\n Select action for file: "+m.File) + "\n\n"
+ s += Paint("silver").Render("Select action for file: "+m.File) + "\n"

Also applies to: 78-78

Tools
golangci-lint

[warning] 76-76: indent-error-flow: if block ends with a return statement, so drop this else and outdent its block

(revive)

Comment on lines +137 to +141
func (m *FilesSelector) doResize(msg tea.WindowSizeMsg) tea.Cmd {
m.WindowHeight = msg.Height
m.WindowWidth = msg.Width
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant nil return in doResize method flagged by static analysis.

The static analysis tool flagged that doResize always returns nil, which might indicate that the method's return type doesn't need to be tea.Cmd. Consider changing the method signature if no command is ever returned.

- func (m *FilesSelector) doResize(msg tea.WindowSizeMsg) tea.Cmd {
+ func (m *FilesSelector) doResize(msg tea.WindowSizeMsg) {
  m.WindowHeight = msg.Height
  m.WindowWidth = msg.Width
- return nil
}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (m *FilesSelector) doResize(msg tea.WindowSizeMsg) tea.Cmd {
m.WindowHeight = msg.Height
m.WindowWidth = msg.Width
return nil
}
func (m *FilesSelector) doResize(msg tea.WindowSizeMsg) {
m.WindowHeight = msg.Height
m.WindowWidth = msg.Width
}
Tools
golangci-lint

137-137: (*FilesSelector).doResize - result 0 (github.com/charmbracelet/bubbletea.Cmd) is always nil

(unparam)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User interaction
1 participant