diff --git a/.gitignore b/.gitignore index 50efcbd..9303b86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# specific dirs .DS_Store .vscode -prepare-code \ No newline at end of file + +# binaries +prepare-code +pc \ No newline at end of file diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index 7b83d6f..ab69c1b --- a/build.sh +++ b/build.sh @@ -3,6 +3,8 @@ go build . # alias backup clone go build -o pc ./main.go +cp pc prepare-code + if [[ $? -ne 0 ]]; then echo "Build failed!" echo "Maybe go is not installed?" diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..0a4fc0a --- /dev/null +++ b/config.yaml @@ -0,0 +1,5 @@ +install_dir: /Users/obrutus/.prepare-code +bypass_prompt: false +preferred_editor: code +open_in_editor: true + diff --git a/main.go b/main.go index 2be8fb7..9dc6ad4 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "prepare-code/src/platform" "prepare-code/src/session" "prepare-code/src/system" + "prepare-code/src/tui" ) func main() { @@ -18,9 +19,7 @@ func main() { fmt.Print("Enter the URL of problem: ") fmt.Scanln(&url) } else if os.Args[1] == "config" { - configMap := config.GetConfigMap() - fmt.Printf("Your config map is : ") - fmt.Println(configMap) + tui.PrintMap(config.GetConfigMap()) return } else if os.Args[1] == "raw" { genericHandover() diff --git a/src/config/config.go b/src/config/config.go index 1ff516e..5262c23 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -91,5 +91,5 @@ func CanBypassPrompt() bool { } func GetEditor() (string, bool) { - return config.prefferedEditor, false + return config.prefferedEditor, config.openInEditor } diff --git a/src/platform/codeforces/codeforces.go b/src/platform/codeforces/codeforces.go index 2664c56..57e4306 100644 --- a/src/platform/codeforces/codeforces.go +++ b/src/platform/codeforces/codeforces.go @@ -101,7 +101,7 @@ func extractCodeNameProblemSet(url string) (string, error) { contestRemovedString := prefixRemovedString[firstSlashIndex+1:] // here the assumption is single character [from A - Z] - questionCode := contestRemovedString[0] + questionCode := strings.ToUpper(string(contestRemovedString[0])) - return contestNumber + codeNameDelimiter + string(questionCode), nil + return contestNumber + codeNameDelimiter + questionCode, nil } diff --git a/src/tui/console_config_nix.go b/src/tui/console_config_nix.go new file mode 100644 index 0000000..08a9f1b --- /dev/null +++ b/src/tui/console_config_nix.go @@ -0,0 +1,28 @@ +//go:build !windows + +package tui + +import ( + "errors" + "os" + "syscall" + "unsafe" +) + +type winsize struct { + Row, Col, Xpixel, Ypixel uint16 +} + +func fetchSize() (int, int, error) { + var ws winsize + _, _, err := syscall.Syscall( + syscall.SYS_IOCTL, + os.Stdout.Fd(), + uintptr(syscall.TIOCGWINSZ), + uintptr(unsafe.Pointer(&ws)), + ) + if err != 0 { + return 0, 0, errors.New("failed to get unix terminal info") + } + return int(ws.Col), int(ws.Row), nil +} diff --git a/src/tui/console_config_windows.go b/src/tui/console_config_windows.go new file mode 100644 index 0000000..78573dd --- /dev/null +++ b/src/tui/console_config_windows.go @@ -0,0 +1,37 @@ +package tui + +import ( + "errors" + "os" + "syscall" + "unsafe" +) + +var ( + kernel32 = syscall.NewLazyDLL("kernel32.dll") + procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo") +) + +type coord struct{ X, Y int16 } +type smallRect struct{ Left, Top, Right, Bottom int16 } +type consoleScreenBufferInfo struct { + Size coord + CursorPosition coord + Attributes uint16 + Window smallRect + MaximumWindowSize coord +} + +func fetchSize() (int, int, error) { + var csbi consoleScreenBufferInfo + ret, _, _ := procGetConsoleScreenBufferInfo.Call( + os.Stdout.Fd(), + uintptr(unsafe.Pointer(&csbi)), + ) + if ret == 0 { + return 0, 0, errors.New("failed to get windows console info") + } + width := int(csbi.Window.Right - csbi.Window.Left + 1) + height := int(csbi.Window.Bottom - csbi.Window.Top + 1) + return width, height, nil +} diff --git a/src/tui/console_print.go b/src/tui/console_print.go new file mode 100644 index 0000000..c0d9c06 --- /dev/null +++ b/src/tui/console_print.go @@ -0,0 +1,52 @@ +package tui + +import ( + "fmt" +) + +const DefaultTabSize = 4 + +func PrintMap(inputMap map[string]interface{}) { + printMapWithPadding(inputMap, []int{DefaultTabSize}) +} + +func printMapWithPadding(inputMap map[string]interface{}, padding []int) { + fmt.Println(" *** config *** ") + fmt.Println("[") + + // mapLen := len(inputMap) + cnt := 0 + for key, value := range inputMap { + curPadding := padding[len(padding)-1] + printSpace(curPadding) + fmt.Printf("\"%s\": ", key) + + switch value.(type) { + case map[string]interface{}, map[string]string, map[string]int, map[int]interface{}: + fmt.Printf("{\n") + + padding = append(padding, curPadding+DefaultTabSize) + printMapWithPadding(value.(map[string]interface{}), padding) + padding = padding[:len(padding)-1] + + printSpace(curPadding) + fmt.Printf("}") + default: + fmt.Printf("%v", value) + // if we wanted comma + // if cnt < mapLen-1 { + // fmt.Print(",") + // } + cnt++ + fmt.Println() + } + } + + fmt.Println("]") +} + +func printSpace(n int) { + for i := 0; i < n; i++ { + fmt.Printf(" ") + } +}