From bf5a7df4608a0cdf5b1c145fcf2002560f3ecf44 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 10 Mar 2023 00:20:47 +0900 Subject: refactor: move common snippet to util.go --- cmd/clock.go | 20 +------------------- cmd/timer.go | 20 +------------------- cmd/util.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 38 deletions(-) create mode 100644 cmd/util.go (limited to 'cmd') diff --git a/cmd/clock.go b/cmd/clock.go index 71244c4..71f01c7 100644 --- a/cmd/clock.go +++ b/cmd/clock.go @@ -13,25 +13,7 @@ func drawClock(scr *term.Screen, now time.Time, bgStyle, fgStyle term.Style) { // Clear the entire screen. scr.Clear(bgStyle) - // Calculate square width/height and offset. - scrW, scrH := scr.Size() - // 17 - // <---------------> - // ### ### ### ### ^ - // # # # # # # # # # | - // # # # # # # # # | 5 - // # # # # # # # # # | - // ### ### ### ### v - squareW := scrW / (17 + 2) - squareH := scrH / (5 + 2) - if squareH > squareW { - squareH = squareW - } - if squareW > squareH*3/2 { - squareW = squareH * 3 / 2 - } - xOffset := (scrW - squareW*17) / 2 - yOffset := (scrH - squareH*5) / 2 + squareW, squareH, xOffset, yOffset := calcSquareSize(scr) // Hour hour := now.Hour() diff --git a/cmd/timer.go b/cmd/timer.go index b4f9857..0e0ff48 100644 --- a/cmd/timer.go +++ b/cmd/timer.go @@ -18,25 +18,7 @@ func drawTimer(scr *term.Screen, leftTime time.Duration, bgStyle, fgStyle term.S // Clear the entire screen. scr.Clear(bgStyle) - // Calculate square width/height and offset. - scrW, scrH := scr.Size() - // 17 - // <---------------> - // ### ### ### ### ^ - // # # # # # # # # # | - // # # # # # # # # | 5 - // # # # # # # # # # | - // ### ### ### ### v - squareW := scrW / (17 + 2) - squareH := scrH / (5 + 2) - if squareH > squareW { - squareH = squareW - } - if squareW > squareH*3/2 { - squareW = squareH * 3 / 2 - } - xOffset := (scrW - squareW*17) / 2 - yOffset := (scrH - squareH*5) / 2 + squareW, squareH, xOffset, yOffset := calcSquareSize(scr) // Minute minute := leftTime.Minutes() diff --git a/cmd/util.go b/cmd/util.go new file mode 100644 index 0000000..93199da --- /dev/null +++ b/cmd/util.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "github.com/nsfisis/term-clock/internal/term" +) + +func calcSquareSize(scr *term.Screen) (int, int, int, int) { + // Calculate square width/height and offset. + scrW, scrH := scr.Size() + // 17 + // <---------------> + // ### ### ### ### ^ + // # # # # # # # # # | + // # # # # # # # # | 5 + // # # # # # # # # # | + // ### ### ### ### v + squareW := scrW / (17 + 2) + squareH := scrH / (5 + 2) + if squareH > squareW { + squareH = squareW + } + if squareW > squareH*3/2 { + squareW = squareH * 3 / 2 + } + xOffset := (scrW - squareW*17) / 2 + yOffset := (scrH - squareH*5) / 2 + + return squareW, squareH, xOffset, yOffset +} -- cgit v1.2.3-70-g09d2