0

Currently writing a CLI application that provides a better wrapper for some Git functionality. As this is a CLI, the user uses their terminal to use the application.

What I need to be able to do is pipe the output of certain commands into "clean" and interactive sub-programs. For example, if the user has my UI up, but presses a key, I need my UI to entirely swap with that of git add --patch [file_name]. When the user is done (as that is a step-by-step process that eventually concludes) I need to swap back to my application's UI.

Problem is, I don't know where or what to pipe this output to. If I pipe it into the terminal, it clashes with the existing CLI app's stdout and that does not work.

A separate but related feature was to show diff logs. To solve this issue of a clean and interactive terminal for that, I piped the stdout into less and this worked perfectly. Just can't find a way to do that for something more complex like git add --patch.

Let me know if more details are needed. Mods got grumpy last time when I added (apparently too much) information, so this is super cut down.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
Devildude4427
  • 892
  • 1
  • 8
  • 28
  • You need to create a pseudo terminal (PTY). – 0andriy Jul 20 '20 at 18:30
  • Okay, never heard of that before. Thanks. Is there some standard way of doing that, or is it something that you generally have to roll yourself? Can't find a whole lot of beginner-level documentation for that, but probably my fault. I'll work on that though, appreciate the pointer. – Devildude4427 Jul 20 '20 at 23:12
  • There is a standard way for POSIX compatible systems. It's a very generic feature. https://devblogs.microsoft.com/commandline/windows-command-line-introducing-the-windows-pseudo-console-conpty/ and https://stackoverflow.com/questions/21545897/how-to-control-interactive-console-input-output-from-python-on-windows for Windows. Article on Wiki: https://en.wikipedia.org/wiki/Pseudoterminal. Unix manual: https://man7.org/linux/man-pages/man7/pty.7.html. **Tons** of documentation available! – 0andriy Jul 21 '20 at 08:16
  • Alright, well this is all way over my head. I assumed there was just something simple and cross platform I could pipe one command into and have everything work. Apparently that's not the case. Thanks for your help though. – Devildude4427 Jul 21 '20 at 17:43
  • That is how to achieve it properly. But you see, even Microsoft realizes that they need a feature. :-) – 0andriy Jul 21 '20 at 20:00

1 Answers1

-1

Use a pager

Most git commands with multi-screen output will automatically invoke a user-configured pager like less or more.

For those that don't, you could explicitly pipe them to less. Less's default behavior is to restore the terminal when it exits.

git add --patch [file_name] 2>&1 | less

Use curses

Use curses to clear the screen at startup and restore it at shutdown. For example, in Python:

import curses

# Clear the screen.
scr = curses.initscr()
scr.clear()
scr.refresh()

# Application code here.
import time
time.sleep(1)

# Restore the screen.
curses.endwin()

(If you're not using Python you can probably find native curses bindings in whatever language you're using.)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I'll look into Curses, thanks. Using Rust, but I know there's some sort of Curses support. The first command you show just locks up my terminal though, might be something with my terminal locally, but I definitely can't get anything out of that. Blank screen and no way to escape. Have to close the entire window. – Devildude4427 Jul 20 '20 at 19:48
  • Using curses for this is simply not enough. You will still need a control terminal and a pseudo one to allow another **interactive** program to work as is. – 0andriy Jul 21 '20 at 08:21