File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ package terminal
2+
3+ import (
4+ "fmt"
5+ "io"
6+ "os"
7+ )
8+
9+ // Println prints line on stdout
10+ func Println (msg string , args ... any ) {
11+ Fprintln (os .Stdout , msg , args ... )
12+ }
13+
14+ // Printf prints content on stdout
15+ func Printf (msg string , args ... any ) {
16+ Fprintf (os .Stdout , msg , args ... )
17+ }
18+
19+ // Fprintln prints line on writer
20+ func Fprintln (w io.Writer , msg string , args ... any ) {
21+ Sync ()
22+ if len (args ) > 0 {
23+ msg = fmt .Sprintf (msg , args ... )
24+ }
25+
26+ if _ , err := fmt .Fprintln (w , msg ); err != nil {
27+ panic (err )
28+ }
29+
30+ Sync ()
31+ }
32+
33+ // Fprintf writes content on writer
34+ func Fprintf (w io.Writer , msg string , args ... any ) {
35+ Sync ()
36+
37+ if _ , err := fmt .Fprintf (w , msg , args ... ); err != nil {
38+ panic (err )
39+ }
40+
41+ Sync ()
42+ }
43+
44+ // SyncBlock syncs stdout and stderr and calls callback in between sync calls
45+ func SyncBlock (callback func ()) {
46+ Sync ()
47+ callback ()
48+ Sync ()
49+ }
50+
51+ // Sync syncs terminal output and ensures logger has finished
52+ func Sync () {
53+ if err := os .Stdout .Sync (); err != nil {
54+ panic (err )
55+ }
56+ if err := os .Stderr .Sync (); err != nil {
57+ panic (err )
58+ }
59+ }
You can’t perform that action at this time.
0 commit comments