first part done

This commit is contained in:
Jeff Carr 2025-03-26 03:36:05 -05:00
parent 2775e36aa4
commit 7a68c6247a
1 changed files with 23 additions and 12 deletions

View File

@ -27,13 +27,15 @@ func protoReformat(filename string) error {
}
var inMessage bool
var curmsg []string
var newfile string
var fmtmsg *FormatMsg
fmtmsg = new(FormatMsg)
linesIter = makeLineIter(data)
var bigName int64
var bigType int64
// gets the max vartype and varname
for line := range linesIter {
if strings.HasPrefix(line, "message ") {
@ -44,9 +46,14 @@ func protoReformat(filename string) error {
// find the end of the message
if strings.HasPrefix(line, "}") {
inMessage = false
formatMessage(curmsg)
// formatMessage(curmsg)
formatMessage2(fmtmsg)
curmsg = nil
if bigName < fmtmsg.MaxVarname {
bigName = fmtmsg.MaxVarname
}
if bigType < fmtmsg.MaxVartype {
bigType = fmtmsg.MaxVartype
}
fmtmsg = new(FormatMsg)
continue
}
@ -55,10 +62,14 @@ func protoReformat(filename string) error {
if !inMessage {
continue
}
curmsg = append(curmsg, line)
fmtmsg.Lines = append(fmtmsg.Lines, line)
}
maxVarname = int(bigName)
maxVartype = int(bigType)
var curmsg []string
// gets the max vartype and varname
for line := range linesIter {
if strings.HasPrefix(line, "message ") {
@ -71,7 +82,7 @@ func protoReformat(filename string) error {
// hack to actually indent comments on the message line itself. you're welcome
start := parts[0] + " " + parts[1] + " " + parts[2]
end := strings.Join(parts[3:], " ")
offset := maxVarname + maxVartype + 16 - len(start)
offset := int(bigName) + int(bigType) + 16 - len(start)
pad := fmt.Sprintf("%d", offset)
hmm := "%s %" + pad + "s %s"
line = fmt.Sprintf(hmm, start, " ", end)
@ -218,11 +229,11 @@ func formatMessage2(curmsg *FormatMsg) []string {
}
vartype, varname, _, _ := tokenMsgVar(line)
if len(vartype) > maxVartype {
maxVartype = len(vartype)
if len(vartype) > int(curmsg.MaxVartype) {
curmsg.MaxVartype = int64(len(vartype))
}
if len(varname) > maxVarname {
maxVarname = len(varname)
if len(varname) > int(curmsg.MaxVarname) {
curmsg.MaxVarname = int64(len(varname))
}
}
@ -233,14 +244,14 @@ func formatMessage2(curmsg *FormatMsg) []string {
continue
}
if strings.HasPrefix(line, "//") {
pad := fmt.Sprintf("%d", maxVartype+maxVarname+21)
pad := fmt.Sprintf("%d", curmsg.MaxVartype+curmsg.MaxVarname+21)
hmm := "%" + pad + "s %s"
line = fmt.Sprintf(hmm, " ", line) // todo: compute 50
newmsg = append(newmsg, line)
continue
}
mt := fmt.Sprintf("%d", maxVartype)
mv := fmt.Sprintf("%d", maxVarname)
mt := fmt.Sprintf("%d", curmsg.MaxVartype)
mv := fmt.Sprintf("%d", curmsg.MaxVarname)
hmm := " %-" + mt + "s %-" + mv + "s = %-3s %s"