50 / 53 lines covered (94.3%)
1unit module BDD::Behave::Watch::UI;23use BDD::Behave::Colors;45our class UI {6 has IO::Handle $.in = $*IN;7 has IO::Handle $.out = $*OUT;8 has Bool $.color = True;9 has Channel $!commands .= new;10 has Promise $!reader;11 has Bool $!stopped = False;1213 method !c(Str $kind, Str $text --> Str) {14 return $text unless $!color;15 given $kind {16 when 'green' { green($text) }17 when 'red' { red($text) }18 when 'yellow' { yellow($text) }19 when 'cyan' { light-blue($text) }20 default { $text }21 }22 }2324 method banner(Str $msg) {25 $.out.say: self!c('cyan', "[behave watch] $msg");26 }2728 method info(Str $msg) {29 $.out.say: "[behave watch] $msg";30 }3132 method warn(Str $msg) {33 $.out.say: self!c('yellow', "[behave watch] $msg");34 }3536 method error(Str $msg) {37 $.out.say: self!c('red', "[behave watch] $msg");38 }3940 method prompt() {41 $.out.print: self!c('cyan', '[behave watch] ');42 $.out.print: 'press ';43 $.out.print: self!c('green', 'r');44 $.out.print: ' rerun selection, ';45 $.out.print: self!c('green', 'a');46 $.out.print: ' rerun all, ';47 $.out.print: self!c('green', 'f');48 $.out.print: ' failed only, ';49 $.out.print: self!c('green', 'q');50 $.out.say: ' quit';51 }5253 method change-summary(@changes) {54 my @paths = @changes.map(*.path.basename).unique;55 my $msg = "changes detected: " ~ @paths.join(', ');56 $.out.say: self!c('yellow', "[behave watch] $msg");57 }5859 method run-banner(@specs, Str $why) {60 my $count = @specs.elems;61 my $word = $count == 1 ?? 'spec' !! 'specs';62 $.out.say: self!c('cyan',63 "[behave watch] running $count $word ({$why})");64 }6566 method run-finished(Bool $passed) {67 if $passed {68 $.out.say: self!c('green', '[behave watch] passed');69 } else {70 $.out.say: self!c('red', '[behave watch] failed');71 }72 }7374 method start-reader() {75 return if $!reader.defined;76 $!reader = start {77 while !$!stopped {78 my $line = $.in.get;79 last unless $line.defined;80 $!commands.send: $line.trim.lc;81 }82 }83 }8485 method poll-command(--> Str) {86 $!commands.poll // Str;87 }8889 method submit-command(Str $cmd) {90 $!commands.send: $cmd;91 }9293 method stop() {94 $!stopped = True;95 try $!commands.close;96 }97}