|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "github.com/fatih/color" |
| 6 | + "github.com/urfave/cli/v2" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | +) |
| 10 | + |
| 11 | +func readFromStdin() ([]string, error) { |
| 12 | + scn := bufio.NewScanner(os.Stdin) |
| 13 | + var lines []string |
| 14 | + for scn.Scan() { |
| 15 | + line := scn.Text() |
| 16 | + if len(line) == 1 { |
| 17 | + // Group Separator (GS ^]): ctrl-] |
| 18 | + if line[0] == '\x1D' { |
| 19 | + break |
| 20 | + } |
| 21 | + } |
| 22 | + lines = append(lines, line) |
| 23 | + } |
| 24 | + |
| 25 | + // TODO add check for errors |
| 26 | + return lines, nil |
| 27 | +} |
| 28 | + |
| 29 | +func writeLinesToFile(filename string, lines []string) { |
| 30 | + f, err := os.Create(filename) |
| 31 | + if err != nil { |
| 32 | + fmt.Println(err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + for _, line := range lines { |
| 37 | + fmt.Fprintln(f, line) |
| 38 | + if err != nil { |
| 39 | + fmt.Println(err) |
| 40 | + return |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +// TODO(Eroui): add checks on errors |
| 47 | +func CustomCaseAction(context *cli.Context) error { |
| 48 | + color.Green("Creating Custom Test Case...") |
| 49 | + |
| 50 | + color.Green("Provide your input:") |
| 51 | + input_lines, _ := readFromStdin() |
| 52 | + |
| 53 | + color.Green("Provide your output:") |
| 54 | + output_lines, _ := readFromStdin() |
| 55 | + |
| 56 | + writeLinesToFile("sample.in", input_lines) |
| 57 | + writeLinesToFile("sample.out", output_lines) |
| 58 | + |
| 59 | + color.Green("Created Custom Test Case...") |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +var CaseCommand = cli.Command{ |
| 64 | + Name: "case", |
| 65 | + Aliases: []string{"c"}, |
| 66 | + Usage: "Create a custom test case.", |
| 67 | + UsageText: "Add custom test cases to egor task.", |
| 68 | + Action: CustomCaseAction, |
| 69 | + // TODO(Eroui): add necessary flags |
| 70 | +} |
0 commit comments