Last active
March 28, 2024 15:28
-
-
Save thetarnav/435194b41e49532dcdcb7645a55c6f29 to your computer and use it in GitHub Desktop.
isatty in odin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| when ODIN_OS == .Windows { | |
| is_terminal :: proc(fd: os.Handle) -> bool { | |
| return false | |
| } | |
| } else { | |
| foreign import libc "system:c" | |
| foreign libc { | |
| @(link_name = "isatty") | |
| _isatty :: proc(fd: os.Handle) -> b32 --- | |
| } | |
| is_terminal :: proc(fd: os.Handle) -> bool { | |
| return bool(_isatty(fd)) | |
| } | |
| } | |
| main :: proc() { | |
| if is_terminal(os.stdin) { | |
| fmt.println("\e[0;36mEnter a name:\e[0m") | |
| } | |
| buf, alloc_err := make([]byte, mem.Megabyte * 10) | |
| if alloc_err != nil { | |
| fmt.panicf("error allocating memory: %v", alloc_err) | |
| } | |
| input: string | |
| input_len, os_err := os.read(os.stdin, buf[:]) | |
| if os_err != os.ERROR_NONE { | |
| fmt.panicf("error reading input: %d", os_err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment