diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/colors/colors.go (renamed from util/colors.go) | 2 | ||||
-rw-r--r-- | util/git/hash.go (renamed from util/hash.go) | 2 | ||||
-rw-r--r-- | util/lamport/lamport.go (renamed from util/lamport.go) | 28 | ||||
-rw-r--r-- | util/lamport/lamport_test.go (renamed from util/lamport_test.go) | 6 | ||||
-rw-r--r-- | util/lamport/persisted_lamport.go (renamed from util/persisted_lamport.go) | 32 | ||||
-rw-r--r-- | util/process/process.go (renamed from util/process.go) | 6 | ||||
-rw-r--r-- | util/text/left_padded.go (renamed from util/left_padded.go) | 2 | ||||
-rw-r--r-- | util/text/text.go (renamed from util/text.go) | 30 | ||||
-rw-r--r-- | util/text/text_test.go (renamed from util/text_test.go) | 6 |
9 files changed, 46 insertions, 68 deletions
diff --git a/util/colors.go b/util/colors/colors.go index 5aa34363..c57e9019 100644 --- a/util/colors.go +++ b/util/colors/colors.go @@ -1,4 +1,4 @@ -package util +package colors import "github.com/fatih/color" diff --git a/util/hash.go b/util/git/hash.go index 0a3964a0..c5fdfad4 100644 --- a/util/hash.go +++ b/util/git/hash.go @@ -1,4 +1,4 @@ -package util +package git import ( "fmt" diff --git a/util/lamport.go b/util/lamport/lamport.go index 4d5dcf5b..640c58bc 100644 --- a/util/lamport.go +++ b/util/lamport/lamport.go @@ -25,47 +25,47 @@ */ -package util +package lamport import ( "sync/atomic" ) -// LamportClock is a thread safe implementation of a lamport clock. It +// Clock is a thread safe implementation of a lamport clock. It // uses efficient atomic operations for all of its functions, falling back // to a heavy lock only if there are enough CAS failures. -type LamportClock struct { +type Clock struct { counter uint64 } -// LamportTime is the value of a LamportClock. -type LamportTime uint64 +// Time is the value of a Clock. +type Time uint64 -func NewLamportClock() LamportClock { - return LamportClock{ +func NewClock() Clock { + return Clock{ counter: 1, } } -func NewLamportClockWithTime(time uint64) LamportClock { - return LamportClock{ +func NewClockWithTime(time uint64) Clock { + return Clock{ counter: time, } } // Time is used to return the current value of the lamport clock -func (l *LamportClock) Time() LamportTime { - return LamportTime(atomic.LoadUint64(&l.counter)) +func (l *Clock) Time() Time { + return Time(atomic.LoadUint64(&l.counter)) } // Increment is used to return the value of the lamport clock and increment it afterwards -func (l *LamportClock) Increment() LamportTime { - return LamportTime(atomic.AddUint64(&l.counter, 1) - 1) +func (l *Clock) Increment() Time { + return Time(atomic.AddUint64(&l.counter, 1) - 1) } // Witness is called to update our local clock if necessary after // witnessing a clock value received from another process -func (l *LamportClock) Witness(v LamportTime) { +func (l *Clock) Witness(v Time) { WITNESS: // If the other value is old, we do not need to do anything cur := atomic.LoadUint64(&l.counter) diff --git a/util/lamport_test.go b/util/lamport/lamport_test.go index de7f1822..c650fe6a 100644 --- a/util/lamport_test.go +++ b/util/lamport/lamport_test.go @@ -25,14 +25,14 @@ */ -package util +package lamport import ( "testing" ) -func TestLamportClock(t *testing.T) { - l := &LamportClock{} +func TestClock(t *testing.T) { + l := &Clock{} if l.Time() != 0 { t.Fatalf("bad time value") diff --git a/util/persisted_lamport.go b/util/lamport/persisted_lamport.go index c8c898e2..4f12dd1b 100644 --- a/util/persisted_lamport.go +++ b/util/lamport/persisted_lamport.go @@ -1,4 +1,4 @@ -package util +package lamport import ( "fmt" @@ -7,21 +7,21 @@ import ( "path/filepath" ) -type PersistedLamport struct { - LamportClock +type Persisted struct { + Clock filePath string } -func NewPersistedLamport(filePath string) *PersistedLamport { - clock := &PersistedLamport{ - LamportClock: NewLamportClock(), - filePath: filePath, +func NewPersisted(filePath string) *Persisted { + clock := &Persisted{ + Clock: NewClock(), + filePath: filePath, } return clock } -func LoadPersistedLamport(filePath string) (*PersistedLamport, error) { - clock := &PersistedLamport{ +func LoadPersisted(filePath string) (*Persisted, error) { + clock := &Persisted{ filePath: filePath, } @@ -33,18 +33,18 @@ func LoadPersistedLamport(filePath string) (*PersistedLamport, error) { return clock, nil } -func (c *PersistedLamport) Increment() (LamportTime, error) { - time := c.LamportClock.Increment() +func (c *Persisted) Increment() (Time, error) { + time := c.Clock.Increment() return time, c.Write() } -func (c *PersistedLamport) Witness(time LamportTime) error { +func (c *Persisted) Witness(time Time) error { // TODO: rework so that we write only when the clock was actually updated - c.LamportClock.Witness(time) + c.Clock.Witness(time) return c.Write() } -func (c *PersistedLamport) read() error { +func (c *Persisted) read() error { content, err := ioutil.ReadFile(c.filePath) if err != nil { return err @@ -61,12 +61,12 @@ func (c *PersistedLamport) read() error { return fmt.Errorf("could not read the clock") } - c.LamportClock = NewLamportClockWithTime(value) + c.Clock = NewClockWithTime(value) return nil } -func (c *PersistedLamport) Write() error { +func (c *Persisted) Write() error { dir := filepath.Dir(c.filePath) err := os.MkdirAll(dir, 0777) if err != nil { diff --git a/util/process.go b/util/process/process.go index ddd3f704..f3bcb7f2 100644 --- a/util/process.go +++ b/util/process/process.go @@ -1,12 +1,12 @@ -package util +package process import ( "os" "syscall" ) -// ProcessIsRunning tell is a process is running -func ProcessIsRunning(pid int) bool { +// IsRunning tell is a process is running +func IsRunning(pid int) bool { // never return no error in a unix system process, err := os.FindProcess(pid) diff --git a/util/left_padded.go b/util/text/left_padded.go index 69c88074..a38dfba5 100644 --- a/util/left_padded.go +++ b/util/text/left_padded.go @@ -1,4 +1,4 @@ -package util +package text import ( "strings" diff --git a/util/text.go b/util/text/text.go index 3b2b0020..10b70b01 100644 --- a/util/text.go +++ b/util/text/text.go @@ -1,41 +1,19 @@ -package util +package text import ( "bytes" "strings" ) -func WordWrap(text string, lineWidth int) (string, int) { - words := strings.Fields(strings.TrimSpace(text)) - if len(words) == 0 { - return "", 1 - } - lines := 1 - wrapped := words[0] - spaceLeft := lineWidth - len(wrapped) - for _, word := range words[1:] { - if len(word)+1 > spaceLeft { - wrapped += "\n" + word - spaceLeft = lineWidth - len(word) - lines++ - } else { - wrapped += " " + word - spaceLeft -= 1 + len(word) - } - } - - return wrapped, lines -} - // Wrap a text for an exact line size // Handle properly terminal color escape code -func TextWrap(text string, lineWidth int) (string, int) { - return TextWrapPadded(text, lineWidth, 0) +func Wrap(text string, lineWidth int) (string, int) { + return WrapLeftPadded(text, lineWidth, 0) } // Wrap a text for an exact line size with a left padding // Handle properly terminal color escape code -func TextWrapPadded(text string, lineWidth int, leftPad int) (string, int) { +func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) { var textBuffer bytes.Buffer var lineBuffer bytes.Buffer nbLine := 1 diff --git a/util/text_test.go b/util/text/text_test.go index a368b329..96234464 100644 --- a/util/text_test.go +++ b/util/text/text_test.go @@ -1,11 +1,11 @@ -package util +package text import ( "strings" "testing" ) -func TestTextWrap(t *testing.T) { +func TestWrap(t *testing.T) { cases := []struct { Input, Output string Lim int @@ -92,7 +92,7 @@ func TestTextWrap(t *testing.T) { } for i, tc := range cases { - actual, lines := TextWrap(tc.Input, tc.Lim) + actual, lines := Wrap(tc.Input, tc.Lim) if actual != tc.Output { t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n\n`%s`", i, tc.Input, tc.Output, actual) |