Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CL_Eye-Catching_User_Interfaces.pdf

 CL_Eye-Catching_User_Interfaces.pdf

Command line interfaces (CLIs) can be as interactive, exciting, and beautiful as any desktop or web app. Like when building web apps, building an interactive CLI affords the developer many features, but comes with a frightening matrix of incompatibilities and quirks. James’ tutorial session will teach you how to use the many features and techniques available for building interactive CLIs, from progress bars and color to mouse input and animated graphics on the command line. You will learn how to detect when these features are available in a terminal and how to ensure your program works across different operating systems and terminals.

James Bowes

August 29, 2018
Tweet

More Decks by James Bowes

Other Decks in Programming

Transcript

  1. James Bowes Technical Lead @ Worked on many CLI tools

    no-one uses any more (up2date, yum) Loves Cats Loves Terminals ABOUT ME ABOUT ME  @jrbowes Manifold
  2. YOU WILL LEARN HOW TO YOU WILL LEARN HOW TO

    make progress bars and spinners decorate text draw anywhere on the terminal collect input do fancy things with mice and images ...safely, for any operating system
  3. YOU WILL LEARN HOW TO YOU WILL LEARN HOW TO

    make progress bars and spinners decorate text draw anywhere on the terminal collect input do fancy things with mice and images ...safely, for any operating system*
  4. NOT JUST FOR GO NOT JUST FOR GO Share these

    system calls and escape sequences with your non-gopher friends.
  5. NOT JUST FOR GO NOT JUST FOR GO Share these

    system calls and escape sequences with your non-gopher friends. Use a package for real code.
  6. GO IS AWESOME FOR CLIS GO IS AWESOME FOR CLIS

    Great support for Windows, Mac OS, Linux Easy cross compilation with GOOS and GOARCH Self-contained binary (as long as you don't need Cgo)
  7. HIERARCHY OF USER HIERARCHY OF USER INTERFACES INTERFACES Command Line

    Interface (CLI) Text-based User Interface (TUI) Graphical User Interface (GUI)
  8. HIERARCHY OF USER HIERARCHY OF USER INTERFACES INTERFACES Command Line

    Interface (CLI) Text-based User Interface (TUI) ☜ Graphical User Interface (GUI)
  9. BUILDING A PROGRESS BAR BUILDING A PROGRESS BAR func drawBar(percent

    int) { cols := 10 prog := strings.Repeat("=", cols*percent/100.0) fmt.Printf("\rdemo progress: %3[1]d%% |%-[3]*[2]s|", percent, prog, cols) }
  10. BUILDING A PROGRESS BAR (CON'T) BUILDING A PROGRESS BAR (CON'T)

    fmt.Printf("\rdemo progress: %3[1]d%% |%-[3]*[2]s|", percent, prog, cols)
  11. BUILDING A PROGRESS BAR (CON'T) BUILDING A PROGRESS BAR (CON'T)

    \r Move to start of line fmt.Printf("\rdemo progress: %3[1]d%% |%-[3]*[2]s|", percent, prog, cols)
  12. BUILDING A PROGRESS BAR (CON'T) BUILDING A PROGRESS BAR (CON'T)

    \r Move to start of line %3[1]d Print the int value of arg 1 fmt.Printf("\rdemo progress: %3[1]d%% |%-[3]*[2]s|", percent, prog, cols)
  13. BUILDING A PROGRESS BAR (CON'T) BUILDING A PROGRESS BAR (CON'T)

    \r Move to start of line %3[1]d Print the int value of arg 1 %% Literal percent fmt.Printf("\rdemo progress: %3[1]d%% |%-[3]*[2]s|", percent, prog, cols)
  14. BUILDING A PROGRESS BAR (CON'T) BUILDING A PROGRESS BAR (CON'T)

    \r Move to start of line %3[1]d Print the int value of arg 1 %% Literal percent -[3]* Left justify and pad by the value of arg 3 fmt.Printf("\rdemo progress: %3[1]d%% |%-[3]*[2]s|", percent, prog, cols)
  15. BUILDING A PROGRESS BAR (CON'T) BUILDING A PROGRESS BAR (CON'T)

    \r Move to start of line %3[1]d Print the int value of arg 1 %% Literal percent -[3]* Left justify and pad by the value of arg 3 [2]s Print the string value of arg 2 fmt.Printf("\rdemo progress: %3[1]d%% |%-[3]*[2]s|", percent, prog, cols)
  16. UNICODE SPINNERS UNICODE SPINNERS Carriage return ❤ unicode var (

    braille = []rune{'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', clock = []rune{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' )
  17. SPICE UP YOUR USER SPICE UP YOUR USER INTERACTIONS INTERACTIONS

    ✅ Success! ⚠ Your password needs more entropy ???
  18. MISSING CHARACTERS IN A MISSING CHARACTERS IN A TYPEFACE TYPEFACE

    missing := "The power symbol is new in Unicode 9:
  19. MISCOUNTING MULTIBYTE MISCOUNTING MULTIBYTE CHARACTERS CHARACTERS miscounted := " "

    fmt.Println(miscounted, "has len", len(miscounted)) fmt.Println(miscounted, "has runes", utf8.RuneCount([]byte(miscounted))) has len 4 has runes 1
  20. WIDE CHARACTERS WIDE CHARACTERS wide := " 十" fmt.Println(wide) fmt.Println("01")

    fmt.Println("runes:", utf8.RuneCount([]byte(wide))) fmt.Println("width:", runewidth.StringWidth(wide)) 十 01 runes: 1 width: 2
  21. SINGLE-WIDTH CHARACTERS THAT SINGLE-WIDTH CHARACTERS THAT RENDER AS WIDE RENDER

    AS WIDE wrongWidth := "☛" fmt.Println(wrongWidth) fmt.Println("01") fmt.Println("runes:", utf8.RuneCount([]byte(wrongWidth))) fmt.Println("width:", runewidth.StringWidth(wrongWidth)) fmt.Printf("%sOver top\n", wrongWidth) ☛ 01 runes: 1 width: 1 ☛Over top
  22. HOW BAD CAN IT BE? HOW BAD CAN IT BE?

    That's a teapot in ReGIS 0kc5N/d|NkuKboHmhDdc4_.f~Y`zTevNbsHipCjn3^`mZlVkkRdkPkkM` lLnlJkmIinIhoIlpImqIorImsJotJiuKkvKawLnwLdxMixMayMfyMjyM nyMoyMczMMgzMMMhzNNizNjzNhzOizOkzOizPjzPhzQfzQczQmyRjyRg yR`yShxSmwSmvSivS`vShuS`uSmtSftSctSlsTjsTesUlrVhrWdrXarY jqZbq[gp\ko]in^gm^cl^ej^dh]f[mcXbaT-f~O{IbxBlt2ZiqPjnEnk 1YjiLmg0^jfOdl@nl/QomCjo.VmqJet-_`wUzLa}D.b`,]obWieSchOi jMlKenJooIgqIdrJgsJctKotLguMluO`vPdvQkvQnvRawSgwShwTmwTa xTexTixTlxT`yTgySfySiySmyS`zSczRRRbzRRnyRRjyReyRayRlxRkx QfxQaxQlwQnwPiwPgwOawOovNivNgvMevM`vMkuLfuLntLjtLosLgsLi
  23. HOW CAN WE TELL HOW CAN WE TELL WHAT IS

    WHAT IS SUPPORTED? SUPPORTED?
  24. TERMINFO TERMINFO A database of terminal names (from TERM) and

    capabilities that started in ncurses. Includes escape codes.
  25. TERMINFO TERMINFO A database of terminal names (from TERM) and

    capabilities that started in ncurses. Includes escape codes. For better portability, use a terminfo database for in-band signalling.
  26. RELY ON THE USER RELY ON THE USER Provide flags

    and configuration for color and interactivity
  27. HANDLING WINDOWS HANDLING WINDOWS Windows 10 can enable VT processing

    For other versions, wrap the output and parse escape codes
  28. WINDOWS SUBSYSTEM FOR WINDOWS SUBSYSTEM FOR LINUX LINUX ANSI escape

    codes and POSIX syscalls under Windows The console related APIS don't work across environments
  29. DISPLAYING GRAPHICS DISPLAYING GRAPHICS SIXEL: raster graphics from DEC ReGIS:

    vector graphics from DEC Custom formats for assorted modern terminals
  30. CAPTURING MOUSE INPUT CAPTURING MOUSE INPUT Broad support Not supported

    in Windows VT Processing. You have to fall back to the input buffer API.