Skip to content

Commit a2aee09

Browse files
committed
added logger package
small changes to how terminal prompt is used reworked terminal print styles for clarity
1 parent a647141 commit a2aee09

File tree

27 files changed

+463
-247
lines changed

27 files changed

+463
-247
lines changed

debugger/commands.go

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/jetsetilly/gopher2600/hardware/memory/memorymap"
3434
"github.com/jetsetilly/gopher2600/hardware/riot/input"
3535
"github.com/jetsetilly/gopher2600/linter"
36+
"github.com/jetsetilly/gopher2600/logger"
3637
"github.com/jetsetilly/gopher2600/patch"
3738
"github.com/jetsetilly/gopher2600/symbols"
3839
)
@@ -91,7 +92,7 @@ func (dbg *Debugger) tokeniseCommand(cmd string, scribe bool, echo bool) (*comma
9192
// print normalised input if this is command from an interactive source
9293
// and not an auto-command
9394
if echo {
94-
dbg.printLine(terminal.StyleInput, tokens.String())
95+
dbg.printLine(terminal.StyleEcho, tokens.String())
9596
}
9697

9798
// test to see if command is allowed when recording/playing a script
@@ -306,7 +307,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
306307
s.WriteString("\n\n")
307308
}
308309

309-
dbg.printInstrument(s)
310+
dbg.printLine(terminal.StyleInstrument, s.String())
310311
} else {
311312
dbg.printLine(terminal.StyleFeedback, "cartridge has no static data areas")
312313
}
@@ -316,7 +317,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
316317
case "REGISTERS":
317318
// !!TODO: poke/peek cartridge registers
318319
if bus := dbg.VCS.Mem.Cart.GetRegistersBus(); bus != nil {
319-
dbg.printInstrument(bus.GetRegisters())
320+
dbg.printLine(terminal.StyleInstrument, bus.GetRegisters().String())
320321
} else {
321322
dbg.printLine(terminal.StyleFeedback, "cartridge has no registers")
322323
}
@@ -346,7 +347,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
346347
s.WriteString("\n\n")
347348
}
348349

349-
dbg.printInstrument(s)
350+
dbg.printLine(terminal.StyleInstrument, s.String())
350351
} else {
351352
dbg.printLine(terminal.StyleFeedback, "cartridge has no RAM")
352353
}
@@ -355,7 +356,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
355356
}
356357
}
357358
} else {
358-
dbg.printInstrument(dbg.VCS.Mem.Cart)
359+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.Mem.Cart.String())
359360
}
360361

361362
case cmdPatch:
@@ -745,7 +746,8 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
745746
}
746747
s.WriteString(dbg.Disasm.GetField(disassembly.FldActualNotes, dbg.lastResult))
747748

748-
if dbg.VCS.CPU.LastResult.Final {
749+
// change terminal output style depending on condition of last CPU result
750+
if dbg.lastResult.Result.Final {
749751
dbg.printLine(terminal.StyleCPUStep, s.String())
750752
} else {
751753
dbg.printLine(terminal.StyleVideoStep, s.String())
@@ -798,7 +800,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
798800
if hasMapped {
799801
dbg.printLine(terminal.StyleInstrument, "%s", s.String())
800802
} else {
801-
dbg.printLine(terminal.StyleInstrument, fmt.Sprintf("%v is not a mappable address", address))
803+
dbg.printLine(terminal.StyleFeedback, fmt.Sprintf("%v is not a mappable address", address))
802804
}
803805

804806
} else {
@@ -849,7 +851,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
849851
// already caught by command line ValidateTokens()
850852
}
851853
} else {
852-
dbg.printInstrument(dbg.VCS.CPU)
854+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.CPU.String())
853855
}
854856

855857
case cmdPeek:
@@ -910,16 +912,16 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
910912
}
911913

912914
case cmdRAM:
913-
dbg.printInstrument(dbg.VCS.Mem.RAM)
915+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.Mem.RAM.String())
914916

915917
case cmdTimer:
916-
dbg.printInstrument(dbg.VCS.RIOT.Timer)
918+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.RIOT.Timer.String())
917919

