-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vinson
committed
Nov 11, 2020
1 parent
70dcdb8
commit 7e7eeba
Showing
10 changed files
with
205 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module annotation-parse | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/mattn/go-runewidth v0.0.9 // indirect | ||
github.com/nsf/termbox-go v0.0.0-20201107200903-9b52a5faed9e | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Copyright The ef Co. ltd All rights reserved. | ||
// Created by vinson on 2020/11/4. | ||
// ShowDoc Scanner | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Copyright The ef Co. All rights reserved. | ||
// Created by vinson on 2020/11/4. | ||
|
||
package statement | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Copyright The ef Co. All rights reserved. | ||
// Created by vinson on 2020/11/4. | ||
|
||
package statement | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Created by vinson on 2020/11/4. | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
term "github.com/nsf/termbox-go" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func reset() { | ||
_ = term.Sync() | ||
} | ||
|
||
// Selection the function receive an array of fileInfo | ||
// return the array index from user selected, -1 means selected all | ||
func Selection(files *[]os.FileInfo) int { | ||
var isExit = false | ||
err := term.Init() | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer func() { | ||
term.Close() | ||
if isExit { | ||
os.Exit(0) | ||
} | ||
}() | ||
// 打印到控制到等待输入 | ||
selectIndex := -1 | ||
loop: | ||
for { | ||
switch ev := term.PollEvent(); ev.Type { | ||
case term.EventKey: | ||
switch ev.Key { | ||
case term.KeyArrowUp: | ||
reset() | ||
selectIndex-- | ||
case term.KeyArrowDown: | ||
reset() | ||
selectIndex++ | ||
case term.KeyEnter: | ||
reset() | ||
break loop | ||
case term.KeyEsc: | ||
reset() | ||
isExit = true | ||
break loop | ||
default: | ||
reset() | ||
fmt.Println("Invalid input") | ||
} | ||
case term.EventError: | ||
panic(ev.Err) | ||
} | ||
switch { | ||
case selectIndex < -1: | ||
selectIndex = -1 | ||
case selectIndex > len(*files)-1: | ||
selectIndex = len(*files) - 1 | ||
default: | ||
CallClear() | ||
var fsBuilder strings.Builder | ||
fsBuilder.WriteString("\033[1;32;40mPlease select and continue or press ESC to exit\033[0m\n\n") | ||
if selectIndex == -1 { | ||
fsBuilder.WriteString("\033[4;33;40mAll\033[0m\n") | ||
} else { | ||
fsBuilder.WriteString("All\n") | ||
} | ||
for i, f := range *files { | ||
if i == selectIndex { | ||
fsBuilder.WriteString("\033[4;33;40m") | ||
fsBuilder.WriteString(f.Name()) | ||
fsBuilder.WriteString("\033[0m\n") | ||
} else { | ||
fsBuilder.WriteString(f.Name()) | ||
fsBuilder.WriteString("\n") | ||
} | ||
} | ||
fmt.Print(fsBuilder.String()) | ||
} | ||
} | ||
CallClear() | ||
return selectIndex | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Created by vinson on 2020/11/11. | ||
|
||
package utils | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
var clear map[string]func() | ||
|
||
func init() { | ||
clear = make(map[string]func()) | ||
clear["linux"] = func() { | ||
cmd := exec.Command("clear") | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
clear["windows"] = func() { | ||
cmd := exec.Command("cmd", "/c", "cls") | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
} | ||
|
||
func CallClear() { | ||
value, ok := clear[runtime.GOOS] // runtime.GOOS -> linux, windows, darwin etc. | ||
if ok { //if we defined a clear func for that platform: | ||
value() //we execute it | ||
} else { //unsupported platform | ||
panic("Your platform is unsupported! I can't clear terminal screen :(") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters