Skip to content

Commit

Permalink
Change the way I store hidden columns
Browse files Browse the repository at this point in the history
Prior to this change, I copied Wireshark and kept a hidden-columns key
in the toml file, with the column expressed using its tshark spec
e.g. "%Yut", "%Cus:tcp.port:0:R", ... But this doesn't distinguish
between two columns that have the same spec - should the user want to
configure columns that way. Wireshark has this problem too; if you have
two identical columns, hide one, then quit and restart, both columns
with the same spec are hidden. I suppose this means Wireshark added the
gui.columns.hidden field after gui.columns.format was established. Now,
instead of storing a termshark column spec as the tshark spec and its
display name, I use a trip - tshark spec, display name, and
visibility. Therefore, if the user has 5 columns configured, the
column-format string list in the toml file will be 15 elements long.
  • Loading branch information
gcla committed Feb 26, 2021
1 parent db2dafb commit 4cf8025
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 53 deletions.
42 changes: 17 additions & 25 deletions shark/columnformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func GetPsmlColumnFormatCached() []PsmlColumnSpec {
defer cachedPsmlColumnFormatMutex.Unlock()

if cachedPsmlColumnFormat == nil {
cachedPsmlColumnFormat = getPsmlColumnFormatWithoutLock("main.column-format", "main.hidden-columns")
cachedPsmlColumnFormat = getPsmlColumnFormatWithoutLock("main.column-format")
}

return cachedPsmlColumnFormat
Expand All @@ -284,29 +284,30 @@ func GetPsmlColumnFormat() []PsmlColumnSpec {
cachedPsmlColumnFormatMutex.Lock()
defer cachedPsmlColumnFormatMutex.Unlock()

cachedPsmlColumnFormat = getPsmlColumnFormatWithoutLock("main.column-format", "main.hidden-columns")
cachedPsmlColumnFormat = getPsmlColumnFormatWithoutLock("main.column-format")

return cachedPsmlColumnFormat
}

func GetPsmlColumnFormatFrom(colKey string, visKey string) []PsmlColumnSpec {
return getPsmlColumnFormatWithoutLock(colKey, visKey)
func GetPsmlColumnFormatFrom(colKey string) []PsmlColumnSpec {
return getPsmlColumnFormatWithoutLock(colKey)
}

func getPsmlColumnFormatWithoutLock(colKey string, visKey string) []PsmlColumnSpec {
func getPsmlColumnFormatWithoutLock(colKey string) []PsmlColumnSpec {
res := make([]PsmlColumnSpec, 0)
widths := termshark.ConfStringSlice(colKey, []string{})
if len(widths) == 0 || (len(colKey)/2)*2 != len(colKey) {
if len(widths) == 0 || (len(colKey)/3)*3 != len(colKey) {
res = DefaultPsmlColumnSpec
} else {
// Cross references with those column specs that we know about from having
// queried tshark with tshark -G column-formats. Any that are not known
// are discarded. If none are left, use our safe defaults
pieces := [2]string{}
pieces := [3]string{}

for i := 0; i < len(widths); i += 2 {
for i := 0; i < len(widths); i += 3 {
pieces[0] = widths[i]
pieces[1] = widths[i+1]
pieces[2] = widths[i+2]

var spec PsmlColumnSpec

Expand All @@ -327,30 +328,21 @@ func getPsmlColumnFormatWithoutLock(colKey string, visKey string) []PsmlColumnSp
// Already confirmed it's in map
spec.Name = AllowedColumnFormats[pieces[0]].Short
}

visible, err := strconv.ParseBool(pieces[2])
if err != nil {
logrus.Warnf("Do not understand PSML column format hidden token '%s' - skipping its use", pieces[2])
continue
}
spec.Hidden = !visible

res = append(res, spec)
}
if len(res) == 0 {
logrus.Warnf("No configured PSML column formats were understood. Using safe default")
res = DefaultPsmlColumnSpec
}

vis := termshark.ConfStringSlice(visKey, []string{})
for _, v := range vis {
var f PsmlField
err := f.FromString(v)
if err != nil {
logrus.Warnf("Ignoring unparsable hidden column '%s'", v)
continue
}
loop:
for i, _ := range res {
// Compare the whole field i.e. "%Cus:<filter>:0:R"
if res[i].Field == f {
res[i].Hidden = true
break loop
}
}
}
}
return res
}
Expand Down
15 changes: 1 addition & 14 deletions ui/psmlcols.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func openEditColumns(app gowid.IApp) {
if len(bakCols) != 0 {
btn := button.New(text.New("Restore"))
btn.OnClick(gowid.MakeWidgetCallback("cb", func(app gowid.IApp, widget gowid.IWidget) {
newPcols := NewPsmlColumnsModelFrom("main.column-format-bak", "main.hidden-columns-bak", app)
newPcols := NewPsmlColumnsModelFrom("main.column-format-bak", app)
if len(newPcols.spec) == 0 {
OpenMessage("Error: backup column-format is empty in toml file", appView, app)
return
Expand Down Expand Up @@ -300,25 +300,12 @@ func openEditColumns(app gowid.IApp) {
newcols := pcols.ToConfigList()
curcols := termshark.ConfStringSlice("main.column-format", []string{})

newvis := pcols.HiddenToConfigList()
curvis := termshark.ConfStringSlice("main.hidden-columns", []string{})

updated := false
if !reflect.DeepEqual(newcols, curcols) {
termshark.SetConf("main.column-format-bak", curcols)
termshark.SetConf("main.column-format", newcols)
updated = true
}
if !reflect.DeepEqual(newvis, curvis) {
termshark.SetConf("main.hidden-columns-bak", curvis)
termshark.SetConf("main.hidden-columns", newvis)
updated = true
}

err := pcols.Close()
if err != nil {
log.Warnf("Unexpected result closing PSML columns dialog: %v", err)
}

editColsDialog.Close(app)

Expand Down
17 changes: 3 additions & 14 deletions ui/psmlcolsmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func NewPsmlColumnsModel(app gowid.IApp) *psmlColumnsModel {
return res
}

func NewPsmlColumnsModelFrom(colsKey string, visKey string, app gowid.IApp) *psmlColumnsModel {
spec := shark.GetPsmlColumnFormatFrom(colsKey, visKey)
func NewPsmlColumnsModelFrom(colsKey string, app gowid.IApp) *psmlColumnsModel {
spec := shark.GetPsmlColumnFormatFrom(colsKey)
res := &psmlColumnsModel{
spec: spec,
}
Expand Down Expand Up @@ -140,18 +140,7 @@ func (p *psmlColumnsModel) ToConfigList() []string {
for i := 0; i < len(p.spec); i++ {
res = append(res, p.FieldToString(i))
res = append(res, p.widgets[i].customName.Text())
}
return res
}

// HiddenToConfigList returns a list of hidden fields, according to the current
// PSML columns model (from the open dialog)
func (p *psmlColumnsModel) HiddenToConfigList() []string {
res := make([]string, 0)
for i := 0; i < len(p.spec); i++ {
if !p.widgets[i].visible.IsChecked() {
res = append(res, p.FieldToString(i))
}
res = append(res, fmt.Sprintf("%v", p.widgets[i].visible.IsChecked()))
}
return res
}
Expand Down

0 comments on commit 4cf8025

Please sign in to comment.