summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doPlayback.go2
-rw-r--r--termSize.go41
-rw-r--r--terminal_width.go16
3 files changed, 42 insertions, 17 deletions
diff --git a/doPlayback.go b/doPlayback.go
index 8482805..1489879 100644
--- a/doPlayback.go
+++ b/doPlayback.go
@@ -86,7 +86,7 @@ func listChats(chats *chatpb.Chats) {
// print out one line for each chat entry
func listEntries(chat *chatpb.Chat) {
log.Printf("--- Entries for Topic: %s ---\n", chat.GetChatName())
- width := getTerminalWidth()
+ width, _ := getTerminalWidth()
// Determine the maximum length of the author and time string
maxAuthorAndTimeLen := 0
diff --git a/termSize.go b/termSize.go
new file mode 100644
index 0000000..a3201f1
--- /dev/null
+++ b/termSize.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "log"
+ "os"
+
+ "golang.org/x/term"
+)
+
+// getTerminalWidth returns the width of the active terminal.
+// If the output is not an interactive terminal (e.g., it's being piped to a file
+// or another command), it returns a default width and false.
+func getTerminalWidth() (int, bool) {
+ // term.IsTerminal checks if the given file descriptor is connected to a terminal.
+ // We use os.Stdout.Fd() to check the standard output.
+ if term.IsTerminal(int(os.Stdout.Fd())) {
+ // term.GetSize returns the dimensions of the given terminal.
+ width, _, err := term.GetSize(int(os.Stdout.Fd()))
+ if err != nil {
+ // If we can't get the size for some reason, fall back to the default.
+ log.Printf("could not get terminal size: %v", err)
+ return 120, false
+ }
+ return width, true
+ }
+
+ // If it's not a terminal, return the default width.
+ return 120, false
+}
+
+// truncateString shortens a string to the specified length, adding an ellipsis if truncated.
+func truncateString(s string, maxLength int) string {
+ if len(s) <= maxLength {
+ return s
+ }
+ // Subtract 3 to make room for the ellipsis "..."
+ if maxLength < 3 {
+ return "..."
+ }
+ return s[:maxLength-3] + "..."
+}
diff --git a/terminal_width.go b/terminal_width.go
deleted file mode 100644
index fe623c6..0000000
--- a/terminal_width.go
+++ /dev/null
@@ -1,16 +0,0 @@
-package main
-
-import (
- "os"
-
- "golang.org/x/term"
-)
-
-func getTerminalWidth() int {
- width, _, err := term.GetSize(int(os.Stdout.Fd()))
- if err != nil {
- // Return a default width if there's an error
- return 80
- }
- return width
-}