1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tools: make v test-cleancode test everything by default (#10050)

This commit is contained in:
Delyan Angelov
2021-05-08 13:32:29 +03:00
committed by GitHub
parent cba2cb6b9c
commit 8a380f4699
132 changed files with 3230 additions and 3440 deletions

View File

@ -31,9 +31,9 @@ fn init_color_table() []int {
color_table_[15] = 0xffffff
// color palette
for i in 0 .. 216 {
r := value_range[(i / 36) % 6]
g := value_range[(i / 6) % 6]
b := value_range[i % 6]
r := ui.value_range[(i / 36) % 6]
g := ui.value_range[(i / 6) % 6]
b := ui.value_range[i % 6]
color_table_[i + 16] = ((r << 16) & 0xffffff) + ((g << 8) & 0xffff) + (b & 0xff)
}
// grayscale
@ -70,7 +70,7 @@ fn lookup_rgb(r int, g int, b int) int {
color := (r << 16) + (g << 8) + b
// lookup extended colors only, coz non-extended can be changed by users.
for i in 16 .. 256 {
if color_table[i] == color {
if ui.color_table[i] == color {
return i
}
}

View File

@ -179,7 +179,7 @@ pub:
pub struct Context {
ExtraContext // contains fields specific to an implementation
pub:
cfg Config
cfg Config // adsasdas
mut:
print_buf []byte
paused bool

View File

@ -21,13 +21,13 @@ mut:
}
fn restore_terminal_state() {
if ctx_ptr != 0 {
if ctx_ptr.cfg.use_alternate_buffer {
if ui.ctx_ptr != 0 {
if ui.ctx_ptr.cfg.use_alternate_buffer {
// clear the terminal and set the cursor to the origin
print('\x1b[2J\x1b[3J')
print('\x1b[?1049l')
}
C.SetConsoleMode(ctx_ptr.stdin_handle, stdin_at_startup)
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
}
load_title()
os.flush()
@ -44,7 +44,7 @@ pub fn init(cfg Config) &Context {
panic('could not get stdin handle')
}
// save the current input mode, to be restored on exit
if C.GetConsoleMode(stdin_handle, &stdin_at_startup) == 0 {
if C.GetConsoleMode(stdin_handle, &ui.stdin_at_startup) == 0 {
panic('could not get stdin console mode')
}
@ -77,14 +77,13 @@ pub fn init(cfg Config) &Context {
}
unsafe {
x := &ctx_ptr
x := &ui.ctx_ptr
*x = ctx
}
C.atexit(restore_terminal_state)
for code in ctx.cfg.reset {
os.signal(code, fn() {
mut c := ctx_ptr
os.signal(code, fn () {
mut c := ui.ctx_ptr
if c != 0 {
c.cleanup()
}
@ -129,10 +128,12 @@ fn (mut ctx Context) parse_events() {
if !C.GetNumberOfConsoleInputEvents(ctx.stdin_handle, &nr_events) {
panic('could not get number of events in stdin')
}
if nr_events < 1 { return }
if nr_events < 1 {
return
}
// print('$nr_events | ')
if !C.ReadConsoleInput(ctx.stdin_handle, &ctx.read_buf[0], buf_size, &nr_events) {
if !C.ReadConsoleInput(ctx.stdin_handle, &ctx.read_buf[0], ui.buf_size, &nr_events) {
panic('could not read from stdin')
}
for i in 0 .. nr_events {
@ -142,13 +143,16 @@ fn (mut ctx Context) parse_events() {
e := unsafe { ctx.read_buf[i].Event.KeyEvent }
ch := e.wVirtualKeyCode
ascii := unsafe { e.uChar.AsciiChar }
if e.bKeyDown == 0 { continue } // we don't handle key_up events because they don't exist on linux...
if e.bKeyDown == 0 {
continue
}
// we don't handle key_up events because they don't exist on linux...
// see: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
code := match int(ch) {
C.VK_BACK { KeyCode.backspace }
C.VK_RETURN { KeyCode.enter }
C.VK_PRIOR { KeyCode.page_up }
14 ... 20 { KeyCode.null }
14...20 { KeyCode.null }
C.VK_NEXT { KeyCode.page_down }
C.VK_END { KeyCode.end }
C.VK_HOME { KeyCode.home }
@ -158,17 +162,23 @@ fn (mut ctx Context) parse_events() {
C.VK_DOWN { KeyCode.down }
C.VK_INSERT { KeyCode.insert }
C.VK_DELETE { KeyCode.delete }
65 ... 90 { KeyCode(ch + 32) } // letters
91 ... 93 { KeyCode.null } // special keys
96 ... 105 { KeyCode(ch - 48) } // numpad numbers
112 ... 135 { KeyCode(ch + 178) } // f1 - f24
65...90 { KeyCode(ch + 32) } // letters
91...93 { KeyCode.null } // special keys
96...105 { KeyCode(ch - 48) } // numpad numbers
112...135 { KeyCode(ch + 178) } // f1 - f24
else { KeyCode(ascii) }
}
mut modifiers := Modifiers{}
if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers.set(.alt) }
if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers.set(.ctrl) }
if e.dwControlKeyState & 0x10 != 0 { modifiers.set(.shift) }
if e.dwControlKeyState & (0x1 | 0x2) != 0 {
modifiers.set(.alt)
}
if e.dwControlKeyState & (0x4 | 0x8) != 0 {
modifiers.set(.ctrl)
}
if e.dwControlKeyState & 0x10 != 0 {
modifiers.set(.shift)
}
mut event := &Event{
typ: .key_down
@ -190,9 +200,15 @@ fn (mut ctx Context) parse_events() {
x := e.dwMousePosition.X + 1
y := int(e.dwMousePosition.Y) - sb_info.srWindow.Top + 1
mut modifiers := Modifiers{}
if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers.set(.alt) }
if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers.set(.ctrl) }
if e.dwControlKeyState & 0x10 != 0 { modifiers.set(.shift) }
if e.dwControlKeyState & (0x1 | 0x2) != 0 {
modifiers.set(.alt)
}
if e.dwControlKeyState & (0x4 | 0x8) != 0 {
modifiers.set(.ctrl)
}
if e.dwControlKeyState & 0x10 != 0 {
modifiers.set(.shift)
}
// TODO: handle capslock/numlock/etc?? events exist for those keys
match int(e.dwEventFlags) {
C.MOUSE_MOVED {
@ -220,23 +236,34 @@ fn (mut ctx Context) parse_events() {
button: button
modifiers: modifiers
})
} C.MOUSE_WHEELED {
}
C.MOUSE_WHEELED {
ctx.event(&Event{
typ: .mouse_scroll
direction: if i16(e.dwButtonState >> 16) < 0 { Direction.up } else { Direction.down }
direction: if i16(e.dwButtonState >> 16) < 0 {
Direction.up
} else {
Direction.down
}
x: x
y: y
modifiers: modifiers
})
} 0x0008 /* C.MOUSE_HWHEELED */ {
}
0x0008 /* C.MOUSE_HWHEELED */ {
ctx.event(&Event{
typ: .mouse_scroll
direction: if i16(e.dwButtonState >> 16) < 0 { Direction.right } else { Direction.left }
direction: if i16(e.dwButtonState >> 16) < 0 {
Direction.right
} else {
Direction.left
}
x: x
y: y
modifiers: modifiers
})
} 0 /* CLICK */, C.DOUBLE_CLICK {
}
0 /* CLICK */, C.DOUBLE_CLICK {
button := match int(e.dwButtonState) {
0 { ctx.mouse_down }
1 { MouseButton.left }
@ -251,7 +278,8 @@ fn (mut ctx Context) parse_events() {
button: button
modifiers: modifiers
})
} else {}
}
else {}
}
}
C.WINDOW_BUFFER_SIZE_EVENT {
@ -287,12 +315,12 @@ fn (mut ctx Context) parse_events() {
[inline]
fn save_title() {
// restore the previously saved terminal title
print('\x1b[22;0t')
// restore the previously saved terminal title
print('\x1b[22;0t')
}
[inline]
fn load_title() {
// restore the previously saved terminal title
print('\x1b[23;0t')
// restore the previously saved terminal title
print('\x1b[23;0t')
}

View File

@ -60,7 +60,8 @@ fn (mut ctx Context) termios_setup() ? {
// store the current title, so restore_terminal_state can get it back
save_title()
if !ctx.cfg.skip_init_checks && !(os.is_atty(C.STDIN_FILENO) != 0 && os.is_atty(C.STDOUT_FILENO) != 0) {
if !ctx.cfg.skip_init_checks && !(os.is_atty(C.STDIN_FILENO) != 0
&& os.is_atty(C.STDOUT_FILENO) != 0) {
return error('not running under a TTY')
}
@ -230,7 +231,7 @@ fn (mut ctx Context) termios_loop() {
sw.restart()
if ctx.cfg.event_fn != voidptr(0) {
unsafe {
len := C.read(C.STDIN_FILENO, byteptr(ctx.read_buf.data) + ctx.read_buf.len,
len := C.read(C.STDIN_FILENO, &byte(ctx.read_buf.data) + ctx.read_buf.len,
ctx.read_buf.cap - ctx.read_buf.len)
ctx.resize_arr(ctx.read_buf.len + len)
}

View File

@ -23,8 +23,8 @@ const (
esu = '\x1bP=2s\x1b\\'
)
[inline]
// write puts the string `s` into the print buffer.
[inline]
pub fn (mut ctx Context) write(s string) {
if s == '' {
return
@ -32,16 +32,16 @@ pub fn (mut ctx Context) write(s string) {
unsafe { ctx.print_buf.push_many(s.str, s.len) }
}
[inline]
// flush displays the accumulated print buffer to the screen.
[inline]
pub fn (mut ctx Context) flush() {
// TODO: Diff the previous frame against this one, and only render things that changed?
if !ctx.enable_su {
C.write(1, ctx.print_buf.data, ctx.print_buf.len)
} else {
C.write(1, bsu.str, bsu.len)
C.write(1, ui.bsu.str, ui.bsu.len)
C.write(1, ctx.print_buf.data, ctx.print_buf.len)
C.write(1, esu.str, esu.len)
C.write(1, ui.esu.str, ui.esu.len)
}
ctx.print_buf.clear()
}
@ -52,14 +52,14 @@ pub fn (mut ctx Context) bold() {
ctx.write('\x1b[1m')
}
[inline]
// set_cursor_position positions the cusor at the given coordinates `x`,`y`.
[inline]
pub fn (mut ctx Context) set_cursor_position(x int, y int) {
ctx.write('\x1b[$y;${x}H')
}
[inline]
// show_cursor will make the cursor appear if it is not already visible
[inline]
pub fn (mut ctx Context) show_cursor() {
ctx.write('\x1b[?25h')
}
@ -70,8 +70,8 @@ pub fn (mut ctx Context) hide_cursor() {
ctx.write('\x1b[?25l')
}
[inline]
// set_color sets the current foreground color used by any succeeding `draw_*` calls.
[inline]
pub fn (mut ctx Context) set_color(c Color) {
if ctx.enable_rgb {
ctx.write('\x1b[38;2;${int(c.r)};${int(c.g)};${int(c.b)}m')
@ -80,8 +80,8 @@ pub fn (mut ctx Context) set_color(c Color) {
}
}
[inline]
// set_color sets the current background color used by any succeeding `draw_*` calls.
[inline]
pub fn (mut ctx Context) set_bg_color(c Color) {
if ctx.enable_rgb {
ctx.write('\x1b[48;2;${int(c.r)};${int(c.g)};${int(c.b)}m')
@ -90,20 +90,20 @@ pub fn (mut ctx Context) set_bg_color(c Color) {
}
}
[inline]
// reset_color sets the current foreground color back to it's default value.
[inline]
pub fn (mut ctx Context) reset_color() {
ctx.write('\x1b[39m')
}
[inline]
// reset_bg_color sets the current background color back to it's default value.
[inline]
pub fn (mut ctx Context) reset_bg_color() {
ctx.write('\x1b[49m')
}
[inline]
// reset restores the state of all colors and text formats back to their default values.
[inline]
pub fn (mut ctx Context) reset() {
ctx.write('\x1b[0m')
}
@ -113,21 +113,21 @@ pub fn (mut ctx Context) clear() {
ctx.write('\x1b[2J\x1b[3J')
}
[inline]
// set_window_title sets the string `s` as the window title.
[inline]
pub fn (mut ctx Context) set_window_title(s string) {
print('\x1b]0;$s\x07')
}
[inline]
// draw_point draws a point at position `x`,`y`.
[inline]
pub fn (mut ctx Context) draw_point(x int, y int) {
ctx.set_cursor_position(x, y)
ctx.write(' ')
}
[inline]
// draw_text draws the string `s`, starting from position `x`,`y`.
[inline]
pub fn (mut ctx Context) draw_text(x int, y int, s string) {
ctx.set_cursor_position(x, y)
ctx.write(s)
@ -248,9 +248,9 @@ pub fn (mut ctx Context) draw_empty_rect(x int, y int, x2 int, y2 int) {
ctx.draw_line(x2, y, x2, y2)
}
[inline]
// horizontal_separator draws a horizontal separator, spanning the width of the screen.
[inline]
pub fn (mut ctx Context) horizontal_separator(y int) {
ctx.set_cursor_position(0, y)
ctx.write(strings.repeat(/* `⎽` */`-`, ctx.window_width))
ctx.write(strings.repeat(`-`, ctx.window_width)) // /* `⎽` */
}