chore: import official kitty sources from upstream

Bring the kovidgoyal/kitty codebase in as the base of FWS-Kitty, vendored at upstream commit 91fb38a without its git history.
This commit is contained in:
BlackAngelTV committed 2026-11-04 11:35:59 +01:00
commit 3d174c45a4
1134 files changed
+433629

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
package humanize
import (
"fmt"
"math"
"strconv"
"strings"
"golang.org/x/exp/constraints"
)
// IEC Sizes.
// kibis of bits
const (
Byte = 1 << (iota * 10)
KiByte
MiByte
GiByte
TiByte
PiByte
EiByte
)
// SI Sizes.
const (
IByte = 1
KByte = IByte * 1000
MByte = KByte * 1000
GByte = MByte * 1000
TByte = GByte * 1000
PByte = TByte * 1000
EByte = PByte * 1000
)
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
func humanize_bytes(s uint64, base float64, sizes []string, sep string) string {
if s < 10 {
return fmt.Sprintf("%d%sB", s, sep)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := math.Floor(float64(s)/math.Pow(base, e)*10+0.5) / 10
f := "%.0f%s%s"
if val < 10 {
f = "%.1f%s%s"
}
return fmt.Sprintf(f, val, sep, suffix)
}
// Bytes produces a human readable representation of an SI size.
// Bytes(82854982) -> 83 MB
func Bytes(s uint64) string {
return Size(s, SizeOptions{})
}
// IBytes produces a human readable representation of an IEC size.
// IBytes(82854982) -> 79 MiB
func IBytes(s uint64) string {
return Size(s, SizeOptions{Base: 1024})
}
type SizeOptions struct {
Separator string
Base int
}
func Size[T constraints.Integer | constraints.Float](s T, opts ...SizeOptions) string {
var o SizeOptions
prefix := ""
if len(opts) == 0 {
o = SizeOptions{}
} else {
o = opts[0]
}
if s < 0 {
prefix = "-"
}
if o.Separator == "" {
o.Separator = " "
}
if o.Base == 0 {
o.Base = 1000
}
var sizes []string
switch o.Base {
default:
sizes = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB"}
case 1024:
sizes = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
}
return prefix + humanize_bytes(uint64(s), float64(o.Base), sizes, o.Separator)
}
func FormatNumber[T constraints.Float](n T, max_num_of_decimals ...int) string {
prec := 2
if len(max_num_of_decimals) > 0 {
prec = max_num_of_decimals[0]
}
ans := strconv.FormatFloat(float64(n), 'f', prec, 64)
return strings.TrimRight(strings.TrimRight(ans, "0"), ".")
}
+167
View File
@@ -0,0 +1,167 @@
package humanize
import (
"fmt"
"github.com/kovidgoyal/kitty/tools/wcswidth"
"math"
"sort"
"strings"
"time"
)
// Seconds-based time units
const (
Day = 24 * time.Hour
Week = 7 * Day
Month = 30 * Day
Year = 12 * Month
LongTime = 37 * Year
)
// Time formats a time into a relative string.
//
// Time(someT) -> "3 weeks ago"
func Time(then time.Time) string {
return RelTime(then, time.Now(), "ago", "from now")
}
// A RelTimeMagnitude struct contains a relative time point at which
// the relative format of time will switch to a new format string. A
// slice of these in ascending order by their "D" field is passed to
// CustomRelTime to format durations.
//
// The Format field is a string that may contain a "%s" which will be
// replaced with the appropriate signed label (e.g. "ago" or "from
// now") and a "%d" that will be replaced by the quantity.
//
// The DivBy field is the amount of time the time difference must be
// divided by in order to display correctly.
//
// e.g. if D is 2*time.Minute and you want to display "%d minutes %s"
// DivBy should be time.Minute so whatever the duration is will be
// expressed in minutes.
type RelTimeMagnitude struct {
D time.Duration
Format string
DivBy time.Duration
}
var defaultMagnitudes = []RelTimeMagnitude{
{time.Second, "now", time.Second},
{2 * time.Second, "1 second %s", 1},
{time.Minute, "%d seconds %s", time.Second},
{2 * time.Minute, "1 minute %s", 1},
{time.Hour, "%d minutes %s", time.Minute},
{2 * time.Hour, "1 hour %s", 1},
{Day, "%d hours %s", time.Hour},
{2 * Day, "1 day %s", 1},
{Week, "%d days %s", Day},
{2 * Week, "1 week %s", 1},
{Month, "%d weeks %s", Week},
{2 * Month, "1 month %s", 1},
{Year, "%d months %s", Month},
{18 * Month, "1 year %s", 1},
{2 * Year, "2 years %s", 1},
{LongTime, "%d years %s", Year},
{math.MaxInt64, "a long while %s", 1},
}
// RelTime formats a time into a relative string.
//
// It takes two times and two labels. In addition to the generic time
// delta string (e.g. 5 minutes), the labels are used applied so that
// the label corresponding to the smaller time is applied.
//
// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"
func RelTime(a, b time.Time, albl, blbl string) string {
return CustomRelTime(a, b, albl, blbl, defaultMagnitudes)
}
// CustomRelTime formats a time into a relative string.
//
// It takes two times two labels and a table of relative time formats.
// In addition to the generic time delta string (e.g. 5 minutes), the
// labels are used applied so that the label corresponding to the
// smaller time is applied.
func CustomRelTime(a, b time.Time, albl, blbl string, magnitudes []RelTimeMagnitude) string {
lbl := albl
diff := b.Sub(a)
if a.After(b) {
lbl = blbl
diff = a.Sub(b)
}
n := sort.Search(len(magnitudes), func(i int) bool {
return magnitudes[i].D > diff
})
if n >= len(magnitudes) {
n = len(magnitudes) - 1
}
mag := magnitudes[n]
args := []any{}
escaped := false
for _, ch := range mag.Format {
if escaped {
switch ch {
case 's':
args = append(args, lbl)
case 'd':
args = append(args, diff/mag.DivBy)
}
escaped = false
} else {
escaped = ch == '%'
}
}
return fmt.Sprintf(mag.Format, args...)
}
func optional_cut(x string, sep string) (string, string) {
a, b, found := strings.Cut(x, sep)
if found {
return a, b
}
return "00", a
}
func zero_pad(x string) string {
if len(x) < 2 {
x = strings.Repeat("0", 2-len(x)) + x
}
return x
}
// Render the duration in exactly 8 visual chars
func ShortDuration(val time.Duration) (ans string) {
if val >= time.Second {
if val > Day {
days := int(val.Hours() / 24)
if days > 99 {
ans = `∞`
} else {
if days == 1 {
ans = ">1 day"
} else {
ans = fmt.Sprintf(">%d days", int(days))
}
}
} else {
ans = val.String()
hr, rest := optional_cut(ans, `h`)
min, rest := optional_cut(rest, `m`)
secs, _, _ := strings.Cut(rest, ".")
secs = strings.Replace(secs, `s`, ``, 1)
ans = zero_pad(hr) + `:` + zero_pad(min) + `:` + zero_pad(secs)
}
} else {
ans = "<1 sec"
}
if w := wcswidth.Stringwidth(ans); w < 8 {
ans = strings.Repeat(" ", 8-w) + ans
} else if w > 8 {
ans = wcswidth.TruncateToVisualLength(ans, 8)
}
return
}
+35
View File
@@ -0,0 +1,35 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package humanize
import (
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
var _ = fmt.Print
func TestShortDuration(t *testing.T) {
q := func(i float64, e string) {
d := time.Duration(i * float64(time.Second))
if diff := cmp.Diff(e, ShortDuration(d)); diff != "" {
t.Fatalf("Failed for %f (%s): %s", i, d, diff)
}
}
q(0.1, " <1 sec")
q(1, `00:00:01`)
q(1.1234567, `00:00:01`)
q(60.1234567, `00:01:00`)
q(61.1234567, `00:01:01`)
q(3600, `01:00:00`)
q(3601.1234567, `01:00:01`)
day := 24. * 3600.
q(day, "24:00:00")
q(day+1, " >1 day")
q(day*2, " >2 days")
q(day*23, ">23 days")
q(day*999, " ∞")
}