(watchFlag bool, f func() (string, error))
| 57 | } |
| 58 | |
| 59 | func rerun(watchFlag bool, f func() (string, error)) { |
| 60 | if watchFlag { |
| 61 | print("\033[H\033[2J") // clear the screen |
| 62 | |
| 63 | var prevStrSlice []string |
| 64 | |
| 65 | for true { |
| 66 | nextStr, err := f() |
| 67 | if err != nil { |
| 68 | fmt.Println() |
| 69 | exit.Error(err) |
| 70 | } |
| 71 | |
| 72 | nextStr = watchHeader() + "\n\n" + s.EnsureSingleTrailingNewLine(nextStr) |
| 73 | nextStrSlice := strings.Split(nextStr, "\n") |
| 74 | |
| 75 | terminalWidth := getTerminalWidth() |
| 76 | if terminalWidth <= 0 { |
| 77 | exit.Error(ErrorNoTerminalWidth()) |
| 78 | } |
| 79 | |
| 80 | nextNumLines := 0 |
| 81 | for _, strLine := range nextStrSlice { |
| 82 | nextNumLines += (len(strLine)-1)/terminalWidth + 1 |
| 83 | } |
| 84 | prevNumLines := 0 |
| 85 | for _, strLine := range prevStrSlice { |
| 86 | prevNumLines += (len(strLine)-1)/terminalWidth + 1 |
| 87 | } |
| 88 | |
| 89 | for i := prevNumLines; i > nextNumLines; i-- { |
| 90 | fmt.Printf("\033[%dA\033[2K", 1) // move the cursor up and clear the line |
| 91 | } |
| 92 | |
| 93 | for i := 0; i < prevNumLines; i++ { |
| 94 | fmt.Printf("\033[%dA", 1) // move the cursor up |
| 95 | } |
| 96 | |
| 97 | for _, strLine := range nextStrSlice { |
| 98 | fmt.Printf("\033[2K%s\n", strLine) // clear the line and print the new line |
| 99 | } |
| 100 | |
| 101 | prevStrSlice = nextStrSlice |
| 102 | |
| 103 | time.Sleep(time.Second * 2) |
| 104 | } |
| 105 | } else { |
| 106 | str, err := f() |
| 107 | if err != nil { |
| 108 | exit.Error(err) |
| 109 | } |
| 110 | fmt.Print(s.EnsureSingleTrailingNewLine(str)) |
| 111 | } |
| 112 | } |
no test coverage detected