fix cutting utf-8 strings #82

This commit is contained in:
piotr 2023-01-12 02:27:27 +01:00
parent 8ff2d5c89c
commit cedcf8619f
2 changed files with 22 additions and 9 deletions

View File

@ -691,3 +691,19 @@ func mapOutputs() (map[string]*gdk.Monitor, error) {
}
return result, nil
}
// KAdot / https://stackoverflow.com/a/38537764/4040598 - thanks!
func substring(s string, start int, end int) string {
startStrIdx := 0
i := 0
for j := range s {
if i == start {
startStrIdx = j
}
if i == end {
return s[startStrIdx:j]
}
i++
}
return s[startStrIdx:]
}

View File

@ -58,9 +58,8 @@ func setUpPinnedFlowBox() *gtk.FlowBox {
name = entry.Name
}
if len(name) > 20 {
r := []rune(name)
name = string(r[:17])
name = fmt.Sprintf("%s…", name)
r := substring(name, 0, 17)
name = fmt.Sprintf("%s…", string(r))
}
btn.SetLabel(name)
@ -252,9 +251,8 @@ func flowBoxButton(entry desktopEntry) *gtk.Button {
button.SetImagePosition(gtk.POS_TOP)
name := entry.NameLoc
if len(name) > 20 {
r := []rune(name[:17])
name = string(r)
name = fmt.Sprintf("%s…", name)
r := substring(name, 0, 17)
name = fmt.Sprintf("%s…", string(r))
}
button.SetLabel(name)
@ -263,9 +261,8 @@ func flowBoxButton(entry desktopEntry) *gtk.Button {
terminal := entry.Terminal
desc := entry.CommentLoc
if len(desc) > 120 {
r := []rune(desc[:117])
desc = string(r)
desc = fmt.Sprintf("%s…", desc)
r := substring(desc, 0, 117)
desc = fmt.Sprintf("%s…", string(r))
}
button.Connect("button-release-event", func(btn *gtk.Button, e *gdk.Event) bool {
btnEvent := gdk.EventButtonNewFromEvent(e)