~delthas/senpai-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH] Use current locale to render dates

Details
Message ID
<20230825080427.756-2-hugo@whynothugo.nl>
DKIM signature
pass
Download raw message
Patch: +28 -69
Use the user's current locale to format dates (e.g.: based on LC_ALL or
LC_TIME). Given that dates have variable lengths, move them onto their
own separate line.
---
 go.mod           |  2 +-
 go.sum           | 13 ++++++++--
 ui/buffers.go    | 20 +++++++++-------
 ui/draw_utils.go | 62 ++++--------------------------------------------
 4 files changed, 28 insertions(+), 69 deletions(-)

diff --git a/go.mod b/go.mod
index 22e4cc0..fa0d016 100644
--- a/go.mod
+++ b/go.mod
@@ -4,8 +4,8 @@ go 1.16

require (
	git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc
	github.com/InstaConsultInc/lctime v0.1.1-0.20221116042817-914fb56b1292
	github.com/delthas/go-libnp v0.0.0-20221222161248-0e45ece1f878
	github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118
	github.com/gdamore/tcell/v2 v2.6.1-0.20230327043120-47ec3a77754f
	github.com/mattn/go-runewidth v0.0.14
	golang.org/x/net v0.0.0-20220722155237-a158d28d115b
diff --git a/go.sum b/go.sum
index af42d20..0a6b008 100644
--- a/go.sum
+++ b/go.sum
@@ -1,19 +1,22 @@
git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc h1:51BD67xFX+bozd3ZRuOUfalrhx4/nQSh6A9lI08rYOk=
git.sr.ht/~emersion/go-scfg v0.0.0-20201019143924-142a8aa629fc/go.mod h1:t+Ww6SR24yYnXzEWiNlOY0AFo5E9B73X++10lrSpp4U=
github.com/InstaConsultInc/lctime v0.1.1-0.20221116042817-914fb56b1292 h1:XO9FWPMlH1rs0IWsuEttzbXd6ARk5J/TLtSDXto8HZc=
github.com/InstaConsultInc/lctime v0.1.1-0.20221116042817-914fb56b1292/go.mod h1:hhdEfoW0/oWNJTP4MChwTYgQGTE2MdvKdVx2/2Wuslc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/delthas/go-libnp v0.0.0-20221222161248-0e45ece1f878 h1:v8W8eW7eb2bHFXBA80UKcoe0TvEu46NlTHSDRvgAbMU=
github.com/delthas/go-libnp v0.0.0-20221222161248-0e45ece1f878/go.mod h1:aGVXnhWpDlt5U4SphG97o1gszctZKvBTXy320E8Buw4=
github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118 h1:Xzf9ra1QRJXD62gwudjI2iBq7x9CusvHd83Dg2OnUmE=
github.com/delthas/go-localeinfo v0.0.0-20221116001557-686a1e185118/go.mod h1:sG54BxlyQgIskYURLrg7mvhoGBe0Qq12DNtYRALwNa4=
github.com/delthas/tcell/v2 v2.4.1-0.20230710100648-1489e78d90fb h1:x0hrYPzXpmn3L/4QnW0UXJnHX9oz0OcZNcsSgregusw=
github.com/delthas/tcell/v2 v2.4.1-0.20230710100648-1489e78d90fb/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/jteeuwen/go-bindata v3.0.7+incompatible/go.mod h1:JVvhzYOiGBnFSYRyV00iY8q7/0PThjIYav1p9h5dmKs=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
@@ -22,6 +25,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@@ -50,6 +55,7 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 h1:Vv0JUPWTyeqUq42B2WJ1FeIDjjvGKoA2Ss+Ts0lAVbs=
@@ -58,7 +64,10 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
mvdan.cc/xurls/v2 v2.3.0 h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=
mvdan.cc/xurls/v2 v2.3.0/go.mod h1:AjuTy7gEiUArFMjgBBDU4SMxlfUYsRokpJQgNWOt3e4=
diff --git a/ui/buffers.go b/ui/buffers.go
index de0ee90..63fa5b5 100644
--- a/ui/buffers.go
+++ b/ui/buffers.go
@@ -881,15 +881,8 @@ func (bs *BufferList) DrawTimeline(screen tcell.Screen, x0, y0, nickColWidth int
			ya, ma, da := b.lines[i].At.Local().Date()
			showDate = yb != ya || mb != ma || dd != da
		}
		if showDate {
			st := tcell.StyleDefault.Bold(true)
			// as a special case, always draw the first visible message date, even if it is a continuation line
			yd := yi
			if yd < y0 {
				yd = y0
			}
			printDate(screen, x0, yd, st, line.At.Local())
		} else if b.lines[i-1].At.Truncate(time.Minute) != line.At.Truncate(time.Minute) && yi >= y0 {

		if i> 0 && b.lines[i-1].At.Truncate(time.Minute) != line.At.Truncate(time.Minute) && yi >= y0 {
			st := tcell.StyleDefault.Foreground(tcell.ColorGray)
			printTime(screen, x0, yi, st, line.At.Local())
		}
@@ -929,6 +922,15 @@ func (bs *BufferList) DrawTimeline(screen tcell.Screen, x0, y0, nickColWidth int
			}
			x += runeWidth(r)
		}
		if showDate {
			yi--
			st := tcell.StyleDefault.Bold(true)
			yd := yi
			if yd < y0 {
				yd = y0
			}
			printDate(screen, x0, yd, st, line.At.Local())
		}
	}

	b.isAtTop = y0 <= yi
