aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/term/term_nix.c.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/old/vlib/term/term_nix.c.v
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/old/vlib/term/term_nix.c.v')
-rw-r--r--v_windows/v/old/vlib/term/term_nix.c.v105
1 files changed, 105 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/term/term_nix.c.v b/v_windows/v/old/vlib/term/term_nix.c.v
new file mode 100644
index 0000000..45a0a9b
--- /dev/null
+++ b/v_windows/v/old/vlib/term/term_nix.c.v
@@ -0,0 +1,105 @@
+module term
+
+import os
+
+#include <sys/ioctl.h>
+#include <termios.h> // TIOCGWINSZ
+
+pub struct C.winsize {
+pub:
+ ws_row u16
+ ws_col u16
+ ws_xpixel u16
+ ws_ypixel u16
+}
+
+fn C.ioctl(fd int, request u64, arg voidptr) int
+
+// get_terminal_size returns a number of colums and rows of terminal window.
+pub fn get_terminal_size() (int, int) {
+ if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
+ return default_columns_size, default_rows_size
+ }
+ w := C.winsize{}
+ C.ioctl(1, u64(C.TIOCGWINSZ), &w)
+ return int(w.ws_col), int(w.ws_row)
+}
+
+// get_cursor_position returns a Coord containing the current cursor position
+pub fn get_cursor_position() Coord {
+ if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
+ return Coord{
+ x: 0
+ y: 0
+ }
+ }
+ // TODO: use termios.h, C.tcgetattr & C.tcsetattr directly,
+ // instead of using `stty`
+ mut oldsettings := os.execute('stty -g')
+ if oldsettings.exit_code < 0 {
+ oldsettings = os.Result{}
+ }
+ os.system('stty -echo -icanon time 0')
+ print('\033[6n')
+ mut ch := int(0)
+ mut i := 0
+ // ESC [ YYY `;` XXX `R`
+ mut reading_x := false
+ mut reading_y := false
+ mut x := 0
+ mut y := 0
+ for {
+ ch = C.getchar()
+ b := byte(ch)
+ i++
+ if i >= 15 {
+ panic('C.getchar() called too many times')
+ }
+ // state management:
+ if b == `R` {
+ break
+ }
+ if b == `[` {
+ reading_y = true
+ reading_x = false
+ continue
+ }
+ if b == `;` {
+ reading_y = false
+ reading_x = true
+ continue
+ }
+ // converting string vals to ints:
+ if reading_x {
+ x *= 10
+ x += (b - byte(`0`))
+ }
+ if reading_y {
+ y *= 10
+ y += (b - byte(`0`))
+ }
+ }
+ // restore the old terminal settings:
+ os.system('stty $oldsettings.output')
+ return Coord{
+ x: x
+ y: y
+ }
+}
+
+// set_terminal_title change the terminal title
+pub fn set_terminal_title(title string) bool {
+ if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
+ return true
+ }
+ print('\033]0')
+ print(title)
+ print('\007')
+ return true
+}
+
+// clear clears current terminal screen.
+pub fn clear() {
+ print('\x1b[2J')
+ print('\x1b[H')
+}