-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Refactor align converter #623
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -360,10 +360,15 @@ const QuitSmallCountDown = 10 | |||||||||||||||||||||||||||||
// when the debug flag is enabled. | ||||||||||||||||||||||||||||||
const MaxWriteLog int = 10 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
// The name of the converter that can be specified. | ||||||||||||||||||||||||||||||
const ( | ||||||||||||||||||||||||||||||
esConv = "es" | ||||||||||||||||||||||||||||||
rawConv = "raw" | ||||||||||||||||||||||||||||||
alignConv = "align" | ||||||||||||||||||||||||||||||
esConv string = "es" // esConv processes escape sequence(default). | ||||||||||||||||||||||||||||||
rawConv string = "raw" // rawConv is displayed without processing escape sequences as they are. | ||||||||||||||||||||||||||||||
alignConv string = "align" // alignConv is aligned in each column. | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
const ( | ||||||||||||||||||||||||||||||
generalName string = "general" | ||||||||||||||||||||||||||||||
Comment on lines
+365
to
+371
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually constants are sharing a common prefix, allowing to find the easily when using completion in the IDE, they are also ordered in the godoc if they are exported
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your comment. |
||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
var Shrink rune = '…' | ||||||||||||||||||||||||||||||
|
@@ -729,7 +734,7 @@ func (root *Root) setCaption() { | |||||||||||||||||||||||||||||
// setViewModeConfig sets view mode config. | ||||||||||||||||||||||||||||||
func (root *Root) setViewModeConfig() { | ||||||||||||||||||||||||||||||
list := make([]string, 0, len(root.Config.Mode)+1) | ||||||||||||||||||||||||||||||
list = append(list, "general") | ||||||||||||||||||||||||||||||
list = append(list, generalName) | ||||||||||||||||||||||||||||||
for name := range root.Config.Mode { | ||||||||||||||||||||||||||||||
list = append(list, name) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,8 +5,8 @@ import ( | |||||||||
"errors" | ||||||||||
"log" | ||||||||||
"math" | ||||||||||
"reflect" | ||||||||||
"regexp" | ||||||||||
"slices" | ||||||||||
"sort" | ||||||||||
"strconv" | ||||||||||
"time" | ||||||||||
|
@@ -120,18 +120,19 @@ func (root *Root) setAlignConverter() { | |||||||||
maxWidths, addRight = m.maxColumnWidths(maxWidths, addRight, ln) | ||||||||||
} | ||||||||||
|
||||||||||
if !reflect.DeepEqual(m.alignConv.maxWidths, maxWidths) { | ||||||||||
if !slices.Equal(m.alignConv.maxWidths, maxWidths) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based of the fact the if starts here and ends with the function, you could do this
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, that's right. |
||||||||||
m.alignConv.orgWidths = m.columnWidths | ||||||||||
m.alignConv.maxWidths = maxWidths | ||||||||||
m.alignConv.rightAlign = make([]bool, len(maxWidths)+1) | ||||||||||
for n := len(m.alignConv.shrink); n < len(maxWidths)+1; n++ { | ||||||||||
m.alignConv.shrink = append(m.alignConv.shrink, false) | ||||||||||
} | ||||||||||
for n := len(m.alignConv.shrink); n < len(maxWidths)+1; n++ { | ||||||||||
for n := len(m.alignConv.rightAlign); n < len(maxWidths)+1; n++ { | ||||||||||
m.alignConv.rightAlign = append(m.alignConv.rightAlign, false) | ||||||||||
} | ||||||||||
for i := 0; i < len(addRight); i++ { | ||||||||||
m.alignConv.rightAlign[i] = addRight[i] > 1 | ||||||||||
if !m.alignConv.rightAlign[i] { | ||||||||||
m.alignConv.rightAlign[i] = addRight[i] > 1 | ||||||||||
} | ||||||||||
} | ||||||||||
m.ClearCache() | ||||||||||
} | ||||||||||
|
@@ -200,37 +201,33 @@ func maxWidthsWidth(lc contents, maxWidths []int, widths []int, rightCount []int | |||||||||
return maxWidths, rightCount | ||||||||||
} | ||||||||||
|
||||||||||
// trimWidth returns the column width and 1 if the column is right-aligned. | ||||||||||
func trimWidth(lc contents) (int, int) { | ||||||||||
l := trimLeft(lc) | ||||||||||
r := trimRight(lc) | ||||||||||
|
||||||||||
l, r := trimmedIndices(lc) | ||||||||||
addRight := 0 | ||||||||||
if l > len(lc)-r { | ||||||||||
addRight = 1 | ||||||||||
} | ||||||||||
return r - l, addRight | ||||||||||
} | ||||||||||
|
||||||||||
func trimLeft(lc contents) int { | ||||||||||
// trimmedIndices returns the number of left spaces. | ||||||||||
func trimmedIndices(lc contents) (int, int) { | ||||||||||
ts := 0 | ||||||||||
for i := 0; i < len(lc); i++ { | ||||||||||
if lc[i].mainc != ' ' { | ||||||||||
ts = i | ||||||||||
break | ||||||||||
} | ||||||||||
} | ||||||||||
return ts | ||||||||||
} | ||||||||||
func trimRight(lc contents) int { | ||||||||||
te := len(lc) | ||||||||||
for i := len(lc) - 1; i >= 0; i-- { | ||||||||||
if lc[i].mainc != ' ' { | ||||||||||
te = i + 1 | ||||||||||
break | ||||||||||
} | ||||||||||
} | ||||||||||
return te | ||||||||||
|
||||||||||
return ts, te | ||||||||||
} | ||||||||||
|
||||||||||
// shiftBody shifts the section header so that it is not hidden by it. | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't rename them in the same logic as
#623 (comment)
But why not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This depends on the naming rules of the function name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I said, why not 😁