Skip to content

Commit

Permalink
Add file create and modify time in selection query
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Jun 20, 2024
1 parent 8152ebe commit 4c1b4fd
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions Wox.UI.Flutter/wox/lib/components/wox_preview_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class _WoxPreviewViewState extends State<WoxPreviewView> {
final allCodeLanguages = {
...allLanguages,
"txt": Mode(),
"conf": Mode(),
"js": javascript,
"ts": typescript,
"yml": yaml,
Expand Down
5 changes: 5 additions & 0 deletions Wox/plugin/system/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ func (i *SelectionPlugin) queryForFile(ctx context.Context, filePath string) (re
Preview: plugin.WoxPreview{
PreviewType: plugin.WoxPreviewTypeFile,
PreviewData: filePath,
PreviewProperties: map[string]string{
"Created At": util.GetFileCreatedAt(filePath),
"Modified At": util.GetFileModifiedAt(filePath),
"Size": util.GetFileSize(filePath),
},
},
})

Expand Down
42 changes: 42 additions & 0 deletions Wox/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/samber/lo"
"os"
"path/filepath"
"strconv"
"strings"
)

Expand Down Expand Up @@ -41,3 +42,44 @@ func IsImageFile(path string) bool {
imageSuffixList := []string{".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".tiff", ".svg"}
return lo.Contains(imageSuffixList, currentExt)
}

func GetFileModifiedAt(path string) string {
stat, err := os.Stat(path)
if err != nil {
return "-"
}

return FormatTime(stat.ModTime())
}

func GetFileSize(path string) string {
stat, err := os.Stat(path)
if err != nil {
return "-"
}

//if size is less than 1KB, show bytes
//if size is less than 1MB, show KB
//if size is less than 1GB, show MB
//if size is less than 1TB, show GB
//if size is less than 1PB, show TB

size := stat.Size()
if size < 1024 {
return strconv.FormatInt(size, 10) + " B"
}
if size < 1024*1024 {
return strconv.FormatInt(size/1024, 10) + " KB"
}
if size < 1024*1024*1024 {
return strconv.FormatInt(size/1024/1024, 10) + " MB"
}
if size < 1024*1024*1024*1024 {
return strconv.FormatInt(size/1024/1024/1024, 10) + " GB"
}
if size < 1024*1024*1024*1024*1024 {
return strconv.FormatInt(size/1024/1024/1024/1024, 10) + " TB"
}

return "-"
}
19 changes: 19 additions & 0 deletions Wox/util/file_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package util

import (
"os"
"syscall"
"time"
)

func GetFileCreatedAt(path string) string {
stat, err := os.Stat(path)
if err != nil {
return "-"
}

statSys := stat.Sys().(*syscall.Stat_t)
creationTime := time.Unix(statSys.Birthtimespec.Sec, int64(statSys.Birthtimespec.Nsec))

return FormatTime(creationTime)
}
5 changes: 5 additions & 0 deletions Wox/util/file_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package util

func GetFileCreatedAt(path string) string {
return "-"
}
19 changes: 19 additions & 0 deletions Wox/util/file_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package util

import (
"os"
"syscall"
"time"
)

func GetFileCreatedAt(path string) string {
stat, err := os.Stat(path)
if err != nil {
return "-"
}

statSys := stat.Sys().(*syscall.Win32FileAttributeData)
creationTime := time.Unix(0, statSys.CreationTime.Nanoseconds())

return FormatTime(creationTime)
}

0 comments on commit 4c1b4fd

Please sign in to comment.