Standard Graphics is a command-line tool for printing 2D graphics from any language to any screen.

Related tags

Command-line stdg
Overview



Standard Graphics (abbreviated as stdg) is a command-line tool for printing 2D graphics from any language to any screen. It uses stdin and stdout to make 2D graphics as simple as printing commands with Python's print() or Java's System.out.println() for example. You can even write plain text and handle user interaction.

You only need to learn stdg once. Then it will no longer matter what language or frameworks you happen to be using. You will always be able to easily print 2D graphics using the same easy-to-remember commands.

Features

  • Learn once - stdg provides a minimal set of commands for 2D graphics. These commands are designed to be intuitive, easy-to-learn, and easy-to-remember. They are mostly inspired by Khan Academy's computer programming environment and Processing.
  • Use anywhere - stdg is both cross-language and cross-platform. This means that you can freely switch between programming languages and operating systems without having to learn a totally new 2D graphics library for the language/OS.
  • Get fast, performant graphics - stdg is an abstraction over MiniFB and Raqote which are both implemented entirely in Rust (except for MacOS which uses some Objective-C). Therefore, your graphics will be rendered at effectively native speed. There is no use of an Electron-like framework or "dynamic" languages in the rendering, event-handling.

Example

Standard Graphics can be used with plain text. As an example, you could save the following to a text file, rectangle.txt.

start 400 400 Untitled

background 255 255 255
fill 255 0 0
rect 50 50 100 100

present forever

If you have stdg and python correctly installed, you can open a terminal (command line/prompt), navigate to the folder containing rectangle.txt by using cd, type out the following, and press enter.

rectangle.txt | stdg

This is what you should see.

An example output through standard graphics

Standard Graphics enables you to do even more complex things. This next example is taken from Khan Academy's "Making animations" lesson. Since this program is written in Python, you will have to use the Python interpreter to run it.

print("start 400 400 Untitled")


# position of the car
x = 10

while True:
  print("background 151 244 247")

  # draw the car body
  print("fill 255 0 115")
  print("nostroke")
  print("rect " + str(x) + " 200 100 20")
  print("rect " + str(x + 15) + " 178 70 40")

  # draw the wheels
  print("fill 77 66 66")
  print("circle " + str(x + 25) + " 221 12")
  print("circle " + str(x + 75) + " 221 12")

  print("present") # this is what actually says to draw, must be in an infinite loop

  x = x + 1

Enter the following in a terminal.

python moving_car.py | stdg

You should see the following pop up as a window.

An example output through standard graphics

Read on the see how you can use stdg for not only simple graphics and animations but also interactive user interfaces.

Built with Standard Graphics

Standard Graphics has been used by open-source projects.

Usage

There are 2 ways of using Standard Graphics.

The first, and simplest, is by simply piping your program's output to stdg. All of your program's output (stuff that would normally be printed) will be sent to stdg. You can open a terminal and type in something like the following, provided stdg is installed.

my_data.csv | ruby my_program.rb | stdg

For each line of input that stdg recieves, it will check if the first word (first token in line split by whitespace) matches a command. If the word matches, it will look at the rest of the line and execute the command by printing a rectangle, or setting color, etc.. If the word doesn't match, it will just print the line out.

The second way is by giving stdg a process to run.

stdg node my_thing.js | settings.csv

In this case, stdg doesn't accept any input. You can't pipe anything to it. You can't interactively type stuff in the terminal. You can, however, give it a process to run.

You give it a process by providing arguments to stdg. These arguments get parsed into a single command that can be executed as a process. Basically you can write stdg python options.py or stdg ./menu but you can't do stdg python options.py && ./menu.

Then, stdg will run your process and read its output in exactly the same way the first usage has stdg reading. Commands get interpreted. Everything else gets printed as output. But now in addition to that, stdg will also sometimes print input to the process itself. This input will be information regarding things like mouse position, mouse click, etc.. The process can read this input one line at a time to get the mouse/key/etc. information.

This second way of using stdg allows for interactivity. You can do things like this-

print("start 400 400 A Rectangle")

while True:
  print("background 255 255 255")
  print("fill 255 0 0")
  
  print("get mousex")
  print("get mousey")
  print("rect " + str(float(input()) - 25.0) + " " + str(float(input()) - 25.0) + " 50.0 50.0")
  print("present")

Cheat Sheet

The following is a cheat sheet/reference for using stdg.

The following are commands for the most basic usage of stdg. Note that all whitespace in text like in start and text commands are converted to single spaces.

Command Example Note
Start everything start 400 400 A rectangle Must be first line printed
Present stuff to be drawn present Must be in an infinite loop
Present forever present forever Useful in .txt files
Get position of mouse get mousex, get mousey Sends back line containing position
Get "is mouse pressed?" get mouseispressed left Must be left, center, or right
Get "is key pressed?" get keyispressed space Valid keys listed below
Get all keys pressed get keys Sends space-seperated valid keys

The following are useful for styling.

