From 1f871b533947a6c551c2b1e595585e651bc653d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fernando=20Carri=C3=B3n?= Date: Mon, 12 Aug 2024 22:29:23 +0200 Subject: [PATCH] better organize the package and make main a test --- requivalence/equivalence.go | 20 ++++++++++++++++++++ codetable.go => vt220/codetable.go | 3 ++- control.go => vt220/control.go | 2 +- equivalence.go => vt220/equivalence.go | 2 +- graphics.go => vt220/graphics.go | 2 +- vt220.go => vt220/vt220.go | 14 ++++++-------- main.go => vt220/vt220_test.go | 11 ++++++----- term.go => vtterm.go | 10 ++++++++-- 8 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 requivalence/equivalence.go rename codetable.go => vt220/codetable.go (99%) rename control.go => vt220/control.go (99%) rename equivalence.go => vt220/equivalence.go (95%) rename graphics.go => vt220/graphics.go (99%) rename vt220.go => vt220/vt220.go (96%) rename main.go => vt220/vt220_test.go (94%) rename term.go => vtterm.go (79%) diff --git a/requivalence/equivalence.go b/requivalence/equivalence.go new file mode 100644 index 0000000..954f4d3 --- /dev/null +++ b/requivalence/equivalence.go @@ -0,0 +1,20 @@ +package requivalence + +type equivalenceTable map[rune]rune + +type EquivalenceTable struct { + et equivalenceTable +} + +func (table EquivalenceTable) Normalize(r rune) rune { + if normal, ok := table.et[r]; ok { + return normal + } + return r +} + +var Equivalence = EquivalenceTable{ + et: equivalenceTable{ + 'a': 'a', + }, +} diff --git a/codetable.go b/vt220/codetable.go similarity index 99% rename from codetable.go rename to vt220/codetable.go index 8140308..1ab7882 100644 --- a/codetable.go +++ b/vt220/codetable.go @@ -1,4 +1,4 @@ -package main +package vt220 import ( "errors" @@ -67,6 +67,7 @@ func (r *GraphicRange) load(gOverlay *CharacterSet) { r.set[*graph] = &r.table[i][j] } } + // fmt.Printf("%+v", r.set) } func (rg *GraphicRange) lockShift(register GraphicRegister, w io.Writer) error { diff --git a/control.go b/vt220/control.go similarity index 99% rename from control.go rename to vt220/control.go index 7c9bf75..43d72c9 100644 --- a/control.go +++ b/vt220/control.go @@ -1,4 +1,4 @@ -package main +package vt220 import ( "io" diff --git a/equivalence.go b/vt220/equivalence.go similarity index 95% rename from equivalence.go rename to vt220/equivalence.go index 14eadf4..bcf18ce 100644 --- a/equivalence.go +++ b/vt220/equivalence.go @@ -1,4 +1,4 @@ -package main +package vt220 type equivalenceTable map[rune]rune diff --git a/graphics.go b/vt220/graphics.go similarity index 99% rename from graphics.go rename to vt220/graphics.go index a31f552..f0d9848 100644 --- a/graphics.go +++ b/vt220/graphics.go @@ -1,4 +1,4 @@ -package main +package vt220 type Grapheme *rune diff --git a/vt220.go b/vt220/vt220.go similarity index 96% rename from vt220.go rename to vt220/vt220.go index e43cd07..d53d07b 100644 --- a/vt220.go +++ b/vt220/vt220.go @@ -1,14 +1,12 @@ -package main +package vt220 import ( "bytes" "errors" "fmt" "io" -) -var ( - VT220Type = TermType{"vt220"} + requiv "git.sdf.org/CRThaze/vtTools/requivalence" ) type graphicRegister struct { @@ -32,7 +30,7 @@ type VT220 struct { fontBuffer **CharacterSet } -func newVT220(w io.Writer) *VT220 { +func NewVT220(w io.Writer) *VT220 { return &VT220{ writer: w, c0: ControlRange{ @@ -141,7 +139,7 @@ func (vt *VT220) LockShift(registerIndex int, right bool) error { if right { rg = vt.gr } - return rg.lockShift(vt.graphicRegistry[registerIndex], vt) + return rg.lockShift(vt.graphicRegistry[registerIndex], vt.writer) } func (vt *VT220) SingleShiftRune(registerIndex int, r rune) ([]byte, error) { @@ -239,7 +237,7 @@ func (vt *VT220) TransDynamic(r rune) []byte { } } for i, set := range vt.graphicRepetoire { - if !set.lookup.Has(r) { + if set == nil || !set.lookup.Has(r) { continue } // TODO: Don't leave this in the register. @@ -258,7 +256,7 @@ func (vt *VT220) TransDynamic(r rune) []byte { // SafeTrans uses an equivalency table for the given rune to determine a similiar rune that is // present in the GraphicRepetoir. It then calls TransDynamic with that rune. func (vt *VT220) SafeTransDynamic(r rune) []byte { - return vt.TransDynamic(Equivalence.Normalize(r)) + return vt.TransDynamic(requiv.Equivalence.Normalize(r)) } func (vt VT220) write(buf *bytes.Buffer) (n int, err error) { diff --git a/main.go b/vt220/vt220_test.go similarity index 94% rename from main.go rename to vt220/vt220_test.go index 506b7d6..95565c0 100644 --- a/main.go +++ b/vt220/vt220_test.go @@ -1,11 +1,12 @@ -package main +package vt220 import ( "os" + "testing" "time" ) -var term = NewVTTerm(VT220Type, os.Stdout) +var term = NewVT220(os.Stdout) var testMSCASCIIGraphic = [16][6]rune{ {' ', '0', '@', 'P', '`', 'p'}, @@ -90,10 +91,10 @@ func printTest(sl, sr [16][6]rune) { term.Print("\n") } -func main() { +func TestOutput(t *testing.T) { term.Init() printTest(testMSCASCIIGraphic, testMSCSupplementalGraphic) - // term.(*VT220).LockShift(2, true) + term.LockShift(2, true) printTest(testMSCASCIIGraphic, testSpecialGraphics) - // term.(*VT220).LockShift(1, true) + term.LockShift(1, true) } diff --git a/term.go b/vtterm.go similarity index 79% rename from term.go rename to vtterm.go index 1d93e6c..38ee3a9 100644 --- a/term.go +++ b/vtterm.go @@ -1,7 +1,9 @@ -package main +package vttools import ( "io" + + "git.sdf.org/CRThaze/vtTools/vt220" ) type TermType struct { @@ -16,6 +18,10 @@ func (id TermType) String() string { return id.ID() } +var ( + VT220Type = TermType{"vt220"} +) + type VTTerm interface { io.Writer Init() @@ -29,7 +35,7 @@ func NewVTTerm(ttype TermType, w io.Writer) VTTerm { var term VTTerm switch ttype { case VT220Type: - term = newVT220(w) + term = vt220.NewVT220(w) } return term }