Also load ~/.config/kitty/mime.types in Go code
This commit is contained in:
parent
22d562ca41
commit
f070b17fee
@ -20,13 +20,13 @@ from kitty.cli import (
|
|||||||
parse_option_spec,
|
parse_option_spec,
|
||||||
serialize_as_go_string,
|
serialize_as_go_string,
|
||||||
)
|
)
|
||||||
|
from kitty.guess_mime_type import text_mimes
|
||||||
from kitty.key_encoding import config_mod_map
|
from kitty.key_encoding import config_mod_map
|
||||||
from kitty.key_names import character_key_name_aliases, functional_key_name_aliases
|
from kitty.key_names import character_key_name_aliases, functional_key_name_aliases
|
||||||
from kitty.options.types import Options
|
from kitty.options.types import Options
|
||||||
from kitty.rc.base import RemoteCommand, all_command_names, command_for_name
|
from kitty.rc.base import RemoteCommand, all_command_names, command_for_name
|
||||||
from kitty.remote_control import global_options_spec
|
from kitty.remote_control import global_options_spec
|
||||||
from kitty.rgb import color_names
|
from kitty.rgb import color_names
|
||||||
from kitty.guess_mime_type import text_mimes
|
|
||||||
|
|
||||||
changed: List[str] = []
|
changed: List[str] = []
|
||||||
|
|
||||||
|
|||||||
@ -3,17 +3,62 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
|
var user_mime_only_once sync.Once
|
||||||
|
var user_defined_mime_map map[string]string
|
||||||
|
|
||||||
|
func load_mime_file(filename string, mime_map map[string]string) error {
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
fields := strings.Fields(scanner.Text())
|
||||||
|
if len(fields) <= 1 || fields[0][0] == '#' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
mime_type := fields[0]
|
||||||
|
for _, ext := range fields[1:] {
|
||||||
|
if ext[0] == '#' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
mime_map["."+ext] = mime_type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func load_user_mime_maps() {
|
||||||
|
conf_path := filepath.Join(ConfigDir(), "mime.types")
|
||||||
|
err := load_mime_file(conf_path, user_defined_mime_map)
|
||||||
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
|
fmt.Fprintln(os.Stderr, "Failed to parse", conf_path, "for MIME types with error:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GuessMimeType(filename string) string {
|
func GuessMimeType(filename string) string {
|
||||||
|
user_mime_only_once.Do(load_user_mime_maps)
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
mime_with_parameters := mime.TypeByExtension(ext)
|
mime_with_parameters := user_defined_mime_map[ext]
|
||||||
|
if mime_with_parameters == "" {
|
||||||
|
mime_with_parameters = mime.TypeByExtension(ext)
|
||||||
|
}
|
||||||
if mime_with_parameters == "" {
|
if mime_with_parameters == "" {
|
||||||
only_once.Do(set_builtins)
|
only_once.Do(set_builtins)
|
||||||
mime_with_parameters = builtin_types_map[ext]
|
mime_with_parameters = builtin_types_map[ext]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user