master
兔子 2 years ago
parent 3f8208d254
commit 54c881b3be

@ -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,
Auth: &server.SimpleAuth{Name: username, Password: pwd},
}
log.Printf("Starting ftp server on %v:%v", opts.Hostname, opts.Port)
log.Printf("Username %v, Password %v", username, pwd)
server := server.NewServer(opts)

@ -5,7 +5,7 @@ import "path/filepath"
func (h *HttpServer) FileType(name string) string {
ext := filepath.Ext(name)
if len(ext) == 0 || ext == "." {
return "文件"
return "其他文件"
}
ext = ext[1:]
mimeMap := map[string]string{

@ -82,11 +82,31 @@ var htmlTitle string = `<!DOCTYPE html>
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
position: relative;
}
th:hover {
cursor: pointer;
background-color: #ddd;
th[data-sort]:before {
content: "▼";
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 {
@ -112,49 +132,69 @@ var htmlTitle string = `<!DOCTYPE html>
<table>
<thead>
<tr>
<th onclick="sortTable(0)">Name</th>
<th onclick="sortTable(1)">Modified</th>
<th onclick="sortTable(2)">Size</th>
<th onclick="sortTable(3)">Type</th>
<th data-sort="name" class="asc">Name</th>
<th data-sort="modified">Modified</th>
<th data-sort="size">Size</th>
<th data-sort="type">Type</th>
</tr>
</thead>
<tbody>`
var htmlTail = ` </tbody>
</table>
<hr />
<pre>
<h2 style="text-align: center;">B612.Me © Apache 2.0 License</h2>
</pre>
</div>
<script>
function sortTable(n) {
const table = document.querySelector('table');
const rows = table.rows;
let switching = true;
let shouldSwitch = false;
let direction = 'asc';
let switchcount = 0;
function sortTable(th, n) {
const table = document.querySelector('table');
const rows = table.rows;
let switching = true;
let shouldSwitch = false;
let direction = 'asc';
let switchcount = 0;
while (switching) {
switching = false;
let i;
for (i = 1; i < rows.length - 1; i++) {
shouldSwitch = false;
const x = rows[i].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 (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
if (xValue > yValue) {
shouldSwitch = true;
break;
}
} else if (direction === 'desc') {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
if (xValue < yValue) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
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>
</body>
@ -444,14 +529,14 @@ func (h *HttpServer) getFolder(log *starlog.StarLogger, w http.ResponseWriter, r
if h.uploadFolder != "" {
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 != "/" {
p := r.URL.Path
if p[len(p)-1:] != "/" {
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>`,
p+"..", "..", "-", "-", "上层文件夹")))
p+"..", "../", "-", "-", "上层文件夹")))
}
if 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()))))
} 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>`,
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 {
if !staros.Exists(fullpath) {
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")
}
//starlog.Debugln(r.Header)
@ -485,7 +571,7 @@ func (h *HttpServer) getFile(log *starlog.StarLogger, w http.ResponseWriter, r *
if err != nil {
log.Errorf("Failed to open file %s,reason:%v\n", r.URL.Path, err)
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
}
defer fp.Close()
@ -495,11 +581,11 @@ func (h *HttpServer) getFile(log *starlog.StarLogger, w http.ResponseWriter, r *
var tani string
tani = fmt.Sprintf("%v Byte", transferData)
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 {
tani = fmt.Sprintf("%v MB", f64)
tani = fmt.Sprintf("%v MiB", f64)
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
tani = fmt.Sprintf("%v Byte", size)
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 {
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 {
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)
}
}
}

@ -5,6 +5,7 @@ import (
"b612.me/apps/b612/base64"
"b612.me/apps/b612/base85"
"b612.me/apps/b612/base91"
"b612.me/apps/b612/calc"
"b612.me/apps/b612/detach"
"b612.me/apps/b612/df"
"b612.me/apps/b612/dfinder"
@ -20,6 +21,9 @@ import (
"b612.me/apps/b612/tcping"
"b612.me/apps/b612/uac"
"b612.me/apps/b612/vic"
"b612.me/stario"
"b612.me/starlog"
"github.com/inconshreveable/mousetrap"
"github.com/spf13/cobra"
)
@ -29,11 +33,17 @@ var cmdRoot = &cobra.Command{
}
func init() {
cobra.MousetrapHelpText = ""
cmdRoot.AddCommand(tcping.Cmd, uac.Cmd, httpserver.Cmd, httpreverse.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() {
starlog.SetLevelColor(starlog.LvError, []starlog.Attr{starlog.FgHiMagenta})
cmdRoot.Execute()
if mousetrap.StartedByExplorer() {
stario.StopUntil("Press Any Key to Continue...", "", true)
}
}

@ -0,0 +1 @@
package ping
Loading…
Cancel
Save