mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-27 02:31:45 +02:00
choose_files: start work on image previews
This commit is contained in:
50
kittens/choose_files/image_preview.go
Normal file
50
kittens/choose_files/image_preview.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package choose_files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/kovidgoyal/kitty/tools/disk_cache"
|
||||
"github.com/kovidgoyal/kitty/tools/utils"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
||||
var dc_size atomic.Int64
|
||||
|
||||
var preview_cache = sync.OnceValues(func() (*disk_cache.DiskCache, error) {
|
||||
cdir := utils.CacheDir()
|
||||
cdir = filepath.Join(cdir, "choose-files")
|
||||
return disk_cache.NewDiskCache(cdir, dc_size.Load())
|
||||
})
|
||||
|
||||
type ImagePreview struct {
|
||||
abspath, cache_key string
|
||||
disk_cache *disk_cache.DiskCache
|
||||
}
|
||||
|
||||
func (p ImagePreview) IsValidForColorScheme(bool) bool { return true }
|
||||
|
||||
func (p ImagePreview) Render(h *Handler, x, y, width, height int) {
|
||||
offset := 0
|
||||
offset += h.render_wrapped_text_in_region("Rendering image, please wait...", x, y, width, height, true)
|
||||
}
|
||||
|
||||
func NewImagePreview(abspath string, metadata fs.FileInfo, opts Settings) (Preview, error) {
|
||||
dc_size.Store(opts.DiskCacheSize())
|
||||
ans := &ImagePreview{abspath: abspath}
|
||||
if dc, err := preview_cache(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
ans.disk_cache = dc
|
||||
}
|
||||
if key, err := disk_cache.KeyForPath(abspath); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
ans.cache_key = key
|
||||
}
|
||||
return ans, nil
|
||||
}
|
||||
@@ -123,6 +123,7 @@ type State struct {
|
||||
display_title bool
|
||||
pygments_style, dark_pygments_style string
|
||||
syntax_aliases map[string]string
|
||||
max_disk_cache_size int64
|
||||
|
||||
selections []string
|
||||
current_idx CollectionIndex
|
||||
@@ -131,6 +132,7 @@ type State struct {
|
||||
redraw_needed bool
|
||||
}
|
||||
|
||||
func (s State) DiskCacheSize() int64 { return s.max_disk_cache_size }
|
||||
func (s State) HighlightStyles() (string, string) { return s.pygments_style, s.dark_pygments_style }
|
||||
func (s State) SyntaxAliases() map[string]string { return s.syntax_aliases }
|
||||
func (s State) DisplayTitle() bool { return s.display_title }
|
||||
@@ -718,6 +720,7 @@ func (h *Handler) set_state_from_config(conf *Config, opts *Options) (err error)
|
||||
h.state.pygments_style = conf.Pygments_style
|
||||
h.state.dark_pygments_style = conf.Dark_pygments_style
|
||||
h.state.syntax_aliases = conf.Syntax_aliases
|
||||
h.state.max_disk_cache_size = int64(conf.Cache_size * (1024 * 1024 * 1024))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,11 @@ builtin styles <https://pygments.org/styles/>` for a list of schemes.
|
||||
This sets the colors used for dark color schemes, use :opt:`pygments_style` to change the
|
||||
colors for light color schemes.''')
|
||||
|
||||
opt('cache_size', '0.5', option_type='positive_float', long_text='''
|
||||
The maximum size of the disk cache, in gigabytes, used for previews. Zero or negative values
|
||||
mean no limit.
|
||||
''')
|
||||
|
||||
opt('syntax_aliases', 'pyj:py pyi:py recipe:py', ctype='strdict_ _:', option_type='syntax_aliases',
|
||||
long_text='''
|
||||
File extension aliases for syntax highlight. For example, to syntax highlight
|
||||
|
||||
@@ -297,6 +297,13 @@ func (pm *PreviewManager) preview_for(abspath string, ftype fs.FileMode) (ans Pr
|
||||
pm.highlight_file_async(abspath, ch)
|
||||
return NewTextFilePreview(abspath, s, ch, pm.highlighter.Sanitize)
|
||||
}
|
||||
if strings.HasPrefix(mt, "image/") {
|
||||
ans, err := NewImagePreview(abspath, s, pm.settings)
|
||||
if err != nil {
|
||||
return NewErrorPreview(err)
|
||||
}
|
||||
return ans
|
||||
}
|
||||
return NewFileMetadataPreview(abspath, s)
|
||||
}
|
||||
|
||||
|
||||
@@ -634,6 +634,7 @@ type Settings interface {
|
||||
GlobalIgnores() ignorefiles.IgnoreFile
|
||||
HighlightStyles() (string, string)
|
||||
SyntaxAliases() map[string]string
|
||||
DiskCacheSize() int64
|
||||
}
|
||||
|
||||
type ResultManager struct {
|
||||
|
||||
Reference in New Issue
Block a user