-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
289 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"math" | ||
"os/user" | ||
"strconv" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/expr-lang/expr" | ||
"github.com/expr-lang/expr/vm" | ||
) | ||
|
||
func compile(code string) *vm.Program { | ||
p, err := expr.Compile(code, expr.Env(Env{})) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return p | ||
} | ||
|
||
type Env struct { | ||
Files []fs.DirEntry `expr:"files"` | ||
CurrentFile fs.DirEntry `expr:"current_file"` | ||
} | ||
|
||
func (e Env) Sprintf(format string, a ...any) string { | ||
return fmt.Sprintf(format, a...) | ||
} | ||
|
||
func (e Env) PadLeft(s string, n int) string { | ||
return fmt.Sprintf("%*s", n, s) | ||
} | ||
|
||
func (e Env) PadRight(s string, n int) string { | ||
return fmt.Sprintf("%*s", n, s) | ||
} | ||
|
||
func (e Env) Size() string { | ||
info, err := e.CurrentFile.Info() | ||
if err != nil { | ||
return "N/A" | ||
} | ||
size := float64(info.Size()) | ||
if size == 0 { | ||
return "0B" | ||
} | ||
units := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} | ||
base := math.Log(size) / math.Log(1024) | ||
unitIndex := int(math.Floor(base)) | ||
if unitIndex >= len(units) { | ||
unitIndex = len(units) - 1 | ||
} | ||
value := size / math.Pow(1024, float64(unitIndex)) | ||
if unitIndex == 0 { | ||
return fmt.Sprintf("%.0f%s", value, units[unitIndex]) | ||
} | ||
return fmt.Sprintf("%.1f%s", value, units[unitIndex]) | ||
} | ||
|
||
func (e Env) Mode() string { | ||
info, err := e.CurrentFile.Info() | ||
if err != nil { | ||
return "?????????" | ||
} | ||
|
||
mode := info.Mode() | ||
|
||
result := make([]byte, 10) | ||
|
||
switch { | ||
case mode.IsDir(): | ||
result[0] = 'd' | ||
case mode&fs.ModeSymlink != 0: | ||
result[0] = 'l' | ||
case mode&fs.ModeSocket != 0: | ||
result[0] = 's' | ||
case mode&fs.ModeNamedPipe != 0: | ||
result[0] = 'p' | ||
case mode&fs.ModeCharDevice != 0: | ||
result[0] = 'c' | ||
case mode&fs.ModeDevice != 0: | ||
result[0] = 'b' | ||
default: | ||
result[0] = '-' | ||
} | ||
|
||
// Owner permissions | ||
result[1] = permBit(mode&0400, 'r') | ||
result[2] = permBit(mode&0200, 'w') | ||
result[3] = permBit(mode&0100, 'x') | ||
|
||
// Group permissions | ||
result[4] = permBit(mode&040, 'r') | ||
result[5] = permBit(mode&020, 'w') | ||
result[6] = permBit(mode&010, 'x') | ||
|
||
// Others permissions | ||
result[7] = permBit(mode&04, 'r') | ||
result[8] = permBit(mode&02, 'w') | ||
result[9] = permBit(mode&01, 'x') | ||
|
||
// Handle special permission bits | ||
if mode&fs.ModeSetuid != 0 { | ||
result[3] = 's' | ||
} | ||
if mode&fs.ModeSetgid != 0 { | ||
result[6] = 's' | ||
} | ||
if mode&fs.ModeSticky != 0 { | ||
result[9] = 't' | ||
} | ||
|
||
return string(result) | ||
} | ||
|
||
func (e Env) ModTime() string { | ||
info, err := e.CurrentFile.Info() | ||
if err != nil { | ||
return "???" | ||
} | ||
modTime := info.ModTime() | ||
now := time.Now() | ||
if modTime.Year() == now.Year() { | ||
return modTime.Format("Jan 2 15:04") | ||
} | ||
return modTime.Format("Jan 2 2006") | ||
} | ||
|
||
func (e Env) Owner() (string, error) { | ||
fileInfo, err := e.CurrentFile.Info() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
stat, ok := fileInfo.Sys().(*syscall.Stat_t) | ||
if !ok { | ||
return "", errors.New("unsupported platform") | ||
} | ||
|
||
uidStr := strconv.FormatUint(uint64(stat.Uid), 10) | ||
gidStr := strconv.FormatUint(uint64(stat.Gid), 10) | ||
|
||
username := uidStr | ||
if userInfo, err := user.LookupId(uidStr); err == nil { | ||
username = userInfo.Username | ||
} | ||
|
||
groupname := gidStr | ||
if groupInfo, err := user.LookupGroupId(gidStr); err == nil { | ||
groupname = groupInfo.Name | ||
} | ||
|
||
return fmt.Sprintf("%s %s", username, groupname), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"io/fs" | ||
"testing" | ||
) | ||
|
||
type mockFile struct { | ||
fs.DirEntry | ||
mode fs.FileMode | ||
size int64 | ||
} | ||
|
||
type mockFileInfo struct { | ||
fs.FileInfo | ||
mode fs.FileMode | ||
size int64 | ||
} | ||
|
||
func (m mockFile) Info() (fs.FileInfo, error) { | ||
return mockFileInfo{ | ||
size: m.size, | ||
mode: m.mode, | ||
}, nil | ||
} | ||
|
||
func (m mockFileInfo) Mode() fs.FileMode { return m.mode } | ||
|
||
func (m mockFileInfo) Size() int64 { return m.size } | ||
|
||
func TestFileSize(t *testing.T) { | ||
testCases := []struct { | ||
size int64 | ||
expected string | ||
}{ | ||
{0, "0B"}, | ||
{500, "500B"}, | ||
{1024, "1.0KB"}, | ||
{1500, "1.5KB"}, | ||
{1024 * 1024, "1.0MB"}, | ||
{1024 * 1024 * 1024, "1.0GB"}, | ||
{1024 * 1024 * 1024 * 1024, "1.0TB"}, | ||
} | ||
|
||
env := Env{} | ||
for _, tc := range testCases { | ||
mockFile := mockFile{size: tc.size} | ||
result := env.FileSize(mockFile) | ||
if result != tc.expected { | ||
t.Errorf("Failed: %v != %v", result, tc.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestFileMode(t *testing.T) { | ||
testCases := []struct { | ||
mode fs.FileMode | ||
expected string | ||
}{ | ||
{0755 | fs.ModeDir, "drwxr-xr-x"}, // Directory | ||
{0644, "-rw-r--r--"}, // Regular file | ||
{0777 | fs.ModeSetuid, "-rwsrwxrwx"}, // SetUID | ||
{0777 | fs.ModeSetgid, "-rwxrwsrwx"}, // SetGID | ||
{0777 | fs.ModeSticky, "-rwxrwxrwt"}, // Sticky bit | ||
{fs.ModeSymlink | 0777, "lrwxrwxrwx"}, // Symbolic link | ||
{fs.ModeSocket | 0777, "srwxrwxrwx"}, // Socket | ||
{fs.ModeNamedPipe | 0777, "prwxrwxrwx"}, // Named pipe | ||
{fs.ModeDevice | fs.ModeCharDevice | 0777, "crwxrwxrwx"}, // Character device | ||
{fs.ModeDevice | 0777, "brwxrwxrwx"}, // Block device | ||
} | ||
|
||
env := Env{} | ||
for _, tc := range testCases { | ||
mockFile := mockFile{mode: tc.mode} | ||
result := env.FileMode(mockFile) | ||
if result != tc.expected { | ||
t.Errorf("Failed: %v != %v", result, tc.expected) | ||
} | ||
} | ||
} |
Oops, something went wrong.