Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
Sciroccogti committed May 6, 2020
1 parent 3fcc652 commit 62522fe
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 91 deletions.
2 changes: 1 addition & 1 deletion assets.go

Large diffs are not rendered by default.

110 changes: 32 additions & 78 deletions foldest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,128 +4,82 @@ package main

import (
"fmt"
"foldest-go/lorca"
"foldest-go/utils"
"log"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"sync"

"foldest-go/lorca"
)

type start struct {
sync.Mutex
isStart bool
ui lorca.UI
}

func (s *start) Start() {
s.ui.Eval(`console.log("OHHHHHHHHHH!");`)
s.Lock()
defer s.Unlock()
exitsignal := make(chan bool)
s.isStart = true
go mainThread(s, exitsignal)
<-exitsignal
s.ui.Eval(`console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!");`)
s.isStart = false
}

func (s *start) Stop() {
s.Lock()
defer s.Unlock()
s.isStart = false
}

func (s *start) Status() (status bool) {
s.Lock()
defer s.Unlock()
return s.isStart
}

func (s *start) Output() (output string) {
s.Lock()
defer s.Unlock()
return "haha!\n"
}

func main() {
args := []string{}
if runtime.GOOS == "linux" {
args = append(args, "--class=Lorca")
}
s := &start{}
pf := &utils.Front{}
var err error
s.ui, err = lorca.New("", "", 480, 320, args...)
pf.Ui, err = lorca.New("", "", 480, 320, args...)
if err != nil {
log.Fatal(err)
}
defer s.ui.Close()
defer pf.Ui.Close()

stdout := make(chan string, 10)
utils.Plog.Init(&stdout)
// A simple way to know when UI is ready (uses body.onload event in JS)
s.ui.Bind("start", func() {
pf.Ui.Bind("start", func() {
log.Println("UI is ready")
utils.Plog.Print("UI is ready\n")
})

// Create and bind Go object to the UI
s.ui.Bind("mainStart", s.Start)
// ui.Bind("mainStop", s.Stop)
s.ui.Bind("mainStatus", s.Status)
s.ui.Bind("getoutput", s.Output)
pf.Ui.Bind("mainStart", pf.Start)
// Ui.Bind("mainStop", pf.Stop)
pf.Ui.Bind("mainStatus", pf.Status)
pf.Ui.Bind("getline", utils.Plog.Getline)

// Load HTML.
// You may also use `data:text/html,<base64>` approach to load initial HTML,
// e.g: ui.Load("data:text/html," + url.PathEscape(html))
// e.g: Ui.Load("data:text/html," + url.PathEscape(html))

ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
go http.Serve(ln, http.FileServer(FS))
s.ui.Load(fmt.Sprintf("http://%s", ln.Addr()))
pf.Ui.Load(fmt.Sprintf("http://%s", ln.Addr()))

// You may use console.log to debug your JS code, it will be printed via
// log.Println(). Also exceptions are printed in a similar manner.
s.ui.Eval(`
console.log("Hello, world!");
console.log('Multiple values:', [1, false, {"x":5}]);
`)
pf.Ui.Eval(`
console.log("Hello, world!");
console.log('Multiple values:', [1, false, {"x":5}]);
`)

// Wait until the interrupt signal arrives or browser window is closed
sigc := make(chan os.Signal)
signal.Notify(sigc, os.Interrupt)
select {
case <-sigc:
case <-s.ui.Done():
case <-pf.Ui.Done():
}

log.Println("exiting...")
}

func mainThread(s *start, exitsignal chan<- bool) {
s.ui.Eval(`console.log("Starting...");`)
fmt.Println("Starting...")
conf := utils.ReadConf()

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

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

fmt.Println("Exiting...")
s.ui.Eval(`console.log("Exiting...");`)
exitsignal <- true
// stdout := make(chan string, 10)
// utils.Plog.Init(&stdout)
// go func() {
// for {
// utils.Plog.Print("hoho\n")
// utils.Plog.Print("hehe\n")
// }

// }()
// for i := 0; i < 100; i++ {
// lines := utils.Plog.Getline()
// fmt.Printf(lines)
// }
}
6 changes: 3 additions & 3 deletions utils/classify.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

// ReadRules : Read rules.yml
func ReadRules() (rules *Rules) {
fmt.Println("Reading conf.yml ...")
Plog.Print("Reading rules.yml ...\n")
rules = new(Rules)

if _, err := os.Stat("rules.yml"); os.IsNotExist(err) {
fmt.Println("rules.yml not found, skipping ...")
Plog.Print("rules.yml not found, skipping ...\n")
return nil
}

Expand Down Expand Up @@ -57,7 +57,7 @@ func DoClassify(rules *Rules, path string, isVerbose bool) {

// doRule :
func doRule(rule *Rule, path string, isVerbose bool) {
fmt.Printf("Performing rule %c[0;33m%s%c[0m ...\n", 0x1B, rule.Name, 0x1B)
Plog.Print("Performing rule %c[0;33m%s%c[0m ...\n", 0x1B, rule.Name, 0x1B)
if !strings.HasSuffix(rule.Name, "/") {
rule.Name = rule.Name + "/"
}
Expand Down
8 changes: 4 additions & 4 deletions utils/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

// ReadConf : Read conf.yml
func ReadConf() (conf *Conf) {
fmt.Println("Reading conf.yml ...")
Plog.Print("Reading conf.yml ...\n")
conf = new(Conf)
if _, err := os.Stat("conf.yml"); os.IsNotExist(err) {
fmt.Println("conf.yml not found, starting with default value ...")
Plog.Print("conf.yml not found, starting with default value ...\n")
} else {
yamlFile, err := ioutil.ReadFile("conf.yml")
if err != nil {
Expand Down Expand Up @@ -48,7 +48,7 @@ func SetPath(path *string) (isChanged bool) {

for {
if *path == "" {
fmt.Println("Please input path of the target folder:")
Plog.Print("Please input path of the target folder:\n")
fmt.Scanln(path)
isChanged = true
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func SetDefault(conf *Conf) {

// SaveConf : Save the conf.yml
func SaveConf(conf *Conf) {
fmt.Println("Saving conf.yml ...")
Plog.Print("Saving conf.yml ...\n")
yamlChanged, err := yaml.Marshal(conf)
if err != nil {
fmt.Printf("Error while saving conf.yml :\n")
Expand Down
58 changes: 58 additions & 0 deletions utils/front.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package utils

import (
"foldest-go/lorca"
"sync"
)

type Front struct {
sync.Mutex
isStart bool
Ui lorca.UI
}

func mainThread(pf *Front, exitsignal chan<- bool) {
pf.Ui.Eval(`console.log("Starting...");`)
Plog.Print("Starting...\n")
conf := ReadConf()

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

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

Plog.Print("Exiting...\n")
pf.Ui.Eval(`console.log("Exiting...");`)
exitsignal <- true
}

func (pf *Front) Start() {
pf.Lock()
defer pf.Unlock()
exitsignal := make(chan bool)
pf.isStart = true
go mainThread(pf, exitsignal)
<-exitsignal
pf.isStart = false
}

func (pf *Front) Stop() {
pf.Lock()
defer pf.Unlock()
pf.isStart = false
}

func (pf *Front) Status() (status bool) {
// pf.Lock()
// defer pf.Unlock()
return pf.isStart
}
54 changes: 54 additions & 0 deletions utils/logjs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package utils

import (
"fmt"
"regexp"
"sync"
)

// Logjs : a log method for js to read
type Logjs struct {
// Buf *bytes.Buffer
buf []byte
stdout *chan string
}

// Plog : global pointer to Logjs
var Plog *Logjs

// mu : instance mutex
var mu sync.Mutex

// Init : Init Logjs
func (pl *Logjs) Init(std *chan string) {
mu.Lock()
defer mu.Unlock()

if Plog == nil {
Plog = &Logjs{}
}
Plog.stdout = std
// Plog.buf []byte = make([]byte, 4096)
}

// Print : Print a string to Logjs
func (pl *Logjs) Print(format string, a ...interface{}) {
// mu.Lock()
// defer mu.Unlock()
s := fmt.Sprintf(format, a...)
end := regexp.MustCompile(`.*?%c[0;\dm.*?(%c[0m)`)
s = end.ReplaceAllString(s, "</span>")
red := regexp.MustCompile(`.*?(%c[0;31m).*?%c[0m`)
s = red.ReplaceAllString(s, "<span style=\"color: red;\">")
*Plog.stdout <- s
// Plog.Buf.WriteString(line)
}

// Getline : Get a string from Logjs
func (pl *Logjs) Getline() (lines string) {
// mu.Lock()
// defer mu.Unlock()

// lines, _ = Plog.Buf.ReadString('\n')
return <-*Plog.stdout
}
11 changes: 6 additions & 5 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@

.output-inner {
padding-right: 17px;
border: 2px solid black;
border: 2px solid darkslategrey;
height: 600px;
background-color: black;
background-color: darkslategrey;
color: white;
font-size: small;
}
Expand All @@ -102,11 +102,11 @@
</div>
<div class="output-outer">
<div class="output-inner">
Welcome to Foldest, Press the button to start.<br>
<!-- Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae nemo, sed porro, officiis, commodi sunt mollitia nihil deleniti perspiciatis repellat facere excepturi laborum. Esse iusto, quam repudiandae consequatur doloremque, id! -->
</div>
</div>
</div>

<!-- Connect UI actions to Go functions -->
<script>
const panel = document.querySelector('.panel');
Expand All @@ -123,7 +123,9 @@
panel.innerText = `Exited`;
document.getElementsByClassName("btn-start").disabled = false;
}
inner.innerText += `${await window.getoutput()}`;
inner.innerText += `${await window.getline()}`;
render();
console.log("render")
// counter.innerText = `Count: ${await window.counterValue()}`;
};

Expand All @@ -136,7 +138,6 @@
// await counterAdd(-1); // Call Go function
// render();
// });

render();
</script>
</body>
Expand Down

0 comments on commit 62522fe

Please sign in to comment.