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

Dynamically wrap strings based on the window width. #87

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/secguro/fix_choose_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import (
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/reflow/wordwrap"
)

type modelChooseOption struct {
prompt string
choices []string
cursor int
choice string
windowWidth int
prompt string
choices []string
cursor int
choice string
}

func (m modelChooseOption) Init() tea.Cmd {
return nil
}

func (m modelChooseOption) Update(msg tea.Msg) (tea.Model, tea.Cmd) { //nolint: ireturn // must be like this
switch msg := msg.(type) { //nolint: gocritic
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.windowWidth = msg.Width
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
Expand Down Expand Up @@ -62,15 +66,16 @@ func (m modelChooseOption) View() string {
}
s.WriteString("\n(press q to quit)\n")

return s.String()
return wordwrap.String(s.String(), m.windowWidth)
}

func initialModelChooseOption(prompt string, choices []string) modelChooseOption {
return modelChooseOption{
prompt: prompt,
choices: choices,
cursor: 0,
choice: choices[0],
windowWidth: 0,
prompt: prompt,
choices: choices,
cursor: 0,
choice: choices[0],
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/secguro/fix_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

func fixSecret(unifiedFinding UnifiedFinding) error {
// TODO: make line breaks dependent on the terminal's width
prompt := "Please specify the secret to remove from all git history. \n" +
"Note that we are not always able to determine the exact bounds of \n" +
prompt := "Please specify the secret to remove from all git history. " +
"Note that we are not always able to determine the exact bounds of " +
"the secret, so it's important you specify the secret exactly."

secret := ""
Expand All @@ -31,17 +31,17 @@ func fixSecret(unifiedFinding UnifiedFinding) error {
break
}

prompt = "The specified secret is in the git index. Please replace the \n" +
"secret, commit your changes, and try again. We only delete \n" +
"secrets that are not in the git index to make sure that your \n" +
"code keeps working. The file system state of your latest commit \n" +
"will never be modified by secguro when deleting secrets.\n" +
"\n" +
"You may use environmnt variables to insert secrets into your \n" +
"program. Make sure that your secrets are also available in any \n" +
"CI tools you are using, as well as your production and CD environments.\n" +
"Check out: https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions\n" +
"\n" +
prompt = "The specified secret is in the git index. Please replace the " +
"secret, commit your changes, and try again. We only delete " +
"secrets that are not in the git index to make sure that your " +
"code keeps working. The file system state of your latest commit " +
"will never be modified by secguro when deleting secrets." +
"\n\n" +
"You may use environmnt variables to insert secrets into your " +
"program. Make sure that your secrets are also available in any " +
"CI tools you are using, as well as your production and CD environments." +
"Check out: https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions" +
"\n\n" +
"We found the secret in:\n" +
searchResult
choices := []string{"back", "I have removed the secret from the latest commit."}
Expand Down
21 changes: 13 additions & 8 deletions src/secguro/fix_text_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/reflow/wordwrap"
)

// TODO: get answer in cleaner way; like in fix_choose_option
Expand All @@ -24,9 +25,10 @@ type (
)

type modelTextInput struct {
prompt string
textInput textinput.Model
err error
windowWidth int
prompt string
textInput textinput.Model
err error
}

func initialModelTextInput(prompt string, defaultAnswer string) modelTextInput {
Expand All @@ -38,9 +40,10 @@ func initialModelTextInput(prompt string, defaultAnswer string) modelTextInput {
ti.SetValue(defaultAnswer)

return modelTextInput{
prompt: prompt,
textInput: ti,
err: nil,
windowWidth: 0,
prompt: prompt,
textInput: ti,
err: nil,
}
}

Expand All @@ -52,6 +55,8 @@ func (m modelTextInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) { //nolint: ire
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.windowWidth = msg.Width
case tea.KeyMsg: //nolint: exhaustive
switch msg.Type {
case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEsc:
Expand All @@ -71,9 +76,9 @@ func (m modelTextInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) { //nolint: ire
}

func (m modelTextInput) View() string {
return fmt.Sprintf(
return wordwrap.String(fmt.Sprintf(
m.prompt+"\n\n%s\n\n%s",
m.textInput.View(),
"(esc to quit)",
) + "\n"
)+"\n", m.windowWidth)
}
2 changes: 1 addition & 1 deletion src/secguro/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.10.0
github.com/codeclysm/extract/v3 v3.1.1
github.com/muesli/reflow v0.3.0
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/urfave/cli/v2 v2.27.1
)
Expand All @@ -25,7 +26,6 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
Loading