| 1 | import os |
| 2 | import term |
| 3 | import strings |
| 4 | |
| 5 | fn test_get_terminal_size() { |
| 6 | cols, _ := term.get_terminal_size() |
| 7 | assert cols > 0 |
| 8 | } |
| 9 | |
| 10 | fn test_h_divider() { |
| 11 | divider := term.h_divider('-') |
| 12 | assert divider.len > 0 |
| 13 | assert divider[0] == `-` |
| 14 | assert divider[divider.len - 1] == `-` |
| 15 | } |
| 16 | |
| 17 | fn test_h_divider_multiple_characters() { |
| 18 | xdivider := term.h_divider('abc') |
| 19 | assert xdivider.len > 0 |
| 20 | assert xdivider.contains('abcabc') |
| 21 | } |
| 22 | |
| 23 | fn test_header() { |
| 24 | divider := term.h_divider('-') |
| 25 | term_width := divider.len |
| 26 | assert term_width > 0 |
| 27 | empty_header := term.header('', '-') |
| 28 | short_header := term.header('reasonable header', '-') |
| 29 | very_long_header := term.header(['abc'].repeat(500).join(' '), '-') |
| 30 | very_long_header_2 := term.header(['abcd'].repeat(500).join(' '), '-') |
| 31 | /* |
| 32 | eprintln(divider) |
| 33 | eprintln(empty_header) |
| 34 | eprintln(short_header) |
| 35 | eprintln(term.header('another longer header', '_-/\\')) |
| 36 | eprintln(term.header('another longer header', '-')) |
| 37 | eprintln(term.header('short', '-')) |
| 38 | eprintln(term.header('12345', '-')) |
| 39 | eprintln(term.header('1234', '-')) |
| 40 | eprintln(term.header('123', '-')) |
| 41 | eprintln(term.header('12', '-')) |
| 42 | eprintln(term.header('1', '-')) |
| 43 | eprintln(very_long_header) |
| 44 | eprintln(divider) |
| 45 | eprintln(very_long_header_2) |
| 46 | eprintln(term.header(['abcd'].repeat(500).join(' '), '_-/\\')) |
| 47 | eprintln(term.header(['abcd'].repeat(500).join(' '), '_-//')) |
| 48 | eprintln(term.header('1', '_-/\\\/')) |
| 49 | eprintln(term.header('12', '_-/\\\/')) |
| 50 | eprintln(term.header('123', '_-/\\\/')) |
| 51 | eprintln(term.header('1234', '_-/\\/\\')) |
| 52 | eprintln(term.header('', '-')) |
| 53 | */ |
| 54 | assert term_width == empty_header.len |
| 55 | assert term_width == short_header.len |
| 56 | assert term_width == very_long_header.len |
| 57 | assert term_width == very_long_header_2.len |
| 58 | assert term_width == term.header('1234', '_-/\\/\\').len |
| 59 | } |
| 60 | |
| 61 | fn test_get_cursor_position() { |
| 62 | original_position := term.get_cursor_position()! |
| 63 | cursor_position_1 := term.get_cursor_position()! |
| 64 | assert original_position.x == cursor_position_1.x |
| 65 | assert original_position.y == cursor_position_1.y |
| 66 | |
| 67 | term.set_cursor_position( |
| 68 | x: 10 |
| 69 | y: 11 |
| 70 | ) |
| 71 | cursor_position_2 := term.get_cursor_position()! |
| 72 | |
| 73 | term.set_cursor_position( |
| 74 | x: 5 |
| 75 | y: 6 |
| 76 | ) |
| 77 | cursor_position_3 := term.get_cursor_position()! |
| 78 | |
| 79 | term.set_cursor_position(original_position) |
| 80 | eprintln('original_position: ${original_position}') |
| 81 | eprintln('cursor_position_2: ${cursor_position_2}') |
| 82 | eprintln('cursor_position_3: ${cursor_position_3}') |
| 83 | // 0,0 is returned on dumb terminals |
| 84 | if cursor_position_2.x == 0 && cursor_position_2.y == 0 { |
| 85 | return |
| 86 | } |
| 87 | if cursor_position_3.x == 0 && cursor_position_3.y == 0 { |
| 88 | return |
| 89 | } |
| 90 | assert cursor_position_2.x == 10 |
| 91 | assert cursor_position_2.y == 11 |
| 92 | assert cursor_position_3.x == 5 |
| 93 | assert cursor_position_3.y == 6 |
| 94 | } |
| 95 | |
| 96 | fn test_set_terminal_title() { |
| 97 | // do not change the current terminal title outside of CI: |
| 98 | if os.getenv('CI') != 'true' { |
| 99 | return |
| 100 | } |
| 101 | term.set_terminal_title('v is awesome!') |
| 102 | dump(@FILE) |
| 103 | assert true |
| 104 | } |
| 105 | |
| 106 | fn test_set_tab_title() { |
| 107 | // do not change the current terminal tab title outside of CI: |
| 108 | if os.getenv('CI') != 'true' { |
| 109 | return |
| 110 | } |
| 111 | term.set_tab_title('v is awesome!') |
| 112 | dump(@FILE) |
| 113 | assert true |
| 114 | } |
| 115 | |
| 116 | fn test_strip_ansi() { |
| 117 | string_list := [ |
| 118 | 'abc', |
| 119 | term.bold('abc'), |
| 120 | term.yellow('abc'), |
| 121 | term.bold(term.red('abc')), |
| 122 | term.strikethrough(term.inverse(term.dim(term.bold(term.bright_bg_blue('abc'))))), |
| 123 | ] |
| 124 | for s in string_list { |
| 125 | assert term.strip_ansi(s) == 'abc' |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | fn test_write_color() { |
| 130 | mut sb := strings.new_builder(100) |
| 131 | term.writeln_color(mut sb, 'hello') |
| 132 | term.writeln_color(mut sb, 'hello', fg: .red) |
| 133 | term.writeln_color(mut sb, 'hello', bg: .cyan) |
| 134 | term.writeln_color(mut sb, 'hello', styles: [.bold, .italic, .underline], fg: .red, bg: .cyan) |
| 135 | term.writeln_color(mut sb, 'hello', custom: '38;5;214') |
| 136 | |
| 137 | output := sb.str() |
| 138 | println(output) |
| 139 | |
| 140 | assert output.contains('\x1b[0m') |
| 141 | assert output.contains('\x1b[31m') |
| 142 | assert output.contains('\x1b[46m') |
| 143 | assert output.contains('\x1b[1;3;4;31;46m') |
| 144 | assert output.contains('\x1b[38;5;214m') |
| 145 | } |
| 146 | |