update
This commit is contained in:
parent
3f8208d254
commit
54c881b3be
49
calc/calc.go
Normal file
49
calc/calc.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package calc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"b612.me/starlog"
|
||||||
|
"b612.me/staros"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Cmd = &cobra.Command{
|
||||||
|
Use: "calc",
|
||||||
|
Short: "计算器",
|
||||||
|
Long: "简单的计算器",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) == 0 {
|
||||||
|
starlog.Errorln("请至少输入一个算式")
|
||||||
|
return errors.New("no sentense")
|
||||||
|
}
|
||||||
|
var res []string
|
||||||
|
printRes := func() {
|
||||||
|
for k, v := range res {
|
||||||
|
fmt.Println(args[k], "=", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k, v := range args {
|
||||||
|
for i := k; i > 0; i-- {
|
||||||
|
v = strings.ReplaceAll(v, "$"+strconv.Itoa(i), res[i-1])
|
||||||
|
}
|
||||||
|
v = strings.ReplaceAll(v, "x", "*")
|
||||||
|
v = strings.ReplaceAll(v, "X", "*")
|
||||||
|
v = strings.ReplaceAll(v, "×", "*")
|
||||||
|
v = strings.ReplaceAll(v, "÷", "*")
|
||||||
|
v = strings.ReplaceAll(v, "(", "(")
|
||||||
|
v = strings.ReplaceAll(v, ")", ")")
|
||||||
|
c, err := staros.Calc(v)
|
||||||
|
if err != nil {
|
||||||
|
printRes()
|
||||||
|
starlog.Errorf("calc %s Error:%v\n", v, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
res = append(res, strconv.FormatFloat(c, 'f', -1, 64))
|
||||||
|
}
|
||||||
|
printRes()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
@ -30,7 +30,6 @@ var Cmd = &cobra.Command{
|
|||||||
Hostname: ip,
|
Hostname: ip,
|
||||||
Auth: &server.SimpleAuth{Name: username, Password: pwd},
|
Auth: &server.SimpleAuth{Name: username, Password: pwd},
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Starting ftp server on %v:%v", opts.Hostname, opts.Port)
|
log.Printf("Starting ftp server on %v:%v", opts.Hostname, opts.Port)
|
||||||
log.Printf("Username %v, Password %v", username, pwd)
|
log.Printf("Username %v, Password %v", username, pwd)
|
||||||
server := server.NewServer(opts)
|
server := server.NewServer(opts)
|
||||||
|
@ -5,7 +5,7 @@ import "path/filepath"
|
|||||||
func (h *HttpServer) FileType(name string) string {
|
func (h *HttpServer) FileType(name string) string {
|
||||||
ext := filepath.Ext(name)
|
ext := filepath.Ext(name)
|
||||||
if len(ext) == 0 || ext == "." {
|
if len(ext) == 0 || ext == "." {
|
||||||
return "文件"
|
return "其他文件"
|
||||||
}
|
}
|
||||||
ext = ext[1:]
|
ext = ext[1:]
|
||||||
mimeMap := map[string]string{
|
mimeMap := map[string]string{
|
||||||
|
@ -82,11 +82,31 @@ var htmlTitle string = `<!DOCTYPE html>
|
|||||||
padding: 12px;
|
padding: 12px;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
border-bottom: 1px solid #ddd;
|
border-bottom: 1px solid #ddd;
|
||||||
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
th:hover {
|
th[data-sort]:before {
|
||||||
cursor: pointer;
|
content: "▼";
|
||||||
background-color: #ddd;
|
display: inline-block;
|
||||||
|
height: 20px;
|
||||||
|
width: 20px;
|
||||||
|
margin-right: 10px;
|
||||||
|
vertical-align: middle;
|
||||||
|
position: absolute;
|
||||||
|
right: 0;
|
||||||
|
top: 50%%;
|
||||||
|
transform: translateY(-50%%);
|
||||||
|
opacity: 0.3;
|
||||||
|
transition: all 0.2s ease-in-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
th[data-sort].asc:before {
|
||||||
|
content: "▲";
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
th:hover:before {
|
||||||
|
opacity: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.filename {
|
.filename {
|
||||||
@ -112,49 +132,69 @@ var htmlTitle string = `<!DOCTYPE html>
|
|||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th onclick="sortTable(0)">Name</th>
|
<th data-sort="name" class="asc">Name</th>
|
||||||
<th onclick="sortTable(1)">Modified</th>
|
<th data-sort="modified">Modified</th>
|
||||||
<th onclick="sortTable(2)">Size</th>
|
<th data-sort="size">Size</th>
|
||||||
<th onclick="sortTable(3)">Type</th>
|
<th data-sort="type">Type</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>`
|
<tbody>`
|
||||||
|
|
||||||
var htmlTail = ` </tbody>
|
var htmlTail = ` </tbody>
|
||||||
</table>
|
</table>
|
||||||
|
<hr />
|
||||||
|
<pre>
|
||||||
|
<h2 style="text-align: center;">B612.Me © Apache 2.0 License</h2>
|
||||||
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function sortTable(n) {
|
function sortTable(th, n) {
|
||||||
const table = document.querySelector('table');
|
const table = document.querySelector('table');
|
||||||
const rows = table.rows;
|
const rows = table.rows;
|
||||||
let switching = true;
|
let switching = true;
|
||||||
let shouldSwitch = false;
|
let shouldSwitch = false;
|
||||||
let direction = 'asc';
|
let direction = 'asc';
|
||||||
let switchcount = 0;
|
let switchcount = 0;
|
||||||
|
|
||||||
while (switching) {
|
while (switching) {
|
||||||
switching = false;
|
switching = false;
|
||||||
let i;
|
let i;
|
||||||
for (i = 1; i < rows.length - 1; i++) {
|
for (i = 1; i < rows.length - 1; i++) {
|
||||||
shouldSwitch = false;
|
shouldSwitch = false;
|
||||||
|
|
||||||
const x = rows[i].getElementsByTagName("td")[n];
|
const x = rows[i].getElementsByTagName("td")[n];
|
||||||
const y = rows[i + 1].getElementsByTagName("td")[n];
|
const y = rows[i + 1].getElementsByTagName("td")[n];
|
||||||
|
|
||||||
|
let xValue, yValue;
|
||||||
|
if (n === 2) { // size sorting
|
||||||
|
if (x.innerText==="-") {
|
||||||
|
xValue=-1;
|
||||||
|
}else{
|
||||||
|
xValue = parseInt(x.innerText.split(' ')[0]);
|
||||||
|
}
|
||||||
|
if (y.innerText==="-") {
|
||||||
|
yValue=-1;
|
||||||
|
}else{
|
||||||
|
yValue = parseInt(y.innerText.split(' ')[0]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
xValue = x.innerText.toLowerCase();
|
||||||
|
yValue = y.innerText.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
if (direction === 'asc') {
|
if (direction === 'asc') {
|
||||||
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
|
if (xValue > yValue) {
|
||||||
shouldSwitch = true;
|
shouldSwitch = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (direction === 'desc') {
|
} else if (direction === 'desc') {
|
||||||
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
|
if (xValue < yValue) {
|
||||||
shouldSwitch = true;
|
shouldSwitch = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldSwitch) {
|
if (shouldSwitch) {
|
||||||
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
|
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
|
||||||
switching = true;
|
switching = true;
|
||||||
@ -166,6 +206,51 @@ var htmlTail = ` </tbody>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update sort class
|
||||||
|
const ths = table.getElementsByTagName('th');
|
||||||
|
for (let i = 0; i < ths.length; i++) {
|
||||||
|
const currentTh = ths[i];
|
||||||
|
if (currentTh !== th) {
|
||||||
|
currentTh.classList.remove('asc');
|
||||||
|
} else {
|
||||||
|
currentTh.classList.toggle('asc');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// hide arrow on non-sorting columns
|
||||||
|
const sortableThs = table.querySelectorAll('thead th[data-sort]');
|
||||||
|
for (let i = 0; i < sortableThs.length; i++) {
|
||||||
|
const sortableTh = sortableThs[i];
|
||||||
|
if (sortableTh !== th) {
|
||||||
|
sortableTh.classList.remove('asc');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// add sorting event listener to thead
|
||||||
|
const ths = document.querySelectorAll('table th[data-sort]');
|
||||||
|
for (let i = 0; i < ths.length; i++) {
|
||||||
|
const th = ths[i];
|
||||||
|
th.addEventListener('click', () => {
|
||||||
|
const sortType = th.getAttribute('data-sort');
|
||||||
|
let columnIndex;
|
||||||
|
switch (sortType) {
|
||||||
|
case 'name':
|
||||||
|
columnIndex = 0;
|
||||||
|
break;
|
||||||
|
case 'modified':
|
||||||
|
columnIndex = 1;
|
||||||
|
break;
|
||||||
|
case 'size':
|
||||||
|
columnIndex = 2;
|
||||||
|
break;
|
||||||
|
case 'type':
|
||||||
|
columnIndex = 3;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sortTable(th, columnIndex);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
@ -444,14 +529,14 @@ func (h *HttpServer) getFolder(log *starlog.StarLogger, w http.ResponseWriter, r
|
|||||||
if h.uploadFolder != "" {
|
if h.uploadFolder != "" {
|
||||||
upload = `<a href=/b612?upload=true>Upload Web Page Is Openned!</a>`
|
upload = `<a href=/b612?upload=true>Upload Web Page Is Openned!</a>`
|
||||||
}
|
}
|
||||||
w.Write([]byte(fmt.Sprintf(htmlTitle, r.URL.Path, version, r.URL.Path, upload)))
|
w.Write([]byte(fmt.Sprintf(htmlTitle, r.URL.Path, version, "Index of "+r.URL.Path, upload)))
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
p := r.URL.Path
|
p := r.URL.Path
|
||||||
if p[len(p)-1:] != "/" {
|
if p[len(p)-1:] != "/" {
|
||||||
p += "/"
|
p += "/"
|
||||||
}
|
}
|
||||||
w.Write([]byte(fmt.Sprintf(`<tr><td><a class="filename" href="%s">%s</a></td><td>%s</td><td>%s</td><td class="filetype">%s</td></tr>`,
|
w.Write([]byte(fmt.Sprintf(`<tr><td><a class="filename" href="%s">%s</a></td><td>%s</td><td>%s</td><td class="filetype">%s</td></tr>`,
|
||||||
p+"..", "..", "-", "-", "上层文件夹")))
|
p+"..", "../", "-", "-", "上层文件夹")))
|
||||||
}
|
}
|
||||||
if r.URL.Path == "/" {
|
if r.URL.Path == "/" {
|
||||||
r.URL.Path = ""
|
r.URL.Path = ""
|
||||||
@ -466,7 +551,7 @@ func (h *HttpServer) getFolder(log *starlog.StarLogger, w http.ResponseWriter, r
|
|||||||
r.URL.Path+"/"+v.Name(), v.Name(), v.ModTime().Format("2006-01-02 15:04:05"), fmt.Sprintf("%d (%s)", v.Size(), h.trimSize(v.Size())), h.FileType(v.Name()))))
|
r.URL.Path+"/"+v.Name(), v.Name(), v.ModTime().Format("2006-01-02 15:04:05"), fmt.Sprintf("%d (%s)", v.Size(), h.trimSize(v.Size())), h.FileType(v.Name()))))
|
||||||
} else {
|
} else {
|
||||||
w.Write([]byte(fmt.Sprintf(`<tr><td><a class="filename" href="%s">%s</a></td><td>%s</td><td>%s</td><td class="filetype">%s</td></tr>`,
|
w.Write([]byte(fmt.Sprintf(`<tr><td><a class="filename" href="%s">%s</a></td><td>%s</td><td>%s</td><td class="filetype">%s</td></tr>`,
|
||||||
r.URL.Path+"/"+v.Name(), v.Name(), v.ModTime().Format("2006-01-02 15:04:05"), "-", "文件夹")))
|
r.URL.Path+"/"+v.Name(), v.Name()+"/", v.ModTime().Format("2006-01-02 15:04:05"), "-", "文件夹")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -477,6 +562,7 @@ func (h *HttpServer) getFolder(log *starlog.StarLogger, w http.ResponseWriter, r
|
|||||||
func (h *HttpServer) getFile(log *starlog.StarLogger, w http.ResponseWriter, r *http.Request, fullpath string) error {
|
func (h *HttpServer) getFile(log *starlog.StarLogger, w http.ResponseWriter, r *http.Request, fullpath string) error {
|
||||||
if !staros.Exists(fullpath) {
|
if !staros.Exists(fullpath) {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
|
w.Write([]byte(`<html><title>B612 Http Server</title><body><h1 "style="text-align: center;">404 NOT FOUND</h1><hr ></body></html>`))
|
||||||
return errors.New("File Not Found! 404 ERROR")
|
return errors.New("File Not Found! 404 ERROR")
|
||||||
}
|
}
|
||||||
//starlog.Debugln(r.Header)
|
//starlog.Debugln(r.Header)
|
||||||
@ -485,7 +571,7 @@ func (h *HttpServer) getFile(log *starlog.StarLogger, w http.ResponseWriter, r *
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to open file %s,reason:%v\n", r.URL.Path, err)
|
log.Errorf("Failed to open file %s,reason:%v\n", r.URL.Path, err)
|
||||||
w.WriteHeader(502)
|
w.WriteHeader(502)
|
||||||
w.Write([]byte("<h1>502 SERVER ERROR</h1>"))
|
w.Write([]byte(`<html><title>B612 Http Server</title><body><h1 "style="text-align: center;">502 SERVER ERROR</h1><hr ></body></html>`))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer fp.Close()
|
defer fp.Close()
|
||||||
@ -495,11 +581,11 @@ func (h *HttpServer) getFile(log *starlog.StarLogger, w http.ResponseWriter, r *
|
|||||||
var tani string
|
var tani string
|
||||||
tani = fmt.Sprintf("%v Byte", transferData)
|
tani = fmt.Sprintf("%v Byte", transferData)
|
||||||
if f64 := float64(transferData) / 1024; f64 > 1 {
|
if f64 := float64(transferData) / 1024; f64 > 1 {
|
||||||
tani = fmt.Sprintf("%v KB", f64)
|
tani = fmt.Sprintf("%v KiB", f64)
|
||||||
if f64 = float64(f64) / 1024; f64 > 1 {
|
if f64 = float64(f64) / 1024; f64 > 1 {
|
||||||
tani = fmt.Sprintf("%v MB", f64)
|
tani = fmt.Sprintf("%v MiB", f64)
|
||||||
if f64 = float64(f64) / 1024; f64 > 1 {
|
if f64 = float64(f64) / 1024; f64 > 1 {
|
||||||
tani = fmt.Sprintf("%v GB", f64)
|
tani = fmt.Sprintf("%v GiB", f64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -575,11 +661,11 @@ func (h *HttpServer) trimSize(size int64) string {
|
|||||||
var tani string
|
var tani string
|
||||||
tani = fmt.Sprintf("%v Byte", size)
|
tani = fmt.Sprintf("%v Byte", size)
|
||||||
if f64 := float64(size) / 1024; f64 > 1 {
|
if f64 := float64(size) / 1024; f64 > 1 {
|
||||||
tani = fmt.Sprintf("%.3f KB", math.Trunc(f64*1e3+0.5)*1e-3)
|
tani = fmt.Sprintf("%.3f KiB", math.Trunc(f64*1e3+0.5)*1e-3)
|
||||||
if f64 = float64(f64) / 1024; f64 > 1 {
|
if f64 = float64(f64) / 1024; f64 > 1 {
|
||||||
tani = fmt.Sprintf("%.3f MB", math.Trunc(f64*1e3+0.5)*1e-3)
|
tani = fmt.Sprintf("%.3f MiB", math.Trunc(f64*1e3+0.5)*1e-3)
|
||||||
if f64 = float64(f64) / 1024; f64 > 1 {
|
if f64 = float64(f64) / 1024; f64 > 1 {
|
||||||
tani = fmt.Sprintf("%.3f GB", math.Trunc(f64*1e3+0.5)*1e-3)
|
tani = fmt.Sprintf("%.3f GiB", math.Trunc(f64*1e3+0.5)*1e-3)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
main.go
12
main.go
@ -5,6 +5,7 @@ import (
|
|||||||
"b612.me/apps/b612/base64"
|
"b612.me/apps/b612/base64"
|
||||||
"b612.me/apps/b612/base85"
|
"b612.me/apps/b612/base85"
|
||||||
"b612.me/apps/b612/base91"
|
"b612.me/apps/b612/base91"
|
||||||
|
"b612.me/apps/b612/calc"
|
||||||
"b612.me/apps/b612/detach"
|
"b612.me/apps/b612/detach"
|
||||||
"b612.me/apps/b612/df"
|
"b612.me/apps/b612/df"
|
||||||
"b612.me/apps/b612/dfinder"
|
"b612.me/apps/b612/dfinder"
|
||||||
@ -20,6 +21,9 @@ import (
|
|||||||
"b612.me/apps/b612/tcping"
|
"b612.me/apps/b612/tcping"
|
||||||
"b612.me/apps/b612/uac"
|
"b612.me/apps/b612/uac"
|
||||||
"b612.me/apps/b612/vic"
|
"b612.me/apps/b612/vic"
|
||||||
|
"b612.me/stario"
|
||||||
|
"b612.me/starlog"
|
||||||
|
"github.com/inconshreveable/mousetrap"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,11 +33,17 @@ var cmdRoot = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
cobra.MousetrapHelpText = ""
|
||||||
cmdRoot.AddCommand(tcping.Cmd, uac.Cmd, httpserver.Cmd, httpreverse.Cmd,
|
cmdRoot.AddCommand(tcping.Cmd, uac.Cmd, httpserver.Cmd, httpreverse.Cmd,
|
||||||
base64.Cmd, base85.Cmd, base91.Cmd, attach.Cmd, detach.Cmd, df.Cmd, dfinder.Cmd,
|
base64.Cmd, base85.Cmd, base91.Cmd, attach.Cmd, detach.Cmd, df.Cmd, dfinder.Cmd,
|
||||||
ftp.Cmd, generate.Cmd, hash.Cmd, image.Cmd, merge.Cmd, search.Cmd, split.Cmd, vic.Cmd)
|
ftp.Cmd, generate.Cmd, hash.Cmd, image.Cmd, merge.Cmd, search.Cmd, split.Cmd, vic.Cmd,
|
||||||
|
calc.Cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
starlog.SetLevelColor(starlog.LvError, []starlog.Attr{starlog.FgHiMagenta})
|
||||||
cmdRoot.Execute()
|
cmdRoot.Execute()
|
||||||
|
if mousetrap.StartedByExplorer() {
|
||||||
|
stario.StopUntil("Press Any Key to Continue...", "", true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
1
ping/ping.go
Normal file
1
ping/ping.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package ping
|
Loading…
x
Reference in New Issue
Block a user