diff --git a/Makefile b/Makefile index e44149f..6947403 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ run: build .PHONY: web web: npm run build - # npm run js-dev + npm run js-dev .PHONY: build build: web diff --git a/file.go b/file.go index a9dd62c..c6134ff 100644 --- a/file.go +++ b/file.go @@ -50,34 +50,32 @@ func (f *File) Write(data []byte) error { } func (f *File) Do() (string, error) { - log.Println("do ", f.Action) if f.Action == "" { return "", nil } - timeout := time.After(2 * time.Second) cmd := &exec.Cmd{} if runtime.GOOS == "windows" { cmd = exec.Command(WIN_SHELL, "/c", f.Action) } else { - exec.Command(UNIX_SHELL, "-c", f.Action) + cmd = exec.Command(UNIX_SHELL, "-c", f.Action) } + log.Println("DO: ", f.Action) done := make(chan string, 1) - log.Println("start ", f.Action) go func() { out, _ := cmd.CombinedOutput() - // real cmd err is only pass down + // real cmd err is unhandled, but passed to client // if err != nil { // return string(out), err // } done <- string(out) }() select { - case <-timeout: + case <-time.After(5 * time.Second): cmd.Process.Kill() log.Println("timeout") return "", errors.New("command timeout") case out := <-done: - log.Println(out) + log.Println("\n", out) return out, nil } } diff --git a/public/js/back.js b/public/js/back.js new file mode 100644 index 0000000..18be59e --- /dev/null +++ b/public/js/back.js @@ -0,0 +1,104 @@ + + +window.ToolIsFollow = false; + +async function FileGet(follower=false){ + let f = '' + if (Active == 'ConfigUI') { + if (follower) f = '?f=true' + const res = await fetch('/api/conf'+f) + .catch(err=>{console.log(err);return;}); + const body = await res.text(); + if(!res.ok){ + Catch(res) + return + } + editor.session.setValue(body); + } + else { + if (follower) f = '&f=true' + const res = await fetch('/api/file?name=' + Active + f) + .catch(err=>{console.log(err);return;}); + const body = await res.json(); + if(!res.ok){ + Catch(res) + return + } + editor.session.setValue(body.data); + } +} + +async function FileSave(){ + if (Active == 'ConfigUI') { + const res = await fetch('/api/conf', { + method: 'POST', + body: editor.getValue(), + headers: new Headers({ + 'Content-Type': 'application/json' + }) + }).catch(err=>{console.log(err);return;}); + if (res.ok) window.location.reload(); + else Catch(res) + } + else { + const res = await fetch('/api/file', { + method: 'POST', + body: JSON.stringify({"name":Active,"data":editor.getValue()}), + headers: new Headers({ + 'Content-Type': 'application/json' + }) + }).catch(err=>{console.log(err);return;}); + if(!res.ok) Catch(res) + } +} + +async function FileApply(){ + if (Active == 'ConfigUI') { + return; + } + else { + const res = await fetch('/api/apply?name='+ Active, { + method: 'POST', + }).catch(err=>{console.log(err);return;}); + if(!res.ok){ + const result = await Catch(res) + result_editor.session.setValue(result) + return + } + const result = await res.text() + result_editor.session.setValue(result) + } +} + +async function Catch(res){ + console.trace() + console.log(res) + const msg = await res.text() + ShowError(msg) + return msg +} + +// starting point +(function(){ + // block ctl-s + window.addEventListener("keypress", function(event) { + if (!(event.code == 115 && event.ctrlKey) && !(event.code == 19)) return true + alert("Ctrl-S pressed") + event.preventDefault() + return false + }) + + // setup ace editor + setEditor() + + // setup result code block + setResult() + + // for follow mode + setInterval((async ()=>{ + if (ToolIsFollow){ + await FileGet(true) + editor.gotoLine(editor.session.getLength()); + } + }), 1000) +}()) \ No newline at end of file diff --git a/src/main.js b/src/main.js index 83b2515..acdf802 100644 --- a/src/main.js +++ b/src/main.js @@ -2,11 +2,12 @@ window.ToolIsFollow = false; -async function FileGet(follower=false){ +FileGet= async function(follower=false){ let f = '' if (Active == 'ConfigUI') { if (follower) f = '?f=true' - const res = await fetch('/api/conf'+f); + const res = await fetch('/api/conf'+f) + .catch(err=>{console.log(err);return;}); const body = await res.text(); if(!res.ok){ Catch(res) @@ -16,7 +17,8 @@ async function FileGet(follower=false){ } else { if (follower) f = '&f=true' - const res = await fetch('/api/file?name=' + Active + f); + const res = await fetch('/api/file?name=' + Active + f) + .catch(err=>{console.log(err);return;}); const body = await res.json(); if(!res.ok){ Catch(res) @@ -26,7 +28,7 @@ async function FileGet(follower=false){ } } -async function FileSave(){ +FileSave = async function(){ if (Active == 'ConfigUI') { const res = await fetch('/api/conf', { method: 'POST', @@ -34,7 +36,7 @@ async function FileSave(){ headers: new Headers({ 'Content-Type': 'application/json' }) - }) + }).catch(err=>{console.log(err);return;}); if (res.ok) window.location.reload(); else Catch(res) } @@ -45,19 +47,19 @@ async function FileSave(){ headers: new Headers({ 'Content-Type': 'application/json' }) - }) + }).catch(err=>{console.log(err);return;}); if(!res.ok) Catch(res) } } -async function FileApply(){ +FileApply = async function(){ if (Active == 'ConfigUI') { return; } else { const res = await fetch('/api/apply?name='+ Active, { method: 'POST', - }) + }).catch(err=>{console.log(err);return;}); if(!res.ok){ const result = await Catch(res) result_editor.session.setValue(result) @@ -68,7 +70,7 @@ async function FileApply(){ } } -async function Catch(res){ +Catch=async function(res){ console.trace() console.log(res) const msg = await res.text() @@ -91,4 +93,12 @@ async function Catch(res){ editor.gotoLine(editor.session.getLength()); } }), 1000) + + // block ctl-s + window.addEventListener("keypress", function(event) { + if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true + alert("Ctrl-S pressed") + event.preventDefault() + return false + }) }()) \ No newline at end of file diff --git a/src/main.scss b/src/main.scss index e5f6736..e238586 100644 --- a/src/main.scss +++ b/src/main.scss @@ -22,7 +22,7 @@ $modal-content-width: 90vw; } .icn-spinner { - animation: spin-animation 0.5s infinite; + animation: spin-animation 1s infinite; display: inline-block; } diff --git a/templates/base/footer.tmpl b/templates/base/footer.tmpl index 551912f..164e080 100644 --- a/templates/base/footer.tmpl +++ b/templates/base/footer.tmpl @@ -1,7 +1,8 @@ {{define "base/footer"}} - + + {{end}} \ No newline at end of file diff --git a/templates/home.tmpl b/templates/home.tmpl index 0cf8e71..f3e198b 100644 --- a/templates/home.tmpl +++ b/templates/home.tmpl @@ -29,6 +29,5 @@ var Active = "{{.File.Name}}"; {{template "components/result" .}} - {{template "base/footer" .}} {{end}} \ No newline at end of file