Command Example Note
Set background color background 220 220 220
Set fill color fill 255 0 0 240 All values of red-green-blue-alpha are 0-255
Set stroke color stroke 20 20 20 Stroke is also called outline
Don't fill or don't stroke nostroke, nofill Default color is nofill, stroke 0 0 0
Set stroke weight strokeweight 5 Default is 1
Set stroke cap strokecap round Must be square, project, or round (default)
Set stroke join strokejoin bevel Must be miter (default), bevel, or round

We can do transformations.

Command Example Note
Push a transformation push This lets us begin a new transformation
Pop top pop This will revert to the old transformation
Translate top translate 50 0
Scale top scale 0.5 1.0
Rotate top rotate 180 Degrees are in degrees

There are the common 2D primitives.

Command Example Note
Draw rectangle rect 50 50 300 300 Must be x, y coordinates, width, height
Draw ellipse ellipse 200 200 50 40 Centered at given coordinates
Draw circle circle 200 200 50 All coordinates in 4th quadrant
Draw line line 300 100 100 300
Draw arc arc 200 200 50 40 0 90 Degrees are in degrees
Draw polygon poly 130 70 180 20 340 100 360 200 270 250 130 70 Arbitrary number of points allowed

Last but not least, we have text and images.

Command Example Note
Set text font textfont C:\Windows\Fonts\Arial.ttf \ might have to be \\
Set text size textsize 20
Draw text text 30 30 Must be followed by line with text
Load image open character.png as char Should be called only once, if possible
Draw image image char 30 70, image char 5 5 50 1 Should be a loaded image

And here are the keys supported by stdg-

  • All numeric characters
  • All lower-case alphabetic characters (use leftshift or rightshift to check for upper-case)
  • up, down, left, right
  • space, tab, enter
  • leftshift, rightshift
  • escape, backspace, delete

About

Standard Graphics is designed to be useful for many sorts of things-

  • User interfaces for Bash scripts
  • Visualization of Python-scripted simulations
  • Visualization of data
  • Desktop games written in JavaScript
  • Simple vector graphics with plain text
  • Simple animations with C
  • and much more...

The software itself is written entirely in pure Rust with the only exception being the MacOS back-end. It uses Raquote and MiniFB behind the scenes for drawing stuff.

Getting Started

There are two ways to install Standard Graphics.

