Skip to content

Commit

Permalink
Merge pull request #2 from Sciroccogti/astilectron
Browse files Browse the repository at this point in the history
v3.0 Astilectron
  • Loading branch information
Sciroccogti authored Jan 9, 2021
2 parents dae4eb7 + 43b0225 commit 6cc66e8
Show file tree
Hide file tree
Showing 33 changed files with 3,611 additions and 63 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ foldest-linux*
# vendor/

*.code-workspace

output
windows.syso
bind*

*.log
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ rule2:
- [ ] Automatic
- [x] Temp trash bin
- [x] Customize rules

## How to Contribute

```Bash
go get -u github.com/asticode/go-astilectron-bundler/...
go install github.com/asticode/go-astilectron-bundler/astilectron-bundler
astilectron-bundler
```
6 changes: 6 additions & 0 deletions bundler.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"app_name": "Foldest",
"icon_path_darwin": "resources/icon.icns",
"icon_path_linux": "resources/icon.png",
"icon_path_windows": "resources/icon.ico"
}
18 changes: 9 additions & 9 deletions foldest.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package main

import (
"fmt"
"foldest-go/utils"
)

func main() {
// Foldest : main process of foldest
func Foldest() {
conf := utils.ReadConf()
fmt.Println("Press enter to start...")
fmt.Scanln()
// utils.Print("Press enter to start...\n")
// fmt.Scanln()

rules := utils.ReadRules()
if rules == nil {
fmt.Println("Skipping classify...")
utils.Print("Skipping classify...\n")
} else {
utils.DoClassify(rules, conf.Targetdir, conf.Verbose)
}

if conf.Tmpbin.Enable {
fmt.Println("Performing tmpbin...")
utils.Print("Performing tmpbin...\n")
utils.Manage(conf)
} else {
fmt.Println("tmpbin is disabled, skipping...")
utils.Print("tmpbin is disabled, skipping...\n")
}

fmt.Println("Press enter to exit...")
fmt.Scanln()
utils.Print("Finished\n")
return
}
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module foldest-go

go 1.13

require gopkg.in/yaml.v2 v2.2.8
require (
github.com/akavel/rsrc v0.10.1 // indirect
github.com/asticode/go-astichartjs v0.1.0
github.com/asticode/go-astikit v0.13.0
github.com/asticode/go-astilectron v0.20.0
github.com/asticode/go-astilectron-bootstrap v0.4.4
github.com/asticode/go-astilectron-bundler v0.7.3 // indirect
gopkg.in/yaml.v2 v2.2.8
)
25 changes: 25 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/akavel/rsrc v0.10.1 h1:hCCPImjmFKVNGpeLZyTDRHEFC283DzyTXTo0cO0Rq9o=
github.com/akavel/rsrc v0.10.1/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/asticode/go-astichartjs v0.1.0 h1:Dscf4R+KDdBRJY1MQlK5p2IZyLc/JmnODUT3O2Cnc1Y=
github.com/asticode/go-astichartjs v0.1.0/go.mod h1:mAFLydbGKvV1fjZJRwl8v/7aKjC0QWyqHtSIk4TGgPo=
github.com/asticode/go-astikit v0.6.0 h1:zmbw5J2Du8SedBG1I5JjlsS5oG2vHcb6ZzhWMCMbLzs=
github.com/asticode/go-astikit v0.6.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astikit v0.13.0 h1:GSKThPQ0UEdsKChq/hNVWKtx9/9mKamggm5G7JQcktY=
github.com/asticode/go-astikit v0.13.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astilectron v0.20.0 h1:HUPl8BPxuRDiOnK4JSpTzJweSNpR85HZUQFopf7MVcM=
github.com/asticode/go-astilectron v0.20.0/go.mod h1:bF7zkk11IwP8lBPhEl3cCtkKIwsRRR6264OFM1v3Yso=
github.com/asticode/go-astilectron-bootstrap v0.4.4 h1:AebHl8ooixuqOgvRz01++l2ki1mSlKeId7OWYjS5Z5M=
github.com/asticode/go-astilectron-bootstrap v0.4.4/go.mod h1:+BSfTf4V8vW27vo4xEwUr4lXTryWL3awdALRSZfy1mM=
github.com/asticode/go-astilectron-bundler v0.7.3 h1:CXQVmSNZ4uE7gkURKqyTtnzKn6nfGGwQrwWeexqeDnw=
github.com/asticode/go-astilectron-bundler v0.7.3/go.mod h1:ZCUI3iEtXQg47aEGKMsVXVQddKF5hOM86GAm3DjiDho=
github.com/asticode/go-bindata v1.0.0 h1:5whO0unjdx2kbAbzoBMS3307jKAEf3oQ1lJcx5RdgA8=
github.com/asticode/go-bindata v1.0.0/go.mod h1:t/Y+/iCLrvaYkv8Y6PscRnyUeYzy9y9+8JC9CMcKdHY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sam-kamerer/go-plister v1.2.0 h1:ZdEF1bhPUoGzwz5eFljw2K/A+oRXq/81jul/A3CHKEY=
github.com/sam-kamerer/go-plister v1.2.0/go.mod h1:gTt1Ko2oTA5bfDYsNcLjRGyyx6LPxHIeo0ZTtTRZG2I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
112 changes: 112 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"foldest-go/utils"
"os"
"time"

"github.com/asticode/go-astikit"
"github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
)