918920
case cmdTIA:
919-
dbg.printInstrument(dbg.VCS.TIA)
921+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.String())
920922

921923
case cmdAudio:
922-
dbg.printInstrument(dbg.VCS.TIA.Audio)
924+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Audio.String())
923925

924926
case cmdTV:
925927
option, ok := tokens.Get()
@@ -947,7 +949,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
947949
// already caught by command line ValidateTokens()
948950
}
949951
} else {
950-
dbg.printInstrument(dbg.tv)
952+
dbg.printLine(terminal.StyleInstrument, dbg.tv.String())
951953
}
952954

953955
// information about the machine (sprites, playfield)
@@ -964,14 +966,14 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
964966

965967
switch plyr {
966968
case 0:
967-
dbg.printInstrument(dbg.VCS.TIA.Video.Player0)
969+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Player0.String())
968970

969971
case 1:
970-
dbg.printInstrument(dbg.VCS.TIA.Video.Player1)
972+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Player1.String())
971973

972974
default:
973-
dbg.printInstrument(dbg.VCS.TIA.Video.Player0)
974-
dbg.printInstrument(dbg.VCS.TIA.Video.Player1)
975+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Player0.String())
976+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Player1.String())
975977
}
976978

977979
case cmdMissile:
@@ -987,21 +989,21 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
987989

988990
switch miss {
989991
case 0:
990-
dbg.printInstrument(dbg.VCS.TIA.Video.Missile0)
992+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Missile0.String())
991993

992994
case 1:
993-
dbg.printInstrument(dbg.VCS.TIA.Video.Missile1)
995+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Missile1.String())
994996

995997
default:
996-
dbg.printInstrument(dbg.VCS.TIA.Video.Missile0)
997-
dbg.printInstrument(dbg.VCS.TIA.Video.Missile1)
998+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Missile0.String())
999+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Missile1.String())
9981000
}
9991001

10001002
case cmdBall:
1001-
dbg.printInstrument(dbg.VCS.TIA.Video.Ball)
1003+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Ball.String())
10021004

10031005
case cmdPlayfield:
1004-
dbg.printInstrument(dbg.VCS.TIA.Video.Playfield)
1006+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.TIA.Video.Playfield.String())
10051007

10061008
case cmdDisplay:
10071009
var err error
@@ -1125,7 +1127,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
11251127
case cmdPanel:
11261128
mode, ok := tokens.Get()
11271129
if !ok {
1128-
dbg.printInstrument(dbg.VCS.Panel)
1130+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.Panel.String())
11291131
return false, nil
11301132
}
11311133

@@ -1173,7 +1175,7 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
11731175
dbg.VCS.Panel.Handle(input.PanelReset, false)
11741176
}
11751177
}
1176-
dbg.printInstrument(dbg.VCS.Panel)
1178+
dbg.printLine(terminal.StyleInstrument, dbg.VCS.Panel.String())
11771179

11781180
case cmdJoystick:
11791181
var err error
@@ -1432,6 +1434,22 @@ func (dbg *Debugger) processTokens(tokens *commandline.Tokens) (bool, error) {
14321434
dbg.Disasm.Prefs.FxxxMirror.Set(!v)
14331435
}
14341436
}
1437+
1438+
case cmdLog:
1439+
option, ok := tokens.Get()
1440+
if ok {
1441+
switch option {
1442+
case "CLEAR":
1443+
logger.Clear()
1444+
}
1445+
} else {
1446+
s := &strings.Builder{}
1447+
if logger.Write(s) {
1448+
dbg.printLine(terminal.StyleLog, s.String())
1449+
} else {
1450+
dbg.printLine(terminal.StyleFeedback, "log is empty")
1451+
}
1452+
}
14351453
}
14361454

14371455
return false, nil

