From 10d2c2aa1faf565c8d1bc697dad4b6bee301821b Mon Sep 17 00:00:00 2001
From: Marco Matthies <71844+marcom@users.noreply.github.com>
Date: Fri, 31 Jan 2025 00:31:58 +0100
Subject: [PATCH] Don't implement Base.show for Matrix{Pixel}

Rename this function to `showlandscape()`.
---
 src/world/landscape.jl  | 30 +++++++++++++++++++++++++++---
 test/landscape_tests.jl |  2 +-
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/world/landscape.jl b/src/world/landscape.jl
index bf8757d..4d3e2b6 100644
--- a/src/world/landscape.jl
+++ b/src/world/landscape.jl
@@ -31,11 +31,14 @@ Pixel(landcover::LandCover, fieldid::Union{Missing,Int64}) =
     Pixel(landcover, fieldid, Vector{Management}(), Vector{Int64}(), Vector{Int64}())
 Pixel(landcover::LandCover) = Pixel(landcover, missing)
 
-function Base.show(io::IO, ::MIME"text/plain", mat::T) where T <: AbstractMatrix{Pixel}
-    max_fieldid = maximum(skipmissing(map(x -> getfield(x, :fieldid), mat)); init=0)
+showlandscape(mat::T) where T <: AbstractMatrix{Pixel} = showlandscape(stdout, mat)
+
+function showlandscape(io::IO, mat::T) where T <: AbstractMatrix{Pixel}
     println(io, "Matrix{Pixel}:")
+
     println(io, "  LandCover:")
     nrow, ncol = size(mat)
+    max_fieldid = maximum(skipmissing(map(x -> getfield(x, :fieldid), mat)); init=0)
     for i in axes(mat, 1)
         print(io, "    ")
         for j in axes(mat, 2)
@@ -49,12 +52,33 @@ function Base.show(io::IO, ::MIME"text/plain", mat::T) where T <: AbstractMatrix
         end
         println(io)
     end
-
     # print legend
     legend = join(map(x -> "$(uppercase(first(string(x)))) = $x", instances(Persefone.LandCover)), ", ")
     println(io, "Legend: ", legend)
 
+    soiltype_to_str(s::SoilType) = replace(string(s), r"^soiltype_" => "")
+    soiltype_to_char(s::SoilType) = uppercase(first(soiltype_to_str(s)))
+    println(io)
+    println(io, "  Soil types:")
+    for i in axes(mat, 1)
+        print(io, "    ")
+        for j in axes(mat, 2)
+            charid = soiltype_to_char(mat[i, j].soiltype)
+            fieldid = if ismissing(mat[i, j].fieldid)
+                repeat(" ", ndigits(max_fieldid))
+            else
+                @sprintf("%*s", ndigits(max_fieldid), mat[i, j].fieldid)
+            end
+            print(io, charid, fieldid, " ")
+        end
+        println(io)
+    end
+    # print legend
+    legend = join(map(x -> "$(soiltype_to_char(x)) = $(soiltype_to_str(x))", instances(Persefone.SoilType)), ", ")
+    println(io, "Legend: ", legend)
+
     # print number of unique animals, events, territories
+    println(io)
     nanimals = length(unique(reduce(vcat, map(x -> getfield(x, :animals), mat))))
     nevents = length(unique(reduce(vcat, map(x -> getfield(x, :events), mat))))
     nterritories = length(unique(reduce(vcat, map(x -> getfield(x, :territories), mat))))
diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl
index 7f1fe42..3b10446 100644
--- a/test/landscape_tests.jl
+++ b/test/landscape_tests.jl
@@ -22,7 +22,7 @@ end
 @testset "Utility functions" begin
     model = inittestmodel()
     iobuf = IOBuffer()
-    show(iobuf, MIME"text/plain"(), model.landscape)
+    Ps.showlandscape(iobuf, model.landscape)
     @test length(take!(iobuf)) > 0
 end
 
-- 
GitLab