-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1774 from actonlang/add-term-stuff
Add term stuff
- Loading branch information
Showing
8 changed files
with
121 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
27 changes: 27 additions & 0 deletions
27
docs/acton-by-example/src/environment/interactive_stdin.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Interactive stdin | ||
|
||
For interactive programs, like a text editor, input is not fed into the program | ||
line by line, rather the program can react on individual key strokes. | ||
|
||
The default stdin mode is the *canonical* mode, which implies line buffering and | ||
that there are typically line editing capabilities offered that are implemented | ||
external to the Acton program. By setting stdin in non-canonical mode we can | ||
instead get the raw key strokes directly fed to us. | ||
|
||
```python | ||
actor main(env): | ||
def interact(input): | ||
print("Got some input:", input) | ||
|
||
# Set non-canonical mode, so we get each key stroke directly | ||
env.set_stdin(canonical=False) | ||
# Turn off terminal echo | ||
env.set_stdin(echo=False) | ||
env.stdin_install(interact) | ||
``` | ||
|
||
We can also disable the echo mode with the echo option. | ||
|
||
The Acton run time system will copy the stdin terminal settings on startup and | ||
restore them on exit, so you do not need to manually restore terminal echo for | ||
example. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# An interacive terminal program that increments and decrements a counter based | ||
# on user input. The program will exit when the user types 'q'. There is also a | ||
# periodic incrementation of the counter every 5 seconds. | ||
# | ||
# The actor based asynchronous I/O model of Acton makes it very natural to | ||
# express event-driven reactive programs like this that can react to user input | ||
# while not blocking on I/O, thus allowing for other tasks to be performed, in | ||
# this case the periodic incrementation of the counter. | ||
|
||
actor main(env: Env): | ||
var count = 0 | ||
|
||
def interact(input): | ||
if input == "q": | ||
print("Quitting!") | ||
env.exit(0) | ||
elif input == "i": | ||
count += 1 | ||
print("Incrementing! Count is now:", count) | ||
elif input == "d": | ||
count -= 1 | ||
print("Decrementing! Count is now:", count) | ||
else: | ||
print("Unknown command:", input) | ||
|
||
# Set non-canonical mode, so we get each key stroke directly | ||
env.set_stdin(canonical=False) | ||
# Turn off terminal echo | ||
env.set_stdin(echo=False) | ||
env.stdin_install(interact) | ||
print("Type 'q' to quit, 'i' to increment a counter and 'd' to decrement it.") | ||
|
||
def periodic(): | ||
count += 1 | ||
print("Periodic +1 increment. Count is now:", count) | ||
after 5: periodic() | ||
after 1: periodic() |