Skip to content

Commit

Permalink
Added --timestamp option to monitor command to fix #2316.
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonler committed Sep 22, 2023
1 parent 0e6133f commit 18aada4
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions internal/cli/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"sort"
"strings"
"time"

"github.com/arduino/arduino-cli/commands/monitor"
"github.com/arduino/arduino-cli/configuration"
Expand All @@ -44,12 +45,13 @@ var tr = i18n.Tr
// NewCommand created a new `monitor` command
func NewCommand() *cobra.Command {
var (
raw bool
portArgs arguments.Port
describe bool
configs []string
quiet bool
fqbn arguments.Fqbn
raw bool
portArgs arguments.Port
describe bool
configs []string
quiet bool
timestamp bool
fqbn arguments.Fqbn
)
monitorCommand := &cobra.Command{
Use: "monitor",
Expand All @@ -59,20 +61,21 @@ func NewCommand() *cobra.Command {
" " + os.Args[0] + " monitor -p /dev/ttyACM0\n" +
" " + os.Args[0] + " monitor -p /dev/ttyACM0 --describe",
Run: func(cmd *cobra.Command, args []string) {
runMonitorCmd(&portArgs, &fqbn, configs, describe, quiet, raw)
runMonitorCmd(&portArgs, &fqbn, configs, describe, timestamp, quiet, raw)
},
}
portArgs.AddToCommand(monitorCommand)
monitorCommand.Flags().BoolVar(&raw, "raw", false, tr("Set terminal in raw mode (unbuffered)."))
monitorCommand.Flags().BoolVar(&describe, "describe", false, tr("Show all the settings of the communication port."))
monitorCommand.Flags().StringSliceVarP(&configs, "config", "c", []string{}, tr("Configure communication port settings. The format is <ID>=<value>[,<ID>=<value>]..."))
monitorCommand.Flags().BoolVarP(&quiet, "quiet", "q", false, tr("Run in silent mode, show only monitor input and output."))
monitorCommand.Flags().BoolVar(&timestamp, "timestamp", false, tr("Timestamp each incoming line."))
fqbn.AddToCommand(monitorCommand)
monitorCommand.MarkFlagRequired("port")
return monitorCommand
}

func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, quiet, raw bool) {
func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []string, describe, timestamp, quiet, raw bool) {
instance := instance.CreateAndInit()
logrus.Info("Executing `arduino-cli monitor`")

Expand Down Expand Up @@ -160,6 +163,10 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str
feedback.FatalError(err, feedback.ErrGeneric)
}

if timestamp {
ttyOut = newTimeStampWriter(ttyOut)
}

ctx, cancel := cleanup.InterruptableContext(context.Background())
if raw {
feedback.SetRawModeStdin()
Expand Down Expand Up @@ -241,3 +248,35 @@ func contains(s []string, searchterm string) bool {
}
return false
}

type timeStampWriter struct {
writer io.Writer
sendTimeStampNext bool
}

func newTimeStampWriter(writer io.Writer) *timeStampWriter {
return &timeStampWriter{
writer: writer,
sendTimeStampNext: true,
}
}

func (t *timeStampWriter) Write(p []byte) (int, error) {
written := 0
for _, b := range p {
if (t.sendTimeStampNext) {
_, err := t.writer.Write([]byte(time.Now().Format("[2006-01-02 15:04:05] ")))
if err != nil {
return written, err
}
t.sendTimeStampNext = false;
}
n, err := t.writer.Write([]byte{b})
written += n
if err != nil {
return written, err
}
t.sendTimeStampNext = b == '\n';
}
return written, nil
}

0 comments on commit 18aada4

Please sign in to comment.