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

Add a converter specified shortcut #625

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,11 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&nonMatchFilter, "non-match-filter", "", "", "filter non match search pattern")
rootCmd.PersistentFlags().BoolVarP(&oviewer.SkipExtract, "skip-extract", "", false, "skip extracting compressed files")

rootCmd.PersistentFlags().BoolVarP(&oviewer.AlignF, "align", "", false, "align column")
rootCmd.PersistentFlags().BoolVarP(&oviewer.RawF, "raw", "", false, "raw output of escape sequences")

// Config.General
rootCmd.PersistentFlags().StringP("converter", "", "es", "converter [es|raw]")
rootCmd.PersistentFlags().StringP("converter", "", "es", "converter [es|raw|align]")
_ = viper.BindPFlag("general.Converter", rootCmd.PersistentFlags().Lookup("converter"))
_ = rootCmd.RegisterFlagCompletionFunc("converter", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"es\tEscape Sequence", "raw\tRaw output of escape sequences", "align\tAlign Column Widths"}, cobra.ShellCompDirectiveNoFileComp
Expand Down
13 changes: 13 additions & 0 deletions oviewer/oviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ const (

var Shrink rune = '…'

// RawF is specifies converter shortcut for raw.
var RawF bool

// AlignF is specifies converter shortcut for align.
var AlignF bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// RawF is specifies converter shortcut for raw.
var RawF bool
// AlignF is specifies converter shortcut for align.
var AlignF bool
// RawF specifies converter shortcut for raw.
var RawF bool
// AlignF specifies converter shortcut for align.
var AlignF bool

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need tgem to be exported


var (
// ErrOutOfRange indicates that value is out of range.
ErrOutOfRange = errors.New("out of range")
Expand Down Expand Up @@ -700,6 +706,13 @@ func (root *Root) prepareRun(ctx context.Context) error {
root.Screen.EnableMouse(MouseFlags)
}

if RawF {
root.General.Converter = convRaw
}
if AlignF {
root.General.Converter = convAlign
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic is strange to read.

So AlignF "wins" vs RawF when present, right?

Maybe this?

Suggested change
if RawF {
root.General.Converter = convRaw
}
if AlignF {
root.General.Converter = convAlign
}
if AlignF {
root.General.Converter = convAlign
} else if RawF {
root.General.Converter = convRaw
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe using

https://pkg.go.dev/github.com/spf13/pflag#Set

So set the converter flag?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thank you for your comment.
I moved to the main side and fixed it.


if root.Config.ShrinkChar != "" {
Shrink = []rune(root.Config.ShrinkChar)[0]
}
Expand Down