// Constants
const version = `3.0`

// const htmlAbout = `Welcome on <b>Astilectron</b> demo!<br>
// This is using the bootstrap and the bundler.`

// Vars injected via ldflags by bundler
var (
AppName string
BuiltAt string
VersionAstilectron string
VersionElectron string
)

// Application Vars
var (
fs = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
debug = fs.Bool("d", false, "enables the debug mode")
)

func main() {
htmlAbout := `<b>` + AppName + `</b> Ver ` + version + `<br>
Built Time: ` + BuiltAt[:10] + `<br>
Astilectron ` + VersionAstilectron + `<br>
Electron ` + VersionElectron + `<br>
<a href="https://github.com/Sciroccogti/Foldest-go">Github</a>
`

// Parse flags
fs.Parse(os.Args[1:])

// Run bootstrap
utils.L.Printf("Running app built at %s\n", BuiltAt)
if err := bootstrap.Run(bootstrap.Options{
Asset: Asset,
AssetDir: AssetDir,
AstilectronOptions: astilectron.Options{
AppName: AppName,
AppIconDarwinPath: "resources/icon.icns",
AppIconDefaultPath: "resources/icon.png",
SingleInstance: true,
VersionAstilectron: VersionAstilectron,
VersionElectron: VersionElectron,
},
Debug: *debug,
Logger: utils.L,
MenuOptions: []*astilectron.MenuItemOptions{{
Label: astikit.StrPtr("File"),
SubMenu: []*astilectron.MenuItemOptions{
{
Label: astikit.StrPtr("About"),
OnClick: func(e astilectron.Event) (deleteListener bool) {
if err := bootstrap.SendMessage(utils.W, "about", htmlAbout, func(m *bootstrap.MessageIn) {
// Unmarshal payload
var s string
if err := json.Unmarshal(m.Payload, &s); err != nil {
utils.L.Println(fmt.Errorf("unmarshaling payload failed: %w", err))
return
}
utils.L.Printf("About modal has been displayed and payload is %s!\n", s)
}); err != nil {
utils.L.Println(fmt.Errorf("sending about event failed: %w", err))
}
return
},
},
{Role: astilectron.MenuItemRoleClose},
},
}},
OnWait: func(_ *astilectron.Astilectron, ws []*astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
utils.W = ws[0]
go func() {
utils.W.OpenDevTools()

time.Sleep(5 * time.Second)

if err := bootstrap.SendMessage(utils.W, "check.out.menu", "Don't forget to check out the menu!"); err != nil {
utils.L.Println(fmt.Errorf("sending check.out.menu event failed: %w", err))
}
}()
return nil
},
RestoreAssets: RestoreAssets,
Windows: []*bootstrap.Window{{
Homepage: "index.html",
MessageHandler: handleMessages,
Options: &astilectron.WindowOptions{
BackgroundColor: astikit.StrPtr("#333"),
Center: astikit.BoolPtr(true),
Height: astikit.IntPtr(700),
Width: astikit.IntPtr(700),
},
}},
}); err != nil {
utils.L.Fatal(fmt.Errorf("running bootstrap failed: %w", err))
}
}
165 changes: 165 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package main

import (
"encoding/json"
"foldest-go/utils"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"sort"
"strconv"

"github.com/asticode/go-astichartjs"
"github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
)