The first way is to download the binaries from here. Once downloaded, make sure that the folder location where the binaries are stored is added to PATH (look that up on the Internet if you aren't sure "how to add folder location to PATH". So basically what you would need to do is...

  1. Download binary file (stdg.exe or stdg)
  2. Move binary to desired folder (maybe Program Files\stdg\stdg.exe for example)
  3. Add the desired folder to PATH.

The second way is to install Standard Graphics with cargo. Make sure you have installed Rust. Then, simply install as follows.

cargo install stdg

During installation, you may have to install a bunch of packages. On Windows, I was personally able to simply install and run. However, on Linux, I had to install at least libfontconfig1-dev, xcursor.

Once installed, you can take a look at the cheat sheet for more information on the various commands you can print.

You might also like...
High performance wlroots screen recording, featuring hardware encoding

wl-screenrec High performance wlroots based screen recorder. Uses dma-buf transfers to get surface, and uses the GPU to do both the pixel format conve

on-screen keyboard display for your coding streams.

⌨ OSKD (On-screen key display) OSKD is an on-screen keyboard display that can be used during streams. It provides an intuitive and easy-to-use interfa

👀Little program I made in 🦀Rust that reminds me every 20 minutes to look away from my computer 🖥screen.
👀Little program I made in 🦀Rust that reminds me every 20 minutes to look away from my computer 🖥screen.

👀 eye break Little program I made in 🦀 Rust that reminds me every 20 minutes to look away from my computer 🖥 screen. I stay way too long on the com

Windows Capture Simple Screen Capture for Windows 🔥

Windows Capture   Windows Capture is a highly efficient Rust library that enables you to effortlessly capture the screen using the Graphics Capture AP

A grub theme in the style of the Minecraft singleplayer (sp) world selection screen!

Minecraft World Selection Grub Theme Wowie, another Minecraft grub theme! But this time it's in the style of the singleplayer world selection menu, wh

A screen saver for the terminal 🌃
A screen saver for the terminal 🌃

tnap - Let's take a nap 💤 tnap is a screen saver for the terminal. You can rest the terminal in a secure. Features Display images in the terminal and

rip is a command-line deletion tool focused on safety, ergonomics, and performance

rip (Rm ImProved) rip is a command-line deletion tool focused on safety, ergonomics, and performance. It favors a simple interface, and does not imple

A command-line benchmarking tool
A command-line benchmarking tool

hyperfine 中文 A command-line benchmarking tool. Demo: Benchmarking fd and find: Features Statistical analysis across multiple runs. Support for arbitra

An interactive cheatsheet tool for the command-line
An interactive cheatsheet tool for the command-line

navi An interactive cheatsheet tool for the command-line. navi allows you to browse through cheatsheets (that you may write yourself or download from

Comments
  • Feature request: close command

    Feature request: close command

    It'll be useful to add a "close" command to close window and end stdg process, for both Windows and Linux.

    Perhaps, something like:

    1 => match command[0].as_str() { "close" => { std::process::exit(); // or abort( ) } "present" => { ...

    opened by crcpucmg 0
  • Don't Fidget with Widgets

    Don't Fidget with Widgets

    Hello!

    Do you know of this paper?

    http://patricklogan.blogspot.com/2003/10/dont-fidget-with-widgets-draw.html https://patricklogan.blogspot.com/2007/08/dont-fidget-with-widgets-draw.html https://ia902200.us.archive.org/13/items/bitsavers_dectechrep_144635/WRL-91-6.pdf

    The ezd system from the links above feels very similar to what you've implemented here, it'll be cool to compare and contrast.

    Bravo for working on such a cool project!

    opened by duncanmak 0
  • `cargo install stdg` fails with compiler error

    `cargo install stdg` fails with compiler error

    I'm getting a compiler error when trying to do cargo install stdg. Tried it both on OSX and Linux.

       Compiling stdg v0.2.0
    error[E0308]: mismatched types
       --> /home/computer/.asdf/installs/rust/1.46.0/registry/src/github.com-1ecc6299db9ec823/stdg-0.2.0/src/main.rs:567:33
        |
    567 | / ...                   &text_font
    568 | | ...                       .clone()
    569 | | ...                       .expect("text font must be given before text is drawn"),
        | |_________________________________________________________________________________^ expected struct `font_kit::loaders::freetype::Font`, found struct `font_kit::font::Font`
        |
        = note: expected reference `&font_kit::loaders::freetype::Font`
                   found reference `&font_kit::font::Font`
        = note: perhaps two different versions of crate `font_kit` are being used?
    
    error: aborting due to previous error
    
    opened by goto-engineering 1
Releases(v0.2.0)
  • v0.2.0(Nov 12, 2019)

    This release completely revamps Standard Graphics. It replaces the SDL2 dependency with pure Rust. It brings full, well-documented support for event handling and more complex graphics.

    The previous release was still an experimental Standard Graphics. This is the real deal. I'm quite excited to begin using it for personal work and I would be absolutely thrilled if others feel similarly.

    Also, please post issues on the GitHub repository. I try to be as responsive as I can.

    Source code(tar.gz)
    Source code(zip)
    stdg(3.15 MB)
    stdg.exe(663.50 KB)
Owner
Caleb Winston
I'm currently working on computing at scale, computing at the edge, neural engineering, lab automation...
Caleb Winston
Small command-line tool to switch monitor inputs from command line

swmon Small command-line tool to switch monitor inputs from command line Installation git clone https://github.com/cr1901/swmon cargo install --path .

William D. Jones 5 Aug 20, 2022
Pink is a command-line tool inspired by the Unix man command.

Pink is a command-line tool inspired by the Unix man command. It displays custom-formatted text pages in the terminal using a subset of HTML-like tags.

null 3 Nov 2, 2023
A command line tool written in Rust and designed to be a modern build tool + package manager for C/C++ projects.

CCake CCake is a command line tool written in Rust and designed to be a modern build tool + package manager for C/C++ projects. Goals To be easily und

Boston Vanseghi 4 Oct 24, 2022
Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're working with files or directories, this tool simplifies common file operations with ease.

Rust FileOps Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're w

Harikesh Ranjan Sinha 5 May 2, 2024
Command-line HTTP client for sending a POST request to specified URI on each stdin line.

line2httppost Simple tool to read lines from stdin and post each line as separate POST request to a specified URL (TCP connection is reused though). G

Vitaly Shukela 3 Jan 3, 2023
Cross-platform terminal screen clearing library

ClearScreen Cross-platform terminal screen clearing library. API documentation. Dual-licensed with Apache 2.0 and MIT. Uses Caretaker Maintainership.

null 23 Dec 30, 2022
A small utility that moves the start menu to the top-center of the screen in Windows 11.

TopCenterStart11 A small utility that moves the start menu to the top-center of the screen in Windows 11. As of right now, this application can only p

Ryan de Jonge 12 Nov 12, 2022
A library to capture the Screen on Linux, MacOS and Windows.

Captis - A library for capturing the screen on Linux, MacOS and Windows It's a fairly simple library that performs good enough, the OS level APIs that

CrewNew.com 17 Dec 23, 2022
a FREE and MODERN split-screen tetris game WITHOUT ADS

tetr:: A ✨ modern ✨ Tetris game made in OpenGL and Rust Gameplay tetr:: is an implementaion of modern Tetris, and essentially a clone of tetr.io. This

Adam Harmansky 3 Sep 10, 2022
Adapt the screen's color spectrum according to the hour of the day in order to improve your sleep

circadianlight What It Is Circadian Light is a program, currently only working on Linux with X, that controls the color spectrum of your screen accord

null 7 Dec 28, 2022