Skip to content

Commit

Permalink
Merge pull request #14 from merschformann/merschformann/inline
Browse files Browse the repository at this point in the history
Adds inline option
  • Loading branch information
merschformann committed Jul 30, 2022
2 parents c0924f9 + 6a2e53e commit d48b8ee
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 42 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ gotz
Show arbitrary time:

```bash
gotz 15
gotz 21
```

![preview](material/screenshot/gotz-15.png)
Expand All @@ -40,7 +40,7 @@ Show arbitrary time using different timezone (index based):
gotz 15@1
```

![preview](material/screenshot/gotz-15-1.png)
![preview](material/screenshot/gotz-15-2.png)

Time can be one of the following formats:

Expand All @@ -64,6 +64,8 @@ gotz --live true

![preview](material/screenshot/gotz-live.png)

(above also uses option `--inline false`; for styling see customization below)

## Basic configuration

Set the timezones to be used by default:
Expand Down Expand Up @@ -157,6 +159,8 @@ The configuration is stored in `$XDG_CONFIG_HOME/gotz/config.json` (usually `~/.
"tics": false,
// Indicates whether to stretch across the full terminal width (causes inhomogeneous segment lengths)
"stretch": true,
// Inline indicates whether location and time info will be plotted on one line with the bars.
"inline": true,
// Indicates whether to colorize the blocks
"hours12": false,
// Indicates whether to use 12-hour format
Expand Down
18 changes: 17 additions & 1 deletion core/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
// Check for any changes
var changed bool
// Define configuration flags
var timezones, symbols, tics, stretch, colorize, hours12, live string
var timezones, symbols, tics, stretch, inline, colorize, hours12, live string
flag.StringVar(
&timezones,
"timezones",
Expand Down Expand Up @@ -45,6 +45,12 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
"",
"indicates whether to stretch across the terminal width at cost of accuracy (one of: true, false)",
)
flag.StringVar(
&inline,
"inline",
"",
"indicates whether to display time info and bars in one line (one of: true, false)",
)
flag.StringVar(
&colorize,
"colorize",
Expand Down Expand Up @@ -120,6 +126,16 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
return startConfig, rt, changed, fmt.Errorf("invalid value for stretch: %s", stretch)
}
}
if inline != "" {
changed = true
if strings.ToLower(inline) == "true" {
startConfig.Inline = true
} else if strings.ToLower(inline) == "false" {
startConfig.Inline = false
} else {
return startConfig, rt, changed, fmt.Errorf("invalid value for inline: %s", inline)
}
}
if colorize != "" {
changed = true
if strings.ToLower(colorize) == "true" {
Expand Down
4 changes: 4 additions & 0 deletions core/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Config struct {
// Indicates whether to stretch across the terminal width at cost of
// accuracy.
Stretch bool `json:"stretch"`
// Inline indicates whether location and time info will be plotted on one
// line with the bars.
Inline bool `json:"inline"`
// Indicates whether to use the 24-hour clock.
Hours12 bool `json:"hours12"`

Expand Down Expand Up @@ -154,6 +157,7 @@ func DefaultConfig() Config {
},
Tics: false,
Stretch: true,
Inline: true,
}
}

Expand Down
114 changes: 76 additions & 38 deletions core/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ func Plot(c Config, t time.Time) error {
style = styles[t]
}
// Print message
for i, r := range fmt.Sprint(msg) {
s.SetContent(x+i, y, r, nil, style)
for _, r := range fmt.Sprint(msg) {
s.SetContent(x, y, r, nil, style)
x++
}
}
Expand Down Expand Up @@ -222,11 +222,28 @@ func Plot(c Config, t time.Time) error {

// PlotTime plots the time on the terminal.
func PlotTime(plt Plotter, cfg Config, t time.Time) error {
// Get infos and time zones for all locations
timeInfos, timeZones, err := createTimeInfos(cfg, t)
if err != nil {
return err
}

// Determine time info width
timeInfoWidth := 0
if cfg.Inline {
for _, ti := range timeInfos {
if len(ti) > timeInfoWidth {
timeInfoWidth = len(ti)
}
}
timeInfoWidth++ // Leave a space between time info and bars
}

// Get terminal width
width := plt.TerminalWidth
width := plt.TerminalWidth - timeInfoWidth
// Set hours to plot
hours := 24
// Get terminal width
// Use integral time slots with no rounding issues, if desired
if !cfg.Stretch {
width = width / 24 * 24
}
Expand All @@ -236,15 +253,15 @@ func PlotTime(plt Plotter, cfg Config, t time.Time) error {
slotMinutes := hours * 60 / width
offsetMinutes := slotMinutes * width / 2
// Plot header
nowDescription := "now"
nowTag := "now"
if !plt.Now {
nowDescription = "time"
nowTag = "time"
}
plt.PlotLine(
ContextNormal,
strings.Repeat(" ",
nowSlot-(len(nowDescription)+1))+
nowDescription+" v "+
timeInfoWidth+nowSlot-(len(nowTag)+1))+
nowTag+" v "+
formatTime(cfg.Hours12, t))
// Prepare slots
for i := 0; i < width; i++ {
Expand All @@ -256,40 +273,25 @@ func PlotTime(plt Plotter, cfg Config, t time.Time) error {
}
}

// Prepare timezones to plot
timezones := make([]*time.Location, len(cfg.Timezones)+1)
descriptions := make([]string, len(cfg.Timezones)+1)
timezones[0] = time.Local
descriptions[0] = "Local"
for i, tz := range cfg.Timezones {
// Get timezone
loc, err := time.LoadLocation(tz.TZ)
if err != nil {
return fmt.Errorf("error loading timezone %s: %s", tz.TZ, err)
}
// Store timezone
timezones[i+1] = loc
descriptions[i+1] = tz.Name
}
descriptionLength := maxStringLength(descriptions)

// Plot all timezones
for i := range timezones {
// --> Plot header
desc := fmt.Sprintf("%-*s", descriptionLength, descriptions[i])
desc = fmt.Sprintf(
"%s: %s %s",
desc,
formatDay(cfg.Hours12, t.In(timezones[i])),
formatTime(cfg.Hours12, t.In(timezones[i])))
if len(desc)-1 < nowSlot {
desc = desc + strings.Repeat(" ", nowSlot-len(desc)) + "|"
// Plot all locations
for i := range timeInfos {
// Start with location info
timeInfo := timeInfos[i]
if cfg.Inline {
// Plot time info and continue in same line
timeInfo += " "
plt.PlotString(ContextNormal, timeInfo)
} else {
// Plot time info (also add the vertical marker) and start new line
if len(timeInfo)-1 < nowSlot {
timeInfo = timeInfo + strings.Repeat(" ", nowSlot-len(timeInfo)) + "|"
}
plt.PlotLine(ContextNormal, timeInfo)
}
plt.PlotLine(ContextNormal, desc)
// --> Plot timeslots
for j := 0; j < width; j++ {
// Convert to tz time
tzTime := timeSlots[j].Time.In(timezones[i])
tzTime := timeSlots[j].Time.In(timeZones[i])
// Get symbol of slot
s := getHourSymbol(plt, tzTime.Hour())
// Get segment type of slot
Expand All @@ -311,6 +313,42 @@ func PlotTime(plt Plotter, cfg Config, t time.Time) error {
return nil
}

// createTimeInfos creates the time info strings for all locations.
func createTimeInfos(cfg Config, t time.Time) (timeInfos []string, times []*time.Location, err error) {
// Init
timeInfos = make([]string, len(cfg.Timezones)+1)

// Prepare timeZones to plot
timeZones := make([]*time.Location, len(cfg.Timezones)+1)
descriptions := make([]string, len(cfg.Timezones)+1)
timeZones[0] = time.Local
descriptions[0] = "Local"
for i, tz := range cfg.Timezones {
// Get timezone
loc, err := time.LoadLocation(tz.TZ)
if err != nil {
return nil, nil, fmt.Errorf("error loading timezone %s: %s", tz.TZ, err)
}
// Store timezone
timeZones[i+1] = loc
descriptions[i+1] = tz.Name
}
descriptionLength := maxStringLength(descriptions)

for i := range timeZones {
// Prepare location and time infos
timeInfo := fmt.Sprintf("%-*s", descriptionLength, descriptions[i])
timeInfo = fmt.Sprintf(
"%s: %s %s",
timeInfo,
formatDay(cfg.Hours12, t.In(timeZones[i])),
formatTime(cfg.Hours12, t.In(timeZones[i])))
timeInfos[i] = timeInfo
}

return timeInfos, timeZones, nil
}

// plotTics adds tics to the plot.
func plotTics(plt Plotter, hours12 bool, timeSlots []timeslot, width int) {
// Prepare tics
Expand Down
Binary file removed material/screenshot/gotz-15-1.png
Binary file not shown.
Binary file added material/screenshot/gotz-15-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified material/screenshot/gotz-15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified material/screenshot/gotz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/testdata/static_inline.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
now v 16:00
Local : Sat 24 Aug 1985 14:00 ▒▒▒██████████|██████▒▒▒▒▒▒▒
New York: Sat 24 Aug 1985 10:00 ▒▒▒███|█████████████▒▒▒▒▒▒
Berlin : Sat 24 Aug 1985 16:00 ▒▒▒█████████████|███▒▒▒▒▒▒
Shanghai: Sat 24 Aug 1985 22:00 ██████████████▒▒▒▒▒▒| ▒▒▒███
Sydney : Sun 25 Aug 1985 00:00 ██████████▒▒▒▒▒▒▒ | ▒▒▒▒██████
49 changes: 49 additions & 0 deletions tests/testdata/static_inline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"config_version": "1.0",
"timezones": [
{
"Name": "New York",
"TZ": "America/New_York"
},
{
"Name": "Berlin",
"TZ": "Europe/Berlin"
},
{
"Name": "Shanghai",
"TZ": "Asia/Shanghai"
},
{
"Name": "Sydney",
"TZ": "Australia/Sydney"
}
],
"style": {
"symbols": "rectangles",
"colorize": false,
"day_segments": {
"morning": 6,
"day": 8,
"evening": 18,
"night": 22
},
"coloring": {
"StaticColorMorning": "red",
"StaticColorDay": "yellow",
"StaticColorEvening": "red",
"StaticColorNight": "blue",
"StaticColorForeground": "",
"DynamicColorMorning": "red",
"DynamicColorDay": "yellow",
"DynamicColorEvening": "red",
"DynamicColorNight": "blue",
"DynamicColorForeground": "",
"DynamicColorBackground": ""
}
},
"tics": false,
"stretch": true,
"inline": true,
"hours12": false,
"live": false
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const Version = "v0.1.5"
const Version = "v0.1.6"

0 comments on commit d48b8ee

Please sign in to comment.