Skip to content

Conversation

@stianhoiland
Copy link

@stianhoiland stianhoiland commented Oct 22, 2025

This is a concise patch bringing simple native Windows support to fzy.

I have tried to keep the changes to absolute minimum and refrained from including fixups in this PR.

It may be interesting to diff tty_posix.c with tty_win32.c for a quick overview of the port.

What I'm most unsure about is if you'll find the conditional build OK. I went through several alternatives before settling on renaming tty.c and the conditional in the Makefile. For compatibility with other forks it may be desirable to keep tty.c and change the non-Windows branch in the Makefile to TTY=tty. Or to keep a tty.c with the following contents:

#ifdef __linux__
#include "tty_posix.c"
#elif _WIN32
#include "tty_win32.c"
#endif

Let me know if you're otherwise fine with the changes but would like them integrated in the build differently. I can prepare it how you are most comfortable.

Needs testing on POSIX-ish. Tested on Windows 11 and functional. Wants further enhancements to mitigate flickering (will do that in a separate PR: #201 ).

747e26d anticipates #199 and implements tty_puts.

Commit message:

choices.c: Win32 processor count
tty.h: Semantic change for tty_newline: No longer clears, and serves as platform LF/CRLF
tty_interface.c: Prior hardcoded '\n' are now calls to tty_newline. Additionally, prior calls to tty_newline is now preceded by tty_clearline
tty.c -> tty_posix.c: The only change to tty.c (other than renaming) is that tty_newline no longer clears the line
tty_win32.c: Simple windows tty support. Considerations:
  - Uses ASCII versions of CreateFile, ReadConsole, and WriteConsole. For future reference consult {Create,Read,Write}{Console,File}{A/W/Input}
  - tty_init doesn't implement tty_filename argument on Windows, only CONIN$/CONOUT$
  - Buffered IO: Buffer size (setvbuf) not set. FILE* vs. HANDLE (for conmode)
  - SIGWINCH not handled. For future reference, see ReadConsoleInput + WINDOW_BUFFER_SIZE_EVENT
Makefile: OS check

choices.c: Win32 processor count
tty.h: Semantic change for tty_newline: No longer clears, and serves as platform LF/CRLF
tty_interface.c: Prior hardcoded '\n' are now calls to tty_newline. Additionally, prior calls to tty_newline is now preceded by tty_clearline
tty.c -> tty_posix.c: The only change to tty.c (other than renaming) is that tty_newline no longer clears the line
tty_win32.c: Simple windows tty support. Considerations:
  - Uses ASCII versions of CreateFile, ReadConsole, and WriteConsole. For future reference consult {Create,Read,Write}{Console,File}{A/W/Input}
  - tty_init doesn't implement tty_filename argument on Windows, only CONIN$/CONOUT$
  - Buffered IO: Buffer size (setvbuf) not set. FILE* vs. HANDLE (for conmode)
  - SIGWINCH not handled. For future reference, see ReadConsoleInput + WINDOW_BUFFER_SIZE_EVENT
Makefile: OS check
@stianhoiland
Copy link
Author

stianhoiland commented Oct 22, 2025

I forgot to comment on 949d08e ("Use opaque pointer for tty_t in preparation for Windows support"). Most importantly this builds on fzy's already great encapsulation and fully isolates struct termios and termios.h, which are not on Windows. struct conmode and struct tty in tty_win32.c shows how I utilized the completed encapsulation.

EDIT

Below are same comments as above but attached to respective files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through several alternatives before settling on renaming tty.c and the conditional in the Makefile. For compatibility with other forks it may be desirable to keep tty.c and change the non-Windows branch in the Makefile to TTY=tty. Or to keep a tty.c with the following contents:

#ifdef __linux__
#include "tty_posix.c"
#elif _WIN32
#include "tty_win32.c"
#endif

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semantic change for tty_newline: No longer clears, and serves as platform LF/CRLF.

Copy link
Author

@stianhoiland stianhoiland Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let tty_init pass an opaque pointer to caller. Move struct termios and termios.h —which are not on Windows—from tty.h to tty.c (renamed tty_posix.c).

See struct conmode and struct tty in tty_win32.c for how I utilized the encapsulation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior hardcoded '\n' are now calls to tty_newline. Additionally, prior calls to tty_newline are now preceded by tty_clearline since tty_newline no longer clears.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semantic change for tty_newline: No longer clears, and serves as platform LF/CRLF.

Copy link
Author

@stianhoiland stianhoiland Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple Windows tty support, sticking as close as possible to present implementation in tty.c (renamed to tty_posix.c).

Considerations:

  • Uses ASCII versions of CreateFile, ReadConsole, and WriteConsole. For future reference consult {Create,Read,Write}{Console,File}{A/W/Input}
  • tty_init doesn't implement tty_filename argument, only CONIN$/CONOUT$
  • Buffered IO: Buffer size (setvbuf) not set. FILE* vs. HANDLE (for conmode)
  • SIGWINCH not handled. For future reference, see ReadConsoleInput + WINDOW_BUFFER_SIZE_EVENT
  • Anticipates Tighten the tty platform abstraction #199 and implements tty_puts.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tty_init now returns an opaque pointer to caller.

Copy link
Author

@stianhoiland stianhoiland Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let tty_init pass an opaque pointer to caller. Move struct termios and termios.h —which are not on Windows—from tty.h to tty.c (renamed tty_posix.c).

See struct conmode and struct tty in tty_win32.c for how I utilized the encapsulation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant