Inspired by the comments and answers here I made this code which uses the environ package:
\usepackage{environ}
\environbodyname\yourinput
\NewEnviron{autocentredtext}{%
\setbox0=\hbox{\yourinput}
\ifdim\wd0<\textwidth
\begin{center}
\yourinput
\end{center}
\fi
\noindent\yourinput
}%[\ignorespacesafterend]
This creates an environment which is implemented by \begin{autocentredtext}...\end{autocentredtext} which first reads your text input, checks if it's shorter than \textwidth, and if it is, it gets centred or otherwise it becomes non-indented.
One might want to add [\ignorespacesafterend] at the end of the above code to fix some possible spacing issues.
Unfortunately, it seems that this cannot be used with \newenvironment. For example,
\newenvironment{autocentering}{\begin{autocentredtext}}{\end{autocentredtext}}
creates an error. If one uses \NewEnviron instead of \newenvironment, they should be able to overcome this.
\captionwithsinglelinecheck, but for, say, aminipageinstead? – Jim May 20 '20 at 03:23\hboxand\ifdim\wd0>\columnwidth. See also https://tex.stackexchange.com/questions/233687/how-not-to-show-caption-of-figure-but-only-of-subfigure-in-list-of-figures/233724#233724 – John Kormylo May 20 '20 at 03:37