// handleMessages handles messages
func handleMessages(_ *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
switch m.Name {
case "explore":
// Unmarshal payload
var path string
if len(m.Payload) > 0 {
// Unmarshal payload
if err = json.Unmarshal(m.Payload, &path); err != nil {
payload = err.Error()
return
}
}

// Explore
if payload, err = explore(path); err != nil {
payload = err.Error()
return
}
break
case "start":
utils.L.Print("golang start!\n")
utils.Print("golang start!\n")
Foldest()
payload = "ok!"
return
break
}
return
}

// Exploration represents the results of an exploration
type Exploration struct {
Dirs []Dir `json:"dirs"`
Files *astichartjs.Chart `json:"files,omitempty"`
FilesCount int `json:"files_count"`
FilesSize string `json:"files_size"`
Path string `json:"path"`
}

// Dir represents a dir payload
type Dir struct {
Name string `json:"name"`
Path string `json:"path"`
}

// explore explores a path.
// If path is empty, it explores the user's home directory
func explore(path string) (e Exploration, err error) {
// If no path is provided, use the user's home dir
if len(path) == 0 {
var u *user.User
if u, err = user.Current(); err != nil {
return
}
path = u.HomeDir
}

// Read dir
var files []os.FileInfo
if files, err = ioutil.ReadDir(path); err != nil {
return
}

// Init exploration
e = Exploration{
Dirs: []Dir{},
Path: path,
}

// Add previous dir
if filepath.Dir(path) != path {
e.Dirs = append(e.Dirs, Dir{
Name: "..",
Path: filepath.Dir(path),
})
}

// Loop through files
var sizes []int
var sizesMap = make(map[int][]string)
var filesSize int64
for _, f := range files {
if f.IsDir() {
e.Dirs = append(e.Dirs, Dir{
Name: f.Name(),
Path: filepath.Join(path, f.Name()),
})
} else {
var s = int(f.Size())
sizes = append(sizes, s)
sizesMap[s] = append(sizesMap[s], f.Name())
e.FilesCount++
filesSize += f.Size()
}
}

// Prepare files size
if filesSize < 1e3 {
e.FilesSize = strconv.Itoa(int(filesSize)) + "b"
} else if filesSize < 1e6 {
e.FilesSize = strconv.FormatFloat(float64(filesSize)/float64(1024), 'f', 0, 64) + "kb"
} else if filesSize < 1e9 {
e.FilesSize = strconv.FormatFloat(float64(filesSize)/float64(1024*1024), 'f', 0, 64) + "Mb"
} else {
e.FilesSize = strconv.FormatFloat(float64(filesSize)/float64(1024*1024*1024), 'f', 0, 64) + "Gb"
}

// Prepare files chart
sort.Ints(sizes)
if len(sizes) > 0 {
e.Files = &astichartjs.Chart{
Data: &astichartjs.Data{Datasets: []astichartjs.Dataset{{
BackgroundColor: []string{
astichartjs.ChartBackgroundColorYellow,
astichartjs.ChartBackgroundColorGreen,
astichartjs.ChartBackgroundColorRed,
astichartjs.ChartBackgroundColorBlue,
astichartjs.ChartBackgroundColorPurple,
},
BorderColor: []string{
astichartjs.ChartBorderColorYellow,
astichartjs.ChartBorderColorGreen,
astichartjs.ChartBorderColorRed,
astichartjs.ChartBorderColorBlue,
astichartjs.ChartBorderColorPurple,
},
}}},
Type: astichartjs.ChartTypePie,
}
var sizeOther int
for i := len(sizes) - 1; i >= 0; i-- {
for _, l := range sizesMap[sizes[i]] {
if len(e.Files.Data.Labels) < 4 {
e.Files.Data.Datasets[0].Data = append(e.Files.Data.Datasets[0].Data, sizes[i])
e.Files.Data.Labels = append(e.Files.Data.Labels, l)
} else {
sizeOther += sizes[i]
}
}
}
if sizeOther > 0 {
e.Files.Data.Datasets[0].Data = append(e.Files.Data.Datasets[0].Data, sizeOther)
e.Files.Data.Labels = append(e.Files.Data.Labels, "other")
}
}
return
}
Loading

0 comments on commit 6cc66e8

Please sign in to comment.