diff --git a/ui/draw_utils.go b/ui/draw_utils.go
index 62f2d79..65f6939 100644
--- a/ui/draw_utils.go
+++ b/ui/draw_utils.go
@@ -2,11 +2,9 @@ package ui

import (
	"fmt"
	"strings"
	"sync"
	"time"

	"github.com/delthas/go-localeinfo"
	"github.com/InstaConsultInc/lctime"

	"github.com/gdamore/tcell/v2"
)
@@ -46,62 +44,12 @@ func printNumber(screen tcell.Screen, x *int, y int, st tcell.Style, n int) {
	printString(screen, x, y, s)
}

var dateConfig sync.Once
var dateMonthFirst bool

func loadDateInfo() {
	// Very overkill, but I like it :)
	// Try to extract from the user locale whether they'd rather have the date
	// printed as dd/mm or mm/dd.
	// If we're not sure, print dd/mm.
	l, err := localeinfo.NewLocale("")
	if err != nil {
		return
	}
	format := l.DateFormat()
	dayIndex := -1
	for _, s := range []string{"%d", "%e"} {
		dayIndex = strings.Index(format, s)
		if dayIndex >= 0 {
			break
		}
	}
	if dayIndex == -1 {
		return
	}
	monthIndex := -1
	for _, s := range []string{"%m", "%b", "%B"} {
		monthIndex = strings.Index(format, s)
		if monthIndex >= 0 {
			break
		}
	}
	if monthIndex == -1 {
		return
	}
	if monthIndex < dayIndex {
		dateMonthFirst = true
	}
}

func printDate(screen tcell.Screen, x int, y int, st tcell.Style, t time.Time) {
	dateConfig.Do(loadDateInfo)
	_, m, d := t.Date()
	var left, right int
	if dateMonthFirst {
		left, right = int(m), d
	} else {
		left, right = d, int(m)
	formatted := lctime.Strftime("%x", t)

	for index, c := range formatted {
		screen.SetContent(x+index, y, c, nil, st)
	}
	l0 := rune(left/10) + '0'
	l1 := rune(left%10) + '0'
	r0 := rune(right/10) + '0'
	r1 := rune(right%10) + '0'
	screen.SetContent(x+0, y, l0, nil, st)
	screen.SetContent(x+1, y, l1, nil, st)
	screen.SetContent(x+2, y, '/', nil, st)
	screen.SetContent(x+3, y, r0, nil, st)
	screen.SetContent(x+4, y, r1, nil, st)
}

func printTime(screen tcell.Screen, x int, y int, st tcell.Style, t time.Time) {
-- 
2.42.0
Thomas Flament <th.flament@laposte.net>
Details
Message ID
<11a0efe6-cec4-4de5-9d42-ec57b9509906@laposte.net>
In-Reply-To
<20230825080427.756-2-hugo@whynothugo.nl> (view parent)
DKIM signature
pass
Download raw message
Hi,

Thanks for your patch.

However, I'd like to have dates kept very short (5 charts only), and to 
avoid putting dates on their own lines.

Typically on low-traffic channels you can sometimes have 1 date per message.

Just adding the year of dates would not be worth halving the density of 
messages.

-- 

delthas
Reply to thread Export thread (mbox)