Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# specific dirs
.DS_Store
.vscode
prepare-code

# binaries
prepare-code
pc
2 changes: 2 additions & 0 deletions build.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand Down
5 changes: 5 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
install_dir: /Users/obrutus/.prepare-code
bypass_prompt: false
preferred_editor: code
open_in_editor: true

5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"prepare-code/src/platform"
"prepare-code/src/session"
"prepare-code/src/system"
"prepare-code/src/tui"
)

func main() {
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ func CanBypassPrompt() bool {
}

func GetEditor() (string, bool) {
return config.prefferedEditor, false
return config.prefferedEditor, config.openInEditor
}
4 changes: 2 additions & 2 deletions src/platform/codeforces/codeforces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
28 changes: 28 additions & 0 deletions src/tui/console_config_nix.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions src/tui/console_config_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 52 additions & 0 deletions src/tui/console_print.go
Original file line number Diff line number Diff line change
@@ -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(" ")
}
}
Loading