debugger/commands_help.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,5 @@ about an address.`,
486486
cmdClear: "Clear all BREAKS, TRAPS, WATCHES and TRACES.",
487487

488488
cmdPref: "Set preferences for debugger.",
489+
cmdLog: "Print log to terminal.",
489490
}

debugger/commands_template.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ const (
6767
cmdDrop = "DROP"
6868
cmdClear = "CLEAR"
6969

70-
// prefs
70+
// meta
7171
cmdPref = "PREF"
72+
cmdLog = "LOG"
7273
)
7374

7475
const cmdHelp = "HELP"
@@ -124,8 +125,9 @@ var commandTemplate = []string{
124125
cmdDrop + " [BREAK|TRAP|WATCH|TRACE] %<number in list>N",
125126
cmdClear + " [BREAKS|TRAPS|WATCHES|TRACES|ALL]",
126127

127-
// prefs
128+
// meta
128129
cmdPref + " ([LOAD|SAVE]|[SET|UNSET|TOGGLE] [RANDSTART|RANDPINS|FXXXMIRROR])",
130+
cmdLog + " (CLEAR)",
129131
}
130132

131133
// list of commands that should not be executed when recording/playing scripts

debugger/debugger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/jetsetilly/gopher2600/hardware"
3131
"github.com/jetsetilly/gopher2600/hardware/cpu/execution"
3232
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/banks"
33+
"github.com/jetsetilly/gopher2600/logger"
3334
"github.com/jetsetilly/gopher2600/reflection"
3435
"github.com/jetsetilly/gopher2600/setup"
3536
"github.com/jetsetilly/gopher2600/symbols"
@@ -296,8 +297,7 @@ func (dbg *Debugger) loadCartridge(cartload cartridgeloader.Loader) error {
296297

297298
symtable, err := symbols.ReadSymbolsFile(cartload.Filename)
298299
if err != nil {
299-
dbg.printLine(terminal.StyleFeedbackNonInteractive, "%s", err)
300-
// continuing because symtable is always valid even if err non-nil
300+
logger.Log("symbols", err.Error())
301301
}
302302

303303
err = dbg.Disasm.FromMemory(dbg.VCS.Mem.Cart, symtable)

debugger/debugger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (trm *mockTerm) IsInteractive() bool {
149149
}
150150

151151
func (trm *mockTerm) TermPrintLine(sty terminal.Style, s string) {
152-
if sty == terminal.StyleInput {
152+
if sty == terminal.StyleEcho {
153153
return
154154
}
155155

debugger/events.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jetsetilly/gopher2600/debugger/terminal"
2222
"github.com/jetsetilly/gopher2600/errors"
2323
"github.com/jetsetilly/gopher2600/gui"
24+
"github.com/jetsetilly/gopher2600/logger"
2425
"github.com/jetsetilly/gopher2600/playmode"
2526
)
2627

@@ -75,7 +76,7 @@ func (dbg *Debugger) guiEventHandler(ev gui.Event) error {
7576
if ev.Down {
7677
_, err = dbg.parseInput(fmt.Sprintf("%s sl %d & hp %d", cmdBreak, ev.Scanline, ev.HorizPos), false, false)
7778
if err == nil {
78-
dbg.printLine(terminal.StyleFeedbackNonInteractive, "mouse break on sl->%d and hp->%d", ev.Scanline, ev.HorizPos)
79+
logger.Log("mouse break", fmt.Sprintf("on sl->%d and hp->%d", ev.Scanline, ev.HorizPos))
7980
}
8081
}
8182
}

debugger/inputloop.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (dbg *Debugger) inputLoop(inputter terminal.Input, videoCycle bool) error {
192192
dbg.haltImmediately = false
193193

194194
// get user input
195-
inputLen, err := inputter.TermRead(dbg.input, dbg.buildPrompt(videoCycle), dbg.events)
195+
inputLen, err := inputter.TermRead(dbg.input, dbg.buildPrompt(), dbg.events)
196196

197197
// errors returned by UserRead() functions are very rich. the
198198
// following block interprets the error carefully and proceeds
@@ -416,7 +416,7 @@ func (dbg *Debugger) handleInterrupt(inputter terminal.Input, inputLen int) {
416416
_, err := inputter.TermRead(confirm,
417417
terminal.Prompt{
418418
Content: "really quit (y/n) ",
419-
Style: terminal.StylePromptConfirm},
419+
Type: terminal.PromptTypeConfirm},
420420
dbg.events)
421421

422422
if err != nil {

debugger/print.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,3 @@ func (wrt styleWriter) Write(p []byte) (n int, err error) {
6969
wrt.dbg.printLine(wrt.style, string(p))
7070
return len(p), nil
7171
}
72-
73-
func (dbg *Debugger) printInstrument(mi fmt.Stringer) {
74-
dbg.printLine(terminal.StyleInstrument, "%s", mi.String())
75-
}

debugger/prompt.go

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,16 @@ import (
2323
"github.com/jetsetilly/gopher2600/disassembly"
2424
)
2525

26-
func (dbg *Debugger) buildPrompt(videoCycle bool) terminal.Prompt {
27-
prompt := strings.Builder{}
28-
prompt.WriteString("[")
29-
30-
if dbg.scriptScribe.IsActive() {
31-
prompt.WriteString("(rec)")
32-
}
26+
func (dbg *Debugger) buildPrompt() terminal.Prompt {
27+
content := strings.Builder{}
3328

3429
// decide which address value to use
3530
var e *disassembly.Entry
3631
if dbg.VCS.CPU.LastResult.Final || dbg.VCS.CPU.HasReset() {
3732
e = dbg.Disasm.GetEntryByAddress(dbg.VCS.CPU.PC.Address())
3833
} else {
3934
// if we're in the middle of an instruction then use the addresss in
40-
// lastResult. in these instances we want the prompt to report the
35+
// lastResult. in these instances we want the prompt to report the
4136
// instruction that the CPU is working on, not the next one to be
4237
// stepped into.
4338
e = dbg.lastResult
@@ -46,40 +41,29 @@ func (dbg *Debugger) buildPrompt(videoCycle bool) terminal.Prompt {
4641
// build prompt based on how confident we are of the contents of the
4742
// disassembly entry. starting with the condition of no disassembly at all
4843
if e == nil {
49-
prompt.WriteString(" unsure")
44+
content.WriteString("unsure")
5045
} else if e.Level == disassembly.EntryLevelUnused {
51-
prompt.WriteString(fmt.Sprintf(" %s unsure", e.Address))
46+
content.WriteString(fmt.Sprintf("%s unsure", e.Address))
5247
} else {
53-
prompt.WriteString(fmt.Sprintf(" %s %s", e.Address, e.Mnemonic))
48+
// this is the ideal path. the address is in the disassembly and we've
49+
// decoded it already
50+
content.WriteString(fmt.Sprintf("%s %s", e.Address, e.Mnemonic))
5451
if e.Operand != "" {
55-
prompt.WriteString(fmt.Sprintf(" %s", e.Operand))
52+
content.WriteString(fmt.Sprintf(" %s", e.Operand))
5653
}
5754
}
58-
prompt.WriteString(" ]")
5955

60-
// display indicator that the CPU is waiting for WSYNC to end. only applies
61-
// when in video step mode.
62-
if videoCycle && !dbg.VCS.CPU.RdyFlg {
63-
prompt.WriteString(" !")
56+
p := terminal.Prompt{
57+
Content: content.String(),
58+
Recording: dbg.scriptScribe.IsActive(),
59+
CPURdy: dbg.VCS.CPU.RdyFlg,
6460
}
6561

66-
// video cycle prompt
67-
if !dbg.VCS.CPU.LastResult.Final {
68-
if videoCycle {
69-
prompt.WriteString(" > ")
70-
} else {
71-
// we're in the middle of a cpu instruction but this is not a video
72-
// cycle prompt. while this is possible it is unusual. indicate
73-
// this by appending a double question mark
74-
//
75-
// an example of this is when the supercharger.TapeLoaded error has
76-
// been triggered
77-
prompt.WriteString(" ?? ")
78-
}
79-
return terminal.Prompt{Content: prompt.String(), Style: terminal.StylePromptVideoStep}
62+
if dbg.VCS.CPU.LastResult.Final {
63+
p.Type = terminal.PromptTypeCPUStep
64+
} else {
65+
p.Type = terminal.PromptTypeVideoStep
8066
}
8167

82-
// cpu cycle prompt
83-
prompt.WriteString(" >> ")
84-
return terminal.Prompt{Content: prompt.String(), Style: terminal.StylePromptCPUStep}
68+
return p
8569
}

0 commit comments

Comments
 (0)