Browse Source

add cache size to config

Benton Edmondson 1 year ago
parent
commit
647e82ef07
2 changed files with 5 additions and 3 deletions
  1. 4 2
      config/config.go
  2. 1 1
      jtp/jtp.go

+ 4 - 2
config/config.go

@@ -26,6 +26,7 @@ type Config struct {
 	} `toml:"style"`
 	Network   struct {
 		Timeout time.Duration `toml:"timeout_seconds"`
+		CacheSize int `toml:"cache_size"`
 	} `toml:"network"`
 }
 
@@ -43,7 +44,7 @@ func init() {
 		os.Stderr.WriteString(fmt.Errorf("failed to parse %s: %w", location, err).Error() + "\n")
 		os.Exit(1)
 	}
-	if err = normalize(Parsed); err != nil {
+	if err = postprocess(Parsed); err != nil {
 		os.Stderr.WriteString(fmt.Errorf("failed to parse %s: %w", location, err).Error() + "\n")
 		os.Exit(1)
 	}
@@ -60,6 +61,7 @@ func parse(location string) (*Config, error) {
 	config.Style.Colors.Highlight = "#0d7d00"
 	config.Style.Colors.Code = "#4b4b4b"
 	config.Network.Timeout = 10
+	config.Network.CacheSize = 128
 
 	if location == "" {
 		return config, nil
@@ -119,7 +121,7 @@ func hexToAnsi(text string) (string, error) {
 	return strconv.Itoa(int(r)) + ";" + strconv.Itoa(int(g)) + ";" + strconv.Itoa(int(b)), nil
 }
 
-func normalize(config *Config) error {
+func postprocess(config *Config) error {
 	var err error
 	config.Style.Colors.Primary, err = hexToAnsi(config.Style.Colors.Primary)
 	if err != nil {

+ 1 - 1
jtp/jtp.go

@@ -25,7 +25,7 @@ type bundle struct {
 	err    error
 }
 
-var cache, _ = lru.New[string, bundle](128)
+var cache, _ = lru.New[string, bundle](config.Parsed.Network.CacheSize)
 
 var mediaTypeRegexp = regexp.MustCompile(`(?s)^(([!#$%&'*+\-.^_\x60|~a-zA-Z0-9]+)/([!#$%&'*+\-.^_\x60|~a-zA-Z0-9]+)).*$`)
 var statusLineRegexp = regexp.MustCompile(`^HTTP/1\.[0-9] ([0-9]{3}).*